csc1023 Chapter5
csc1023 Chapter5
Control Statements
Boolean expressions and
relational operators
In C++ the testing of conditions is done with the use of Boolean expressions which
yield bool values that are either true or false. The simplest and most common way to
construct such an expression is to use the so-called relational operators.
if ( boolean-expression )
statement;
The IF selection control
statement
The if statement tests for a particular condition (expressed
as a boolean expression) and only executes the following
statement(s) if the condition is true. An example follows of
a fragment of a program which tests if the denominator is
not zero before attempting to calculate fraction.
if(total != 0)
fraction = counter/total;
If the value of total is 0, the boolean expression above is
false and the statement assigning the value of fraction is
ignored.
The IF selection control
statement
If a sequence of statements is to be executed, this can be
done by making a compound statement or block by
enclosing the group of statements in braces.
if( boolean-expression )
{
statements;
}
An example of this is:
if(total != 0)
{
fraction = counter/total;
cout << "Proportion = " << fraction << endl;
}
The IF/ELSE selection control
statement
Often it is desirable for a program to take one branch if the
condition is true and another if it is false.
if( boolean-expression )
statement-1;
else
statement-2;
The IF/ELSE selection control
statement
This can be done by using an if/else selection statement.
Again, if a sequence of statements is to be executed, this is
done by making a compound statement by using braces to
enclose the sequence:
if( boolean-expression )
{statements; }
else
{ statements; }
ELSE IF multiple selection
statement
Occasionally a decision has to be made on the value of a
variable which has more than two possibilities. This can be
done by placing if statements within other if-else
constructions. This is commonly known as nesting and a
different style of indentation is used to make the multiple-
selection functionality much clearer. This is given below:
if( boolean-expression-1 )
statement-1;
else if( boolean-expression-2 )
statement-2;
else
statement-N;
ELSE IF multiple selection
statement
Example:
#include <iostream.h> else if (age==100)
{
main(){ cout<< “You are old\n”;
int age; }
else
cout<< “Please input your {
age: ”; cout<< “You are really
cin>>age; old\n”;
}
if (age<100){ }
cout<< “ Your are still
young!\n” ;}
Switch(x) statement
Instead of using multiple if/else statements C++ also
provides a special control structure, switch.
For a variable x the switch(x) statement tests whether x is
equal to the constant values x1, x2, x3, etc. and take
appropriate action. The default option is the action to be
taken if the variable does not have any of the values listed.
The break statement causes the program to proceed to the
first statement after the switch structure. Note that the
switch control structure is different to the others in that
braces are not required around multiple statements.
Switch(x) statement
Example:
switch( x )
case x3:
{
statements3;
casex1:
break;
statements1;
default:
break;
statements4;
break;
case x2:
}
statements2;
break;
WHILE repetition control
statements
Repetition control statements allow the programmer to
specify actions which are to be repeated while some
condition is true. In the while repetition control structure:
While ( boolean-expression )
{
statements;
}
WHILE repetition control
statements
The boolean expression (condition) is tested and the statements (or
statement) enclosed by the braces are (is) executed repeatedly while the
condition given by the boolean expression is true. The loop terminates
as soon as the boolean expression is evaluated and tests false.
Execution will then continue on the first line after the closing brace.
The true represents a boolean expression which could be x == 1 or
while ( x != 7 ) (x does not equal 7). It can be any combination of
boolean statements that are legal. Even, (while x ==5 || v == 7) which
says execute the code while x equals five or while v equals 7. Notice
that a while loop is the same as a for loop without the initialization and
update sections. However, an empty condition is not legal for a while
loop as it is with a for loop.
WHILE repetition control
statements
Note that if the boolean expression is initially false the statements
(or statement) are not executed. In the following example the
boolean condition becomes false when the first negative number is
input at the keyboard. The sum is then printed.
do {
statements;
}
The DO..WHILE repetition
control statement
The condition is tested at the end #include <iostream.h>
of the block instead of the
using namespace std;
beginning, so the block will be
executed at least once. If the main(){
condition is true, it jumps back to int x;
the beginning of the block and
x=0;
execute it again. A do..while
loop is basically a reversed while do {
loop. A while loop says "Loop cout<< “hello
while the condition is true, and there!\n”;
execute this block of code", a
do..while loop says "Execute this } while (x!=0);
block of code, and loop while the return;
condition is true". }
Increment and decrement
operators
Increasing and decreasing the value of an integer variable is a
commonly used method for counting the number of times a loop is
executed. C++ provides a special operator ++ to increase the value of a
variable by 1. The following are equivalent ways of incrementing a
counter variable by 1.
count = count + 1;
count++;
main()
{