Chapter 4

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

Chapter Four

Control Statements

1
Selection Statements
 Sometimes we need to execute a block of statements only when a particular
condition is met or not met. This is called decision making, as we are executing a
certain code after making a decision in the program logic.
 For decision making in C++, there are five types of selection statements:
 if statement
 nested if statement
 if-else statement
 if-else-if statement
 Switch statements

2
If statement
 The general form of a simple if statement is

 The reserved word if begins the if statement.


 The Boolean expression condition determines whether or not the body will be
executed.
 The Boolean expression must be enclosed within parentheses as shown.
 The statement is the statement to be executed if the Boolean expression is true. The
statement makes up the body of the if statement.

3
• Flow diagram of if statement

4
Example of if statement

5
Nested if statement
When there is an if statement inside another if statement then it is called the nested if statement

6
Example of Nested if statement

7
If else statement
The general form of an if/else statement is:

• The reserved word if begins the if/else statement.


• The condition is a Boolean expression that determines whether the
running program will execute statement 1 or statement 2.
8
If else statement
• The program executes statement 1 if the condition is true. This part of the if
statement is called the body of the if.
• The reserved word else begins the second part of the if/else statement. The
program executes statement 2 if the condition is false. This part of the else
statement is called the body of the else.
• Flow diagram of if-else

9
Example of if-else statement

10
if-else-if Statement
 if-else-if statement is used when we need to check multiple conditions.

 In this control structure, we have only one “if” , one “else”, and multiple “else if” blocks.

11
Example of if-else-if

12
The switch Statement
 Just like the else-if chain, the switch statement allows you to choose
between multiple alternatives. The switch statement compares the
value of one expression with multiple constants.

13
The switch Statement
 If the value of an expression matches one of the case constants, the
program branches to the appropriate case label. The program then
continues and the case labels lose their significance.
 You can use break to leave the switch statement unconditionally. The
statement is necessary to avoid executing the statements contained in
any case labels that follow.
 If the value of the expression does not match any of the case constants,
the program branches to the default label, if available. If you do not
define a default label, nothing happens. The default does not need to
be the last label; it can be followed by additional case labels.

14
Example of the switch statement

15
Iteration/Looping statements
Loops are used to execute a block of code more than once
Types of loops
 For loop statement
 While loop statement
 Do…while statement

16
for loop
 A for loop has the following form:
 Syntax: for( expression1; expression2; expression3 )
statement
 expression1 is used to initialize the loop.
 expression2 is the controlling expression, which is always evaluated
prior to executing the loop body:
 if expression2 is false, the loop is terminated
 if expression2 is true, the loop body is executed.
 Subsequently, the loop is reinitialized by executing expression3 and
expression2 is re-tested.
17
for loop

18
Example

19
while loop
 The while statement takes the following format:
 Syntax: while( condition)
statement // loop body
 When entering the loop, the condition is verified, i.e. the expression is
evaluated. If this value is true, the loop body is then executed before the
controlling expression is evaluated once more.
 If the condition is false, i.e. expression evaluates to false, the program
goes on to execute the statement following the while loop.
 It is common practice to place the loop body in a new line of the source
code and to indent the statement to improve the readability of the program.
20
21
Example

22
do-while loop
 In contrast to while and for loops, which are controlled by their
headers, the do while loop is controlled by its footer,
 i.e. the controlling expression is evaluated after executing the first
loop. This results in the loop body being performed at least once.
 Syntax: do{
statements ;
}while( expression);
 When a do-while loop is executed, the loop body is processed first.
Only then is the controlling expression evaluated
 The loop body is iterated again if the result is true, otherwise the loop
23 is terminated
24
Example

25
Flow control statements
 Flow control statements change execution from its normal sequence.

 When execution leaves a scope, all automatic objects that were created in
that scope are destroyed.
 C++ supports the following control statements.
 The continue statement

 The break statement

 The goto statement


26
The continue statement
 Continue statement is used inside loops.
 Whenever a continue statement is encountered inside a loop, control directly
jumps to the beginning of the loop for next iteration, skipping the execution of
statements inside loop’s body for the current iteration.

27
Example: continue statement inside for loop

28
Flow Diagram of Continue Statement

29
Example: Continue in While loop

30
Break statement
The break statement is used in following two scenarios:
1. Use break statement to come out of the loop instantly. Whenever a break
statement is encountered inside a loop, the control directly comes out of loop
terminating it. It is used along with if statement, whenever used inside loop(see
the example below) so that it occurs only for a particular condition.
2. It is used in switch case control structure after the case blocks. Generally all
cases in switch case are followed by a break statement to avoid the subsequent
cases execution. Whenever it is encountered in switch-case block, the control
comes out of the switch-case body.

31
32
Example: break statement in a while loop

33
goto statement
• The goto statement is used for transferring the control of a program to a given label.
• Syntax:

• In a program, we have any number of goto and label statements, the goto
statement is followed by a label name, whenever goto statement is
encountered, the control of the program jumps to the label specified in the
goto statement.
34
Example of goto statement

35

You might also like