CHAPTER THREE: CONTROL STATEMENTS
CONDITIONAL STATEMENTS
If statement
If-else statement
If… , else if…, else statement
Switch statement
LOOPING STATEMENTS
For loop statement
While loop statement
Do…while statement
OTHER STATEMENTS
Break statement
Return statement
Continue statement
Faculty of Computational Science Department of Computer Science Instructor Dech R
3. CONTROL STATEMENTS
3.1. Introduction
• The order in which statements are executed is called
flow control or control flow.
• Flow control in a program is typically sequential, from
one statement to the next, but may be diverted to
other paths by branch statements.
• C++ provides different forms of statements for
different purposes.
Faculty of Computational Science Department of Computer Science Instructor Dech R
Con’t
Declaration statements are used for defining variables.
Assignment-like statements are used for simple algebraic
computations.
Branching statements are used for specifying alternate
paths of execution, depending on the outcome of a
logical condition.
Loop statements are used for specifying computations,
which need to be repeated until a certain logical
condition is satisfied.
Faculty of Computational Science Department of Computer Science Instructor Dech R
3.2. CONDITIONAL STATEMENTS
3.2.1. the if statement
• a conditional statement that allows you to
execute a block of code only if a specific
condition is true.
• the general form of which is:
if (expression)
statement;
Faculty of Computational Science Department of Computer Science Instructor Dech R
Con’t
First expression is evaluated. If the outcome is
nonzero (true), then statement is executed.
Otherwise, nothing happens.
• For example, when dividing two values, we may
want to check that the denominator is nonzero:
if (count != 0)
average = sum / count;
Faculty of Computational Science Department of Computer Science Instructor Dech R
Con’t
• To make multiple statements dependent on the
same condition, we can use a compound
statement:
if (balance > 0) {
interest = balance * creditRate;
balance += interest;
}
Faculty of Computational Science Department of Computer Science Instructor Dech R
3.2.2. if ….else statement
• A variant form of the if statement allows us to specify two
alternative statements.
• one is executed if a condition is satisfied and the other is
executed if the condition is not satisfied.
It has the general form:
if (expression)
statement1;
else
statement2;
Faculty of Computational Science Department of Computer Science Instructor Dech R
Con’t
For example:
if (balance > 0) {
interest = balance * creditRate;
balance += interest;
else {
interest = balance * debitRate;
balance += interest;
}
Faculty of Computational Science Department of Computer Science Instructor Dech R
3.2.3. if… else if… else
• The if... else if... else statement is used in programming to
execute different blocks of code based on certain
conditions.
• General form:
if(expression){
statement1;}
else if(expression){
statement2;}
else{
statement3;}
Faculty of Computational Science Department of Computer Science Instructor Dech R
For example.
if (ch >= '0' && ch <= '9'){
kind = digit; }
else if (ch >= 'A' && ch <= 'Z'){
kind = capitalLetter; }
else if (ch >= 'a' && ch <= 'z'){
kind = smallLetter; }
Else{
kind = special; }
Faculty of Computational Science Department of Computer Science Instructor Dech R
3.2.4. The switch statement
The switch statement provides a way of choosing between a set of
alternatives, based on the value of an expression.
The general form of the switch statement is:
switch (expression) {
case constant1:
statements;
break;
.
.
.
case constantn:
statements;
break;
default:
statements;
Faculty} of Computational Science Department of Computer Science Instructor Dech R
Con’t
• First expression (called the switch tag) is
evaluated, and the outcome is compared to each
of the numeric constants (called case labels), in
the order they appear, until a match is found.
• The statements following the matching case are
then executed.
• The final default case is optional and is exercised
if none of the earlier cases provide a match.
Faculty of Computational Science Department of Computer Science Instructor Dech R
Example
switch(mark){
case 100:
grade=‘A’;
break;
case 80:
grade=‘B’;
break;
case 60:
grade=‘C’;
break;
case : 40:
grade=‘D’;
break;
default:
cout<<“invalid grade”;
break;
}
Faculty of Computational Science Department of Computer Science Instructor Dech R
3.3. LOOPING STATEMENTS
3.3.1. for loop
Allows a block of code to be executed repeatedly in
a certain number of times, based on a specified
condition or iteration over a sequence.
• The general form of the for loop statement is:
for (expression1; expression2; expression3)
statement;
Faculty of Computational Science Department of Computer Science Instructor Dech R
Con’t
• First expression1 is evaluated. Each time round
the loop, expression2 is evaluated.
• If the outcome is nonzero then statement is
executed and expression3 is evaluated.
Otherwise, the loop is terminated.
• The most common use of for loops is for
situations where a variable is incremented or
decremented with every iteration of the loop.
Faculty of Computational Science Department of Computer Science Instructor Dech R
example
sum = 0;
for (i = 1; i <= n; i++)
sum += i;
• C++ allows the first expression in a for loop to be a
variable definition.
• For example
• We can also define i inside the loop itself:
for (int i = 1; i <= n; ++i)
sum += i;
Faculty of Computational Science Department of Computer Science Instructor Dech R
3.3.2. while loop
• while loop, in programming repeats a block of
code as long as a specified condition remains
true.
• It's a control flow statement used for iterative
tasks.
• General form of while loop is:
while(expression)
statement;
Faculty of Computational Science Department of Computer Science Instructor Dech R
Con’t
• First expression (called the loop condition) is
evaluated.
• If the outcome is nonzero then statement (called the
loop body) is executed and the whole process is
repeated.
• Otherwise, the loop is terminated.
• For example, suppose we wish to calculate the sum of
all numbers from 1 to some integer denoted by n.
• This can be expressed as:
Faculty of Computational Science Department of Computer Science Instructor Dech R
Con’t
i = 1;
sum = 0;
while (i <= n)
sum += i;
i++;
Let n=5.
Iteration i n i <= n sum += i
First 1 5 1 1
Second 2 5 1 3
Third 3 5 1 6
Fourth 4 5 1 10
Fifth 5 5 1 15
Sixth 6 5 0
Faculty of Computational Science Department of Computer Science Instructor Dech R
3.3.3. do… while loop
• Executes a block of code at least once, then repeatedly executes it as long
as a given condition remains true.
• For example.
int a, b;
do{
cout<<"enter any number"<<endl;
cin>>a;
cout<<“The number you entered is :"<<a<<endl;
cout<<"Do you want to continue? (press 1 for yes)"<<endl;
cin>>b;
while(b==1);
Faculty of Computational Science Department of Computer Science Instructor Dech R