0% found this document useful (0 votes)
4 views43 pages

Chapter 3

Chapter 3 of 'Fundamentals of Programming' covers control statements in C++, including selection statements (if, nested if, if-else, if-else-if, and switch), repetition statements (for, while, and do-while loops), and flow control statements (break, continue, and goto). Each type of statement is explained with examples to illustrate their usage and functionality in decision making and controlling the flow of execution in programs. The chapter emphasizes the importance of these statements in programming logic and structure.

Uploaded by

etsubd99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views43 pages

Chapter 3

Chapter 3 of 'Fundamentals of Programming' covers control statements in C++, including selection statements (if, nested if, if-else, if-else-if, and switch), repetition statements (for, while, and do-while loops), and flow control statements (break, continue, and goto). Each type of statement is explained with examples to illustrate their usage and functionality in decision making and controlling the flow of execution in programs. The chapter emphasizes the importance of these statements in programming logic and structure.

Uploaded by

etsubd99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Fundamental of programming

Chapter 3: Control Statements

May 22, 2025


Outline
 Selection Statements
 If statements
 Nested if statements
 If-else statement
 If… else if… else statement
 Switch statement
 Repetition or Iteration/Looping statements
 For loop statement
 While loop statement
 Do…while statement
 Flow control statement
 Break statement
 Continue statement
 Goto statement

05/22/2025 2
Introduction
 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++, we have four types of selection
statements, which are as follows:
 if statement
 nested if statement
 if-else statement
 if-else-if statement

05/22/2025 3
05/22/2025 4
If statement
 If statement consists a condition, followed by statement or a set
of statements as shown below:

 The statements inside if parenthesis (usually referred as if


body) gets executed only when the given condition is true. If the
condition is false then the statements inside if body are
completely ignored.

05/22/2025 5
05/22/2025 6
Example of if statement

05/22/2025 7
Nested if statement
 When there is an if statement inside another if statement then it
is called the nested if statement

05/22/2025 8
Example of Nested if statement

05/22/2025 9
If else statement
 Sometimes you have a condition and you want to execute a
block of code if condition is true and execute another piece of
code if the same condition is false. This can be achieved in C++
using if-else statement.

05/22/2025 10
05/22/2025 11
Example of if-else statement

05/22/2025 12
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” and
one “else”, however we can have multiple “else if” blocks.
 Look the next slide

05/22/2025 13
05/22/2025 14
Example of if-else-if

05/22/2025 15
Switch Case statement
 Switch case statement is used when we have multiple conditions and we
need to perform different action based on the condition.
 When we have multiple conditions and we need to execute a block of
statements when a particular condition is satisfied. In such case either we
can use lengthy if…else-if statement or switch case.
 The problem with lengthy if…else-if is that it becomes complex when we
have several conditions.
 Switch Case statement is mostly used with break statement
even though the break statement is optional. We will first see an
example without break statement and then we will discuss
switch case with break

05/22/2025 16
The syntax of Switch case statement

05/22/2025 17
05/22/2025 18
Repetition or Iteration/Looping statements
For loop statement
While loop statement
Do…while statement

05/22/2025 19
For loop
 A loop is used for executing a block of statements repeatedly
until a particular condition is satisfied.
 For example, when you are displaying number from 1 to 100
you may want set the value of a variable to 1 and display it 100
times, increasing its value by 1 on each loop iteration.

05/22/2025 20
05/22/2025 21
Flow of Execution of the for Loop
 As a program executes, the interpreter always keeps track of
which statement is about to be executed. We call this the
control flow, or the flow of execution of the program.

05/22/2025 22
Steps
 First step: In for loop, initialization happens first and only once, which
means that the initialization part of for loop only executes once.
 Second step: Condition in for loop is evaluated on each loop iteration, if the
condition is true then the statements inside for for loop body gets executed.
Once the condition returns false, the statements in for loop does not execute
and the control gets transferred to the next statement in the program after
for loop.
 Third step: After every execution of for loop’s body, the
increment/decrement part of for loop executes that updates the loop counter.
 Fourth step: After third step, the control jumps to second step and condition
is re-evaluated
 The steps from second to fourth repeats until the loop condition returns false.

05/22/2025 23
Example

05/22/2025 24
While loop
 As discussed earlier, loops are used for executing a block of
program statements repeatedly until the given loop condition
returns false.

05/22/2025 25
How while Loop works?
 In while loop, condition is evaluated first and if it returns true
then the statements inside while loop execute, this happens
repeatedly until the condition returns false. When condition
returns false, the control comes out of loop and jumps to the
next statement in the program after while loop.

05/22/2025 26
05/22/2025 27
Example

05/22/2025 28
do-while loop
 do-while loop is similar to while loop, however there is a
difference between them:
 In while loop, condition is evaluated first and then the
statements inside loop body gets executed, on the other hand in
do-while loop, statements inside do-while gets executed first
and then the condition is evaluated.

05/22/2025 29
How do-while loop works?
 First, the statements inside loop execute and then the condition
gets evaluated, if the condition returns true then the control
jumps to the “do” for further repeated execution of it, this
happens repeatedly until the condition returns false. Once
condition returns false control jumps to the next statement in the
program after do-while.

05/22/2025 30
05/22/2025 31
Example

05/22/2025 32
Flow 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. C++
supports the following control statements.
 The continue statement
 The break statement
 The goto statement

05/22/2025 33
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.

05/22/2025 34
Example: continue statement inside for loop

05/22/2025 35
Flow Diagram of Continue Statement

05/22/2025 36
Example: Use of continue in While loop

05/22/2025 37
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 (see the example
below) execution. Whenever it is encountered in switch-case
block, the control comes out of the switch-case body.
05/22/2025 38
05/22/2025 39
Example: Use of break statement in a while loop

05/22/2025 40
go to 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
05/22/2025 41
Example of goto statement

05/22/2025 42
Thank You!

05/22/2025
? 43

You might also like