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

Chapter 3

Chapter 3 of the Computer Science document 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). It explains how these statements allow for decision-making and control the flow of execution in programming. The chapter includes examples and syntax for each type of statement discussed.

Uploaded by

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

Chapter 3

Chapter 3 of the Computer Science document 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). It explains how these statements allow for decision-making and control the flow of execution in programming. The chapter includes examples and syntax for each type of statement discussed.

Uploaded by

su1401168
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Department of Computer Science

Chapter 3: Control Statements

April 15, 2026


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

4/15/2026 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

4/15/2026 3
4/15/2026 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.

4/15/2026 5
4/15/2026 6
Example of if statement

4/15/2026 7
Nested if statement
▪ When there is an if statement inside another if statement then it
is called the nested if statement

4/15/2026 8
Example of Nested if statement

4/15/2026 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.

4/15/2026 10
4/15/2026 11
Example of if-else statement

4/15/2026 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

4/15/2026 13
4/15/2026 14
Example of if-else-if

4/15/2026 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
4/15/2026 16
The syntax of Switch case statement

4/15/2026 17
4/15/2026 18
Repetition or Iteration/Looping statements
✓For loop statement
✓While loop statement
✓Do…while statement

4/15/2026 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.

4/15/2026 20
4/15/2026 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.

4/15/2026 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.

4/15/2026 23
Example

4/15/2026 24
While loop
▪ As discussed earlier, loops are used for executing a block of
program statements repeatedly until the given loop condition
returns false.

4/15/2026 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.

4/15/2026 26
4/15/2026 27
Example

4/15/2026 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.

4/15/2026 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.

4/15/2026 30
4/15/2026 31
Example

4/15/2026 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

4/15/2026 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.

4/15/2026 34
Example: continue statement inside for loop

4/15/2026 35
Flow Diagram of Continue Statement

4/15/2026 36
Example: Use of continue in While loop

4/15/2026 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.

4/15/2026 38
4/15/2026 39
Example: Use of break statement in a while
loop

4/15/2026 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
4/15/2026 41
Example of goto statement

4/15/2026 42
Thank You!

4/15/2026 43

You might also like