0% found this document useful (0 votes)
8 views10 pages

Lecture 5

The document discusses control structures in computer programming, specifically in C++, emphasizing their role in determining the order of instruction execution. It outlines three basic forms of control structures: sequential, selection, and iterative, with a focus on selection control statements such as 'if' and 'if-else'. Examples are provided to illustrate how these control structures function within a program.

Uploaded by

rugamwela
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)
8 views10 pages

Lecture 5

The document discusses control structures in computer programming, specifically in C++, emphasizing their role in determining the order of instruction execution. It outlines three basic forms of control structures: sequential, selection, and iterative, with a focus on selection control statements such as 'if' and 'if-else'. Examples are provided to illustrate how these control structures function within a program.

Uploaded by

rugamwela
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
You are on page 1/ 10

Computer Programming

IS121 – 2024/2025

Uledi Ngulo

Control Structures 1/2

April 23, 2025


Control Structures
Motivation
Fundamental means of controlling the order of
execution of instructions within a program
First electronic computers over sixty years ago were
referred to as “Electronic Brains”; misleading
There is no more intelligence a computer than
what it is instructed to do
What computers can do, however, is to execute a
series of instructions very quickly and very reliably.
It is the speed of execution that gives computers
their power. Of course and obedience too.

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;

Each suite can contain as many statements as required


IS121 – 2024/2025 6 / 10
Control Structures
Example 1: (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 =12;
6 i f ( c <=15)
7 {
8 c o u t <<”Number i s l e s s than
9 e q u a l t o 15 ”<< e n d l ;
10 }
11 return 0;
12 }

IS121 – 2024/2025 7 / 10
Control Structures

Example 1: (ExampleofCPP.cpp) Cont.


1 if (c <= 15) checks whether the value of variable
c is less than or equal to 15.
2 The expression evaluates to be true and as a result
statement cout << “Number is less then equal
to 15”<<endl; is executed.

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

Example 2: (ExampleofCPP.cpp) Cont.


1 if (c <= 15) checks whether the value of variable
c is less than or equal to 15.
2 The expression evaluates to be true and as a result
statement cout << “Number
c <= 15”<<endl; is executed.
3 The statement cout << “Number
c >= 15”<<endl; under else is not executed as
value of the variable c is not greater than 15.

IS121 – 2024/2025 10 / 10

You might also like