Article | Talk | Edit | History  

CE - Intro to programming/Application of concepts/loops

From the World Wide Wiki

"If you ain't got no master, you can't be a slave!" -- Evan Greer

Jump to: navigation, search
Computer Engineering
Index of articles in the Computer Engineering Curriculum
Prereqs
*Science prereqs
*Calc I - derivatives and intergrals
* Electrostatics
100 level
*Intro to computer engineering
*Intro to programming
*Intro to electricty
*Calc II - limits and series
200 level
*Linear circuits
*Intro to digital logic
*Intro to Object Oriented Programming with Java
300 level
*Computer architecture
*Intro to electronic devices
*Programming in C and C++
400 level
*Embedded systems
*Networks
*Programming Data Structures and Algorithms
*Signal processing
Electives
*Additional topics in computer programming

This article is part of a series of articles intending to offer a curriculum of Computer Engineering. For information, please see Category:Computer engineering curriculum.

We discussed loops a little in the general introduction to programming concepts. Now we're going to learn how to use them in C++

Contents

Boolean operators

Remember that the key to looping is that it continue to execute a block of code, over and over, until some test condition fails. Test conditions have to evaluate to bool types, which we already discussed as a primitive type. However, using a static bool value as a test condition is not generally useful. Instead, we want to be able to perform operations on various pieces of data which will result in a bool type value. That's where boolean operators come in. Boolean operators are simply those operators whose resulting value is of type bool.

equality and non-equality operators

These are the most basic boolean operator. The equality operator tests to see if the two operands are equal in value. If they are, the operation returns true. If they are not, it returns false. In C++, the equality operator is two equals signs side by side (no space between them, remember, white space constitutes a delimiter).

The non-equality operator is just the opposite, it tests if two value are not-equal. The operator returns true if the values are not equal, and false if they are equal. The non-equality operator in C++ is an exclamation point immediately followed by a single equals sign.

The following code assigns a bool value to a variable based on whether two ints are equal in value:

#include <iostream.h>
 
int main(){
    
    int x = 10;
    int y = 15;
    int z = 15;
    
    bool b1 = x == y;    //false, x is not equal to y
    bool b2 = y == z;    //true, y is equal to z
    bool b3 = x != z;    //true, x is not equal to z
    
    cout << b1 << endl << b2 << endl << b3 << endl;
    
}

If you compile and run this, you should see simply:

0
1
1

Notice that cout prints the bool value false as 0 and true as 1.


greater than and less than, or equal to

Besides just checking to see if things are equal, we can see if some value is numerically greater than another. Or we can check to see if it's less than another. Or we can see if it's greater than or equal to. Or less than or equal to. These operators look a lot like the do in mathematics. Less than is a left angle bracket: <. Greater than is a right angle bracket: >. Less than or equal to is just <=, and greater than or equal to is >=. try the following code, all the bool values should be true.

int main(){
    
    int x1 = 1;
    int x2 = 1;
    int y1 = 2;
    int y2 = 2;
    
    bool b1 = x1 < y1;    //1 is less 2, true
    bool b2 = x1 <= y1;    //1 is less than or equal to 2, true
    bool b3 = x1 <= x2;    //1 is less than or equal to 1, true
    bool b4 = y1 > x1;    //2 is greter than 1, true
    bool b5 = y1 >= x1;    //2 is greater than or equal to 1, true
    bool b6 = y1 >= y2;    //2 is greater than or equal to 2, true.    
    
}

Short-circuit combinational operators

The short-circuit combinational operators are based on Boolean algebra, and perform an operation on two bool values. These operators include and, or, and not. Like it's name sake in Boolean algebra, the and-operator produces a true result if and only if both of the operands are true. The short circuit and-operator in C++ is two ampersands, side by side: && . The or-operator, on the other hand, produces a true result if either of the operands are true. This operator is two parallel vertical lines side by side: ||. (The keyboard key for a single vertical line is usually the shift version of the forwards slash. The double vertical line is just two of these characters in a row). The not-operator only takes one operand (following the operator), and simply returns the opposite bool value. the not operator is simply an exclamation point: !.

These operators are called short-circuit operators because they stop evaluating their operands as soon as possible. For example, the short circuit or operator will not evaluate the second operand if the first operand is true, since the result of the operation will be true no matter what. Likewise, the short circuit and will stop evaluating if the first operand if false, because the result will always be false, regardless of the second operand.

This may seem insignificant, but it is actually very important if your operands are result values. For instance, imagine you have two functions that each return bool values. The first function locks the doors of your house, and returns true if and only if the doors successfully locked. The second function checks to see if there are intruders in your house and, if there are, turns on an alarm. This function returns true if there are no intruders in your house. Now lets say you have some program that's set to run after you leave your house, or before you go to bed, or whatever. This program calls the other two functions and checks to see that they both returned true, indicating that your house is safe. If you use the doorLock function as the first operand to a short circuit and operator, and the checkForIntruders function as the second, you could be in for a nasty surprise if the intruder is holding the doors open. In this case, the doors will fail to lock, the doorLock function will return false, the short-circuit and will fail and therefore shortcircuit, preventing the checkForIntruders function (which turns on an alarm, remember) from ever getting called.

How that's a rather outlandish example, but it illustrates the point. We'll later learn about the bit-wise combinational operators which can be used as non-short circuit operators. But for now, if you really need a non-short circuit, just call the functions outside of the operator, store the result in a variable, and use that as the operand.


While loops

Okay, back to loops, so we can put some of these operators into practice.

Enlarge

The simplest loop in C++ is the while loop, illustrated at the left. A while loop starts with a test condition which is evaluated at the beginning of every iteration, continues by executing the code in the code block if and only if the test condition evaluates to true, and then jumps back up to the beginning to re-evaluate the test condition and start all over. The general structure looks like this:

while(<test condition>){
<code block to execute in a loop>
}


Do while loops

For loops


This article is a stub.