First | Next | Previous | Last | Glossary | About |
The decision structure is an essential part of algorithms in computer programming. If a computer language can't evaluate decisions then we can't write programs that can branch.
In computing terms a decision is a structure that has a truth value. It can only be either TRUE or FALSE.
In C++ decisions, also known as conditions or propositions or logical expressions or relational expressions or boolean expressions, appear enclosed inside parentheses:
if (relational expression) statement1; else statement2; // optional
The (relational expression) is evaluated, if TRUE then statement1 is executed else statement2 is executed.
C++ has the bool data type which it treats as numeric values (usually true is 1, or at least non-zero, and false is zero), so (relational expression) since it is numeric can also be (expression != 0) which has the same effect as (relational expression).
We use the relational operators when evaluating relational expressions:
Example | Comments |
if (n> 0) if (a> b) z = a; else z = b; |
Without any indication to the contrary, an else belongs to the previous if. In this case the else belongs with the condition (a> b). |
if (n> 0) { if (a> b) z = a; } else z = b; |
To change this association, uses braces as shown here. In this case the else belongs with the condition (n> 0). |
A semi-colon follows the statement associated with if. A statement is always terminated by a semi- colon.
We can combine many relational expressions by using the logical operators. For instance:
Here are some examples using the syntax of C++:
Example | Comments |
Logical AND if ((a > b) && (a < 22)) c = a + b; else c = a - b; |
There are two conditions (a > b) logically combined with (a < 22) and they are combined using the logical operator AND which in C++ is written &&. If (a > b) is true AND (a < 22) is true then the statement c = a + b; is executed. Note that the whole logical expression is enclosed in parentheses ((a > b) && (a < 22)). If either (a > b) or (a < 22) is false then the whole expression ((a > b) && (a < 22)) is false and the statement c = a - b is executed. |
Logical OR if ((a > b) || (a < 22)) c = a + b; else c = a - b; |
In this example there are two conditions (a > b) logically combined with (a < 22) and they are combined using the logical operator OR which in C++ is written || (the pipe symbol or broken bar). If (a > b) is true OR (a < 22) is true then the statement c = a + b; is executed. Note again that the whole logical expression is enclosed in parentheses ((a > b) || (a < 22)). The statement c = a - b will only be executed if (a > b) is false and (a < 22) is false. |
Logical NOT if !(a > b) c = a + b; else c = a - b; |
In this example there is a single condition (a > b) logically combined with the logical operator NOT which in C++ is written ! (exclamation mark). The ! operator changes the truth sense of the relational expression. If (a > b) is true then !(a > b) is false. In this case the statement c = a + b is executed. If (a > b) is false then !(a > b) is true and the statement c = a - b is executed. |
The table below shows truth tables for each of the logical operators &&, || and !.
AND |
OR |
NOT |
||||||||||||||||||||||||||||||||||||
|
|
|
A common requirement in a computer program is the need to make decisions based on the outcome of previous decisions. For example if x is true then do y else if j is true then do z else if ...
if (expression) statement1; else if (expression) statement2; else if (expression) statement3; else if (expression) statement4; else statement5;
If any expression is true then its associated statement is executed and the chain is terminated. The last else is the default condition in the event that none of the (expression)'s are true.
The switch statement achieves a similar effect to the nested if else. It tests whether an expression matches a number of constant values.
switch (expression) { case 1 : statement1a; /* execute the statements */ statement1b; /* then fall out of the */ statement1c; /* switch statement */ break; case 2 : statement2; break; case 3 : statement3; break; case 4 : /* a fall-through case */ case 5 : case 6 : statement456; break; default : statementdefault; /* do this if all else fails */ break; } // end of switch
The (expression) is evaluated and its value compared with all the cases. Each case must be labelled by an integer constant, character constant or constant expression. The break statement terminates the switch. The default case executes if all other cases fail (it is optional), the last break (in the default case) isn't required but is good defensive programming practice, you may want to add more cases later.
Example | Comments |
#include <iostream> int main() { int i; cin >> i; switch (i) { case 1 : ++i; cout << i << endl; break; case 2 : --i; cout << i << endl; break; case 3 : i += 9; cout << i << endl; break; case 4 : case 5 : case 6 : cout << (i < 4) << endl; break; default : cout << i << endl; break; } // end of switch return 0; } |
The program accepts a value for i from the stdin device and tests this in the switch statement to determine if it is one of several values. Cases 1, 2 and 3 each do something differen. The code for each case is executed and then the break statement follows, breaking out from the switch. Cases 4, 5 and 6 are all combined into a single case. In the event that i was not in the range 1 to 6 there is a default case. |
#include <iostream> #include <ctype.h> int main() { char i; cout << "Enter a single character: "; cin >> i; i = toupper(i); switch (i) { case 'A' : ++i; cout << i << endl; break; case 'B' : --i; cout << i << endl; break; case 'C' : i += 9; cout << i << endl; break; case 'D' : case 'E' : case 'F' : cout << (i < 4) << i * 2 << endl; break; default : cout << i << endl; break; } // end of switch return 0; } |
In this example we use a switch statement with characters. Note the use of the toupper() function. You need to include the file ctype.h to use this function. As the name suggests toupper() converts a character to its uppercase form. This example shows too that you can do arithmetic with chars. Since the char is a simple data type then operations like + and - are quite OK. It is usually a good idea to enclose expressions headed for the cout stream in parentheses. This helps to avoid complaints from the compiler. |
The while loop has the form: while (expression) statement; which means that while (expression) is true execute statement. The while loop is distinguished by its having the condition before any processing which is within the scope of the loop.
The expression must be enclosed in parentheses and must evaluate to a boolean value, ie true or false, for example:
while (i > 9) ... while (a != 'Q') ... while (x) ... //x is treated as bool
The statement can be a simple statement or a compound statement, for example:
while (i > 9) //using a simple statement j += 4; while (a != 'Q') //using a compound statement { j += 4; s = "123456" cout << j; }
#include <iostream> int main() { int i = 1; while (true) { cout << "Enter an integer value: "; cin >> i; if (i > 10) break; } return 0; }
Here is an interesting example of using a "permanent" while which uses the break statement to "break out" of the loop. The condition for the while always true.
#include <iostream> int main() {bool value = true; int i = 1; while (value) { ++i; value = i < 5; cout << "value is " << value << " i is " << i << endl; } return 0; } produces the output: value is 1 i is 2 value is 1 i is 3 value is 1 i is 4 value is 0 i is 5
The last while example shows how you would normally use a while loop. The loop contains a statement which will terminate the loop when a certain condition arises. You can see too that the bool value is treated as an integer. A bool has a non-zero value (1) when true and is zero when false.
do statement while (expression);
The do ... while loop is a repeat ... until loop. The do ... while loop is distinguished by its having the condition after any processing which is within the scope of the loop.
//ex15.cc #include <iostream> int main() { int number, right_digit; cout << "enter a number "; cin >> number; do { right_digit = number % 10; cout << right_digit; number = number / 10; } while (number != 0); return 0; }
Here is an example of do ... while which reverses the digits of a number:
The general form of a for loop is:
for ( initial condition; terminal condition; iteration step) statement;
or
for (expr1; expr2; expr3) statement;
The for loop is a special case of a while loop:
expr1; while (expr2) { statement; expr3; }
Usually expr1 and expr3 are assignment statements and expr2 a relational expression but the expressions can be left out:
for(;;) { some statements }
The relational expression is assumed always true which means a break or return would have to be used to break out of the loop.
Here is are some simple examples of the for loop:
Example | Comments |
#include <iostream> int main() { int i; for (i = 0; i < 10; ++i) cout << " " << i; return 0; } |
|
#include <iostream> int main() { for (int i = 10; i < 30; i += 4) cout << " " << i; return 0; } |
|
#include <iostream> #include <math.h> int main() { int i; double j; for (i = 8; i < 16; i++) cout << "2 " << " raised to the power " << i << " is " << pow(2,i) << endl; cout << hex; for (i = 8; i < 16; i++) { j = pow(2,i); cout << "2 " << " raised to the power " << i << " is " << (long) j << endl; } return 0; } ========= a modification ========== ... long j; ... cout << hex; for (i = 8; i < 16; i++) { j = (long) pow(2,i); cout << "2 " << " raised to the power " << i << " is " << j << "H" << endl; } ... |
|
The comma operator most often finds use in a for statement. A pair of operands separated by a comma is evaluated left to right and the type and value of the result are the type and value of the right operand, i.e. multiple expressions can be placed in the for statement expressions.
#include <iostream≶ #include <math.h> //we probably dont need this include file #include <time.h> //we need this to use the time function #include <stdlib.h> //we need this for the srand() and rand() functions #define MAXTRIES 10 //Maximum number of tries int main() { int compguess, //the computer's number myguess, //the user's guess count = 0; //a count of tries srand(time(0)); //seed the random number generator //with the current time compguess = rand() + 1; //get a random number // // RAND_MAX is the largest random value // It is defined in stdlib.h // cout << "RAND_MAX is " << RAND_MAX << endl; myguess = 0; do { cout << "Enter your guess: "; cin >> myguess; // // You now have the user's guess // Use if ... else to determine if the user's guess is correct, too low // or too high. // // Print these messages where appropriate: // // This is try number X your guess was correct // This is try number X your guess was too low // This is try number X your guess was too high // // X is the current try count // Keep a count of the user's attempts // You will need to complete this condition // While guess isn't the same as the computer number and we have // more tries yet } while ( ); cout << "Computer's number was " << compguess; return 0; }
First | Next | Previous | Last | Glossary | About |
Copyright © 1999 - 2001
David Beech