0% found this document useful (0 votes)
25 views

C++ Chapter Three

Chapter 3 is also about computers programming Discribin about control statements.

Uploaded by

abdulhakimhedo6
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

C++ Chapter Three

Chapter 3 is also about computers programming Discribin about control statements.

Uploaded by

abdulhakimhedo6
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

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.

🞂 ​ If expression evaluates to false, the first set of code after


the end of the if statement will be executed.
Cont’d …
• There are four types of if statements in C++. These
are
If Statements

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

• The statements will be


evaluated if the value of e
th condition is true.
F
i
g
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.

🞂 ​ Write a program to display “you are adult” when


age
is greater than 20.

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.

• If expression is false, statement2 is executed


and then next_statement is executed.
Cont’d …
🞂 ​ If
the boolean expression evaluates to true, then the if
block of code will be executed, otherwise else block
of code will be 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:

🞂 ​ Write a program that displays grade of a student


using fixed scale system.

🞂 ​ Write a code that accepts three integer values from


the user then it displays:
• the maximum integer number
• The minimum integer number

🞂 ​ Write a program to display days of a week using if


else if ladder.

18
Switch Statements

What is
switch?

• allows a variable to be tested for equality against a list


of
values.
• Each value is called a case, and the variable
being switched on is checked for each case.
• You can have any number of case statements.
19
Cont’d …
• The switch statement enables you to test several
cases generated by a given expression.
• allows a variable to be tested for equality against a
list of
values.
• Each value is called a case, and the variable being
switched on is checked for each case.
• You can have any number of case statements.

🞂​ 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

 Write a program to perform the arithmetic operations


using switch

 Write a program that display the name of the


day using a day value.

 Write the above program when input is from a


user.

23
Iteration Statements
What is Iteration
Statements?

 Also known as a looping statements.


 to execute a block of code several number of
times
 It allows to you to execute a statement or block
of statements repeatedly.
24
Cont’d …
• Executes a block of statements when a
particular condition is true

Advantage of looping statement


🞂​ Reduce length of Code

🞂​ Take less memory space.

🞂​ Burden on the developer is reducing.

🞂​ Time consuming process to execute the


program is
reduced.

25
Cont’d …
🞂 ​ There are three types of loops in C+
+:
 for loops
 while loops
 do-while loops

• Conditional statement executes only once where as


looping statements executes repeatedly several
number of time.

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);
}

How for loop works:

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.

🞂 ​ Next, the condition is evaluated.


• If it is true, the body of the loop is executed.
• If it is false, the body of the loop does not execute and
flow of control jumps to the next statement

🞂 ​ 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.

🞂​A for loop is useful


when you know how
many times a task is to
be repeated.

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.

 Display sum of numbers between 200 and


300

 Display all even numbers between 400 and


600.

 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.

🞂 ​ In the while, the program tests the loop-continuation


condition at the beginning of the loop, before
executing the loop’s body; if the condition is false, the
body never executes.

🞂 ​ The do…while statement tests the loop-


continuation condition after executing the loop’s
body.

🞂​ therefore, the body always executes at least once. When


a do…while statement terminates, execution continues
with the next statement in sequence.
39
Cont’d …
Example1
Output
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
40 of x : 19
Questions
🞂 ​ Write a C++ program for the following
Questions.

 Display sum of numbers between 300 and


100

 Display all even numbers between 600 and


300.

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.

🞂 ​ Performs continue running the loop, but stop


processing the remainder of the code in the body of
loop, to the loops end.
43
Cont’d …
Syntax: Example:

44
Questions
🞂 ​ For the following Questions write a program
using break continue statement.

A. Display all numbers between 200 and 300,


with out 167 and 187

B. Display numbers starting from 20 up to 120


but exclude all numbers divisible by 7.

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.

• It can be used to terminate a case in the switch


statement
(covered in the next chapter).
46
Cont’d …
Example:

47
Questions
🞂 ​ For the following Questions write a program
using break break statement.

A. Display all numbers between 200 and 300,


with out 167 and 187

B. Display numbers starting from 20 up to 120


but exclude all numbers divisible by 7.

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 ( : ).

 Any other statement is put on right side of colon or


in next line.

 In the following program we have put the label name


as Again. So at the point of jump the statement is
written as goto Again;

 At the destination it is Again: . The following program


illustrates the application of goto statement for
iteration.
50
Example

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

2. What must be used to separate each section of a


for statement. Semicolons

3. Which statement causes a program to skip to the


nextiteration of a loop. Continue

4. Write a for loop that outputs 100-1 in reverse


sequence.

5. Write a for loop that outputs all numbers that


are divisible by 3 between 0-50.
Question

55

You might also like