Chapter 3 - C++ Handout
Chapter 3 - C++ Handout
Control Statements
By: Sinodos G.
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.
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
Nested If
6
If Statement
• The if decision statement executes a statement only if
an expression is true
Syntax:
if (expression) {
statement1;
}
rest_of_program
7
Example 1:
int main(){
int x = 10;
if( x < 20 ) {
cout<<“The first C++ control program ";
getch();
}
Out put: The first C++ control program
Example 2:
int main(){
int a = 10, b=20;
if (a > b) {
cout<<”a is greater than b “;
getch();
Out put:
}
8
Exercise
Write a program to check a student is passed or not.
9
If-Else Statement
• If-else followed by an optional else statement, which
executes when the Boolean expression is false.
if (expression) {
statement1;
}
else{
statement2;
•
}
next_statement;
• If the expression is true, statement1 is executed and
then next_statement is executed.
Syntax:
if (expression){
statement1;
} else {
statement2;
}
rest_of_program
11
Example: if-else
int main(){
int x = 30;
if(x < 20) {
cout<<“This is if statement";
}else {
cout<<"This is else statement";
}
}
Exercise:
Write a program using if-else statement that displays DDIT
Student fixed grade system.
12
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{
Statement 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
default : // 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 = args[0].charAt(0); cout<<"You passed";
char grade = 'C'; case 'F' :
switch(grade) { cout<<"Better try again";
case 'A' : break;
cout<<"Excellent!"; default :
break; cout<<"Invalid grade";
case 'B' : }
case 'C' : Cout<<"Your grade is " << grade);
cout<<"Well done"; }
break; }
22
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.
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 999, 1+2+3+4+5+6+7
correctly labeling them even or odd + 8 + 9 + 10 = 55
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 : 19
36
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.
40
Questions
Write a C++ program for the following
Questions.
41
Jump Statements
Also known as a loop control statements
Continue Statement
Break Statement
Return Statement
42
Continue Statements
The continue 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.
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.
46
Cont’d …
Example:
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.
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 ( : ).
50
Example
51
Nested loops
A repetition structure that contains one or more loops in
its body.
53
Exercise
1. In the switch statement, which types can expression
evaluate to? char, byte, short, int
55