You are on page 1of 41

Control Structures in C++

PROGRAM CONTROL STRUCTURES

 The program control structures are the different ways


in which the nature of a program can be designed or
modeled with respect to control the execution of
program code or instructions.
 A programmer can choose any of the programming

language and design the program in any of the three


main types of control structures, namely: sequence,
selection and loop or iteration program control
structures
Sequence
 The program statements are executed one after the
other in the order in which they appear or occur
 Statement 1
 Statement 2
 …
 …
 Statement n
SELECTION CONTROL STRUCTURE

 This a type of control structure in which


alternative execution paths radiate from a
decision (branching) point.
 Program statements() are executed based

on whether a condition evaluate to a true


or false or whether the condition has been
met or not
 It is done using If.. End if and Select..

End Select
If … End if
 There are three ways of
implementing if
 Single If
 Double If
 Multiple if
Single if
 Syntax
 If (condition)
{
 Statements
}
 End if
Example
 If ( Age >= 18 )
{
 cout << “ Adult”;
}
 End if
IF-ELSE STATEMENT (Double if)

 The general structure of the if-else control structures is:


 if (expression is true)
 {
 statement 1;
 }
 else
 {
 statement 2;
 }
 Explanation: If the expression is true, then statement

1 is executed, otherwise the statement 2 is executed


when the expression is found to be false.
 EXAMPLE
Int age;
Cout << “Enter your age”;
Cin >> age;
If (age >= 18)
{
Cout << “ Adult”;
}
Else
{
Cout << “Child”;
}
 Multiple if
 If (condition 1)
 {
 Statement
 }
 Else if (condition 2)
 {
 Statement
 }
 Else if (condition 3)
 {
 statement
 }
 Else
 {
 Statement
 }
The if...else if...else Statement:

 An if statement can be followed by an optional else


if...else statement, which is very useful to test various
conditions using single if...else if statement.
 When using if, else if , else statements there are few

points to keep in mind.


 An if can have zero or one else's and it must come after

any else if's.


 An if can have zero to many else if's and they must

come before the else.


 Once an else if succeeds, none of the remaining else if's

or else's will be tested.


 Example
 Int marks;
 Cout << “Enter marks”;
 Cin >> marks;
 If (marks < 40)
 {
 Cout “F”;
 }
 Else if (marks >=40 && marks < 50)
 {
 Cout << “D”;
 }
 Else if ( marks >=50 && marks <60)
 {
 Cout << “C”;
 }
 Else if ( marks >=60 && marks <70)
 {
 Cout << “B”;
 }
 Else
 {
 Cout << “A”;

Exercise
 Usemultiple if to write a program that
gives the following output

 Age 0 to <14 Child


 Age 14 to <18 Teen
 Age 18 to <35 Youth
 Age 35 ad above Adult
SWITCH STATEMENT

 The switch statement is applied in a case


where the if-else statement may end up
being too deeply nested. The switch
statements make it easy to branch on any
of a number of different values
 Syntax
 switch (expression)
 {
 case 1:
 {
 statement 1;
 }
 case 2:
 {
 statement 2;
 }
 .
 .
 .
 default:
 {
 statement m;
 }
 statement n;
 The switch statement evaluates the expression
and compares the result to each of the case
values. This evaluation is only for equality;
otherwise neither relational operators nor
Boolean expressions can be used.
 If any of the case values is found to match the

expression, the execution jumps to those


statements and continues up to the end of the
switch block unless a break statement is
encountered. If there is no case value that
matches the expression, the execution branches to
the optional default statement.
 // This program demonstrates the Switch control structure
 #include<iostream.h>
 int main ( )
 {
 switch (x)
 case 1:
 cout << “x is 1”;
 break;
 case 2:
 cout << “x is 2”;
 break;
 default:
 cout << “ value of x unknown”;
 return 0;
 }
 Further Example:
 #include <iostream>
 using namespace std;
 int main ()
 {
 // local variable declaration:
 char grade = 'D';
 switch(grade)
 {
 case 'A' :
 cout << "Excellent!" << endl;
 break;
 case 'B' :
 case 'C' :
 cout << "Well done" << endl;
 break;
 case 'D' :
 cout << "You passed" << endl;
 break;
 case 'F' :
 cout << "Better try again" << endl;
 break;
 default :
 cout << "Invalid grade" << endl;
 }
 cout << "Your grade is " << grade << endl;
 return 0;
Exercise
 Write a program takes in students marks ad return the
student grade based on this chart
 0 and less than 40 F
 40 and less than 50 D
 50 and less than 60 C
 60 and less than 70 B
 70 and above A
LOOP STRUCTURE

 A loop is a program control structure that allows for the


execution to follow conditional structures, repetitive
structures or bifurcation of controls. Loops can be
nested in such a way that one loop sits in the body of
another. For every execution of the outer loop, the
inner loop is executed in full. The loop control
structure is as illustrated below
 Examples of loop control structures include:
 do-while loop,
 while loop,
 for loop,
 bifurcation or jump controls namely: the break

instruction, the continue instruction and the goto


instruction
DO-WHILE CONTROL STRUCTURE

 The do-while control structure is an exit-controlled


loop structure. The do-while loop executes the body of
the loop before its condition is tested, and ensures that
the body always executes at least one time. The do-
while loop structure takes the form
 do
{
 statement 1;
}
 while (condition is true)
 Example
 intx, y;
 y =2;
 x= 1;
 do
{
 cout << y << “ ”;
 cout << endl;
 y +=2;
 x= x+1;
}
 while (x <=5)
 The output is 2 4 6 8 10
 Exr1
 97531
 Exr 2
 3
 9
 81
 6561
 Exr 3
 256
 64
 16
 4
 1
 int x;
 Int y;
 X=1;
 Y =9;
 For ( x= 1; X<=5; X=x+1)
 {
 Cout << y << “ ”;
 Y = y-2;
 X=x+1;
 }
 While (x<=5)
 {
 cout << y << “ ”;
 Y = y-2;
 X=x+1;
 }
WHILE CONTROL STRUCTURE

 The while control structure, is an entry-controlled loop


structure that checks its condition before executing any of its
statements. If the condition evaluates false, the entire body is
jumped. The while control structure takes the following

 while (condition is true)


{
 statement 1;
}



 Example: while loop
 // A program for custom countdown using the while loop
 #include<iostream.h>
 int main ( )
 {
 int n;
 cout << “Enter the starting number:”;
 cin >> n;
 while (n > 0)
 {
 Cout << n << “ , ”;
 – – n;
 }
 return 0;
 }
 The output of the above program is:-
 Enter the starting number (e.g. 10)
 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
 Another Example
 intx, y ;
 x=1;
 y= 2;
 While ( x<=5)
 {
 Cout << y << “ ”;
 Y +=2;
 x= x+1;
 }
 Output is 2 4 6 8 10
 Comparing do.. While and While condition
 Syntax
 Do while(condition)
 { {
 statements ; statements;
 } }
 while (condition);
 Example using do -- while(condition)

int x, y;
x= 1;
y = 2;
do
{
cout << y << “ ”;
y =y+2;
x+=1;
}
Example using while (condition)
int x, y;
x= 1;
y = 2;
while(x<=5)
{
cout << y << “ ”;
y =y+2;
x+=1;
}
FOR CONTROL STRUCTURE

For ( start value; test value; increment)


{
Statement(s);
}
Examples
For ( x=1; x<=5; x=x +1) 1,2,3,4,5
For ( x= 10; x>=6; x=x-1) 10,9,8,7,6
For ( x= 7; x<=14; x =x+2) 7,9,11,13
FOR CONTROL STRUCTURE

 The for control structure is an entry-controlled loop


structure and is used when an action is to be repeated
for a pre-determined number of times. The structure
takes the format:
 for (initial value; test value; increment)
 {
 statement 1;
 }
 A for loop follows the following sequence: It performs

the operations in the initialization, Evaluates the


condition, If the condition is true, it executes the
statement and the loop. The statements are then
executed repeatedly until when the condition is found
to evaluate false.

Example
 int x;
 for ( x=2; x<=10, x+=2)
 {
 cout << x << “ ”;
 int x , y;
 y = 2;
 For (x=1; x<=5; x+=1)
 {
 cout << y << endl;
 y +=2;
 }
 int x, y;
 y =3;
 For (x=1; x<=5; x=x+1)
 {
 cout << y << endl;
 y=y*y
 }

Output is as shown below


3
9
81
6561
Exercise try using Nested For …
 4 1
 4 2
 4 3
 4 4
Exercise try using For…
 256
 64
 16
 4
 1
Solution
1. int x, y;
2. y = 256;
3. For ( x = 10; x>=2; x-=2)
4. {
5. cout << y <<endl;
6. y= y/4;
7. }
 Example: for loop
 // A program for custom count up using the for loop
 #include<iostream.h>
 int main ( )
 {
 int n;
 for (n < 0; n = 10; n ++ )
 {
 cout << n << “ , ”;
 return 0;
 }
 The output of the above program is:-
 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
 Question: Consider and explain the following for
statement
BIFURCATION STRUCTURES

 These are mainly the jump or skip control


structures that are used to alter the normal
or sequential execution of the program
instructions. Mainly such controls are
effected in a program using the break
instruction, continue instruction and the
goto instruction
 THE BREAK INSTRUCTION
 // A program for custom countdown using break instruction
 #include<iostream.h>
 int main ( )
 {
 int n;
 for (n = 10; n > 0; n – – )
 {
 cout << n << “ , ”;
 if (n = = 3)
 {
 cout << Countdown Aborted !”;
 break;
 }
 }
 return 0;
 }
 The output of the above, jump program is:-
 10, 9, 8, 7, 6, 5, 4
 THE CONTINUE INSTRUCTION
 // A program for custom countdown using continue instruction
 #include<iostream.h>
 int main ( )
 {
 for (int n = 10; n > 0; n – – )
 {
 if (n == 5)
 continue;
 cout << n << “ , ”;
 }
 cout << “FIRE !”;
 return 0;
 }
 The output of the above, continue program is:-
 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
 FIRE !
 THE CONTINUE INSTRUCTION
 // A program for custom countdown using goto instruction
 #include<iostream.h>
 int main ( )
 {
 int n = 10;
 loop:
 cout << n << “ , ”;
 n––;
 {
 if (n > 0 )
 goto loop;
 }
 cout << “FIRE !”;
 return 0;
 }
 The output of the above goto program is:-
 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

You might also like