Lecture 5
Lecture 5
IS121 – 2024/2025
Uledi Ngulo
IS121 – 2024/2025 2 / 10
Control Structures
Control flow is the order that instructions are
executed in a program
A control statement is a statement that
determines the control flow of a set of instructions
C++ has 3 basic forms of control structures;
sequential, selection, and iterative control
IS121 – 2024/2025 3 / 10
Selection Control
C++ provides 3 selection control statements.
1. if Statement
SYNTAX
if (expression) header :
statements; clause
IS121 – 2024/2025 4 / 10
Selection Control
2. if else Statement
SYNTAX
if (expression)
statements;
else
statements;
2 headers:
2 clauses
IS121 – 2024/2025 5 / 10
Control Structures
if–else Indentation
if (expression) header 1
statement 1; suite/block 1
clause 1
statement 2;
statement 3;
else header 2
statement 1; suite/block 2
clause 2
statement 2;
statement 3;
IS121 – 2024/2025 7 / 10
Control Structures
IS121 – 2024/2025 8 / 10
Control Structures
Example 2: (ExampleofCPP.cpp)
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3
4 i n t main ( )
5 { i n t c =17;
6 i f ( c <=15)
7 {
8 c o u t <<”Number c <= 15 15 ”<< e n d l ;
9 }
10 else
11 {
12 c o u t <<”Number c >= 15 ”<< e n d l ;
13 }
14 return 0;
15 } IS121 – 2024/2025 9 / 10
Control Structures
IS121 – 2024/2025 10 / 10