C++ Chapter Three
C++ Chapter Three
Control Statements
1
Outline
• Introduction
• Selection
Statements
• Iteration Statements
• Jump Statements
2
Introduction
🞂 Control statements alter the flow of the program, the
sequence of statements that are executed in a
program.
🞂They act as "direction signals" to control the path of a
program.
🞂 Used to cause the flow of control to advance and branch
based on changes to the state of a program.
🞂 C++ control statements are categorized in
to three. Control Statements
Selection Stat.
Iteration Stat.
Jump Statements
Selection Statements
🞂 It is also called as a decision making statements.
🞂 It allows the code to execute a statement or block of
statements conditionally.
🞂 one or more conditions to be evaluated or tested by
the program
🞂 There are two types of decisions statements
in C++: Selection Statements
If Statements
Switch Statements
If Statement
🞂 An if statement consists of a Boolean expression
followed by one or more statements.
🞂 Syntax:
if (expression)
{ statement
;
}
rest_of_program
;
🞂 expression must evaluate to a boolean value, either true
or false
🞂 If expression evaluates to true then the block of code
inside the if statement will be executed.
Simple if
If else
If else
Ladder
6 Nested If
If Statement
• The if decision statement executes a statement only
if an expression is true
Syntax:
if (expression)
{ statement1;
}
rest_of_program
9
If-Else Statement
• If-else followed by an optional else statement, which
executes when the Boolean expression is false.
if (expression)
{ statement1;
}
else{ state
ment2;
}
•
next_statement;
• If the expression is true, statement1 is executed
and then next_statement is executed.
Syntax:
if (expression){
statement1;
} else
{ statement
2;
}
rest_of_progr
am
11
Example: if-else
int main(){
int x = 30;
if(x < 20)
{
cout<<“T
his is if
statement"
;
}else {
cout<<"
This is
else
stateme
12
nt";
Nested if statement
🞂 It is refer to the else if statement inside another if or
else if statement.
🞂A nested if is an if statement that is the target of
another if or else.
🞂 Nested ifs are very common in programming.
Syntax:
if(Boolean_expression 1) {
// Executes when the Boolean expression 1 is true
if(Boolean_expression 2) {
// Executes when the Boolean expression 2 is true
}
}
13
Cont’d …
Example1:
14
If-else-if ladder Statement
🞂 The if-else-if statement (if else ladder) executes
one condition from multiple statements.
🞂 An if statement can be followed by an optional
else if...else statement.
🞂 very useful to test various conditions using single
if...else if statement.
🞂 Used when:
• An if can have zero or one else's and it must come after
any else if's.
• An if can have zero to many else if's and they must come
before the else.
• Once an else if succeeds, none of the remaining else if's or
else's will be tested.
15
Cont’d …
🞂 Syntax:
if(condition1){
Statement 1
}
else if(condition2{
Statement 2
}
else if(condition3){
Statement 3
}
...
else{ State
ment n
}
16
Cont’d …
int main(){
int x = 30;
if( x == 10 )
{ cout<<“Value of X is
10";
}else if( x == 20 ) {
cout<<“Value of X is 20";
}else if( x == 30 )
{ cout<<“Value of X is
30";
}else {
cout<<“This is else
statement";
17
}}
Exercise:
18
Switch Statements
What is
switch?
🞂 Syntax:
switch(expression) {
case value ://
Statements break; //
optional
case value : //
Statements break; //
optional
Cont’d …
• When a break
statement is reached,
the switch terminates,
and the flow of
control jumps to the
next line following the
switch statement.
• If no break appears,
the flow of control
will fall through to
subsequent cases until
a break is reached.
21
Example: 1
int main(){ case 'D' :
// char grade = cout<<"You passed";
args[0].charAt(0); case 'F' :
char grade = cout<<"Better try again";
'C'; break;
switch(grade) default :
{ case 'A' : cout<<
cout<<"Excellent!"; "Invalid
break; grade";
case }
Cout<<"
'B' :
Your grade is "
case 'C' : < < grade);
cout<<"Well
22 done"; }
Exercise
Write a program to display days of a week using
switch statement
23
Iteration Statements
What is Iteration
Statements?
25
Cont’d …
🞂 There are three types of loops in C+
+:
for loops
while loops
do-while loops
26
for loop
allows you to efficiently write a loop that needs to
be executed a specific number of times.
useful when you know how many times a task is to
be repeated.
for loop is used to iterate a part of the program
several
times.
If the number of iteration is fixed, it is recommended
to use for loop.
For loop contains 3 parts Initialization, Condition
and Increment or Decrements
27
Cont’d …
Syntax:
for ( initialization; condition; increment ){
statement(s);
}
28
Cont’d …
Explanation:
🞂 The initialization step is executed first, and only once.
• This allows you to declare and initialize variables.
• Not required to put a statement here, as long as a
semicolon appears.
🞂 Increment/ decrement
• This statement allows you to update variables.
29
Example:
Write a program that display “Hello Ethiopia” 6 times using
for loop.
30
Cont’d …
🞂 For loop allows you to
efficiently write a loop
that needs to be
executed a specific
number of times.
31
Cont’d …
🞂 If there is more than one variable to set up or increment
they are separated by a comma.
for(i=0, j=0; i*j<100;i++,j+=2){
cout<<(i * j);
}
🞂 You do not have to fill all three
control expressions but you
must still have two semicolons.
int n = 0;
for(; n <= 100;){
cout<<(++n);
} Example:
🞂 Example 1:
What will this for loop do? Example: 2
What will this for loop do?
Solution
Solution
Prints out each integer from 0 to 1+2 +3 +4 +5 +6 +7
999,
+ 8 + 9 + 10 = 55
correctly labeling them even or odd
While Loop
🞂 This while loop executes as long as the given
logical expression between parentheses is true.
🞂 When expression is false, execution continues with
the
statement following the loop block.
Syntax:
while (expression){
statement;
}
🞂 The expression is tested at the beginning of the loop, so if
it is initially false, the loop will not be executed at all.
🞂 if the boolean_expression result is true, then the actions
inside the loop will be executed. This will continue as
long as the expression result is true.
Cont’d … Example:
1+2 +3 +4 +5 +6 +7+8
+ 9 + 10 = 55
35
Cont’d …
Output
Example2:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x :
36 19
Questions
🞂 Write a C++ program for the following
Questions.
Write
a program that display sum of 20
consecutive numbers input from a
user
37
do … while
🞂 It
is similar to a while loop, except that a do...while
loop is guaranteed to execute at least one time.
Syntax:
do {
// Statements
}
while(Boolean_expression);
38
Cont’d …
🞂 The do…while repetition statement is similar to the
while statement.
41
J u m p Statements
🞂 Also known as a loop control statements
🞂 Loop control statements change execution from its
normal sequence.
🞂 When execution leaves a scope, all automatic
objects that were created in that scope are
destroyed.
J u m p Statements
Continue Statement
Break Statement
Return Statement
42
Continue Statements
🞂 Thecontinue keyword can be used in any of the
loop control structures.
🞂 It
causes the loop to immediately jump to the
next iteration of the loop.
🞂 Ina while loop or do/while loop, control immediately
jumps to the Boolean expression.
🞂 The continue statement causes the program to jump
to the next iteration of the loop.
44
Questions
🞂 For the following Questions write a program
using break continue statement.
45
Break Statement
🞂 It uses the break keyword in the given
looping statements.
🞂 in C++ programming language it has the following
two usages.
• When the break statement is encountered inside a loop,
the loop is immediately terminated and the program
control resumes at the next statement following the
loop.
47
Questions
🞂 For the following Questions write a program
using break break statement.
48
goto Statement
used in many programming languages for moving back
and forth in the program.
using goto statement one needs to put in a label.
the jump is to be executed we simply code as
goto name_of_label as illustrated below.
statements;
Syntax: goto name_of_label ; //code at point of
jump
----------------
statements;
name_of_label : //code at destination of jump
-----------------
statements;
49
Cont’d …
At the destination, the label name is put at the start
of line and it has to be followed by colon ( : ).
51
Nested loops
A repetition structure that contains one or more loops
in its body.
In every repetition of the outer loop the inner loops
are completely executed.
A loop contained in another loop forms a
doubly nested loop.
A doubly nested loop can be placed inside another
loop to form triply nested loop, and so on.
In C++ a nested loop structure may be formed using
while, for, and do-while statements.
52
Example:- multiplication tables
53
Exercise
1. In the switch statement, which types can
expression
evaluate to? char, byte, short, int
55