Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

CONTROL STRUCTURE

DR.MUMTAZ ZAHOOR
COURSE TITLE: PROGRAMMING FUNDAMENTALS
LECTURE NO 7
Overview
2

 Control structure & its types


 If statement
 If else statement
 Multiple If-else-If structure

Dr.Mumtaz Zahoor Programming Fundamentals


3

CONTROL STRUCTURE
Control Structure & its Types
4

 A statement used to control the flow of execution in


program is called control structure .
 There are 3 kind of control structure
 Sequence
 Selection
 Repetition

Dr.Mumtaz Zahoor Programming Fundamentals


Sequence
5

 In sequence the statement are executed in same


order in which they are specified in the program
 The control flow from one statement to other in a
logical sequence
 All statement are executes exactly once, It means no
statement is skipped and no statement is executed
more than once

Dr.Mumtaz Zahoor Programming Fundamentals


Selection
6

 A selection select a statement or set of statement to


execute on the basis of a condition .
 In this structure statement or set of statement is
executed when a particular condition is true and
ignored when the condition is false
 There are different type of selection structure In
C++
 These are if ,if….else ,and switch statements.

Dr.Mumtaz Zahoor Programming Fundamentals


Repetition
7

 A repletion structure execute a statement or set of


statement repeatedly.
 It is also is known as iteration structure or loop.
 There are different kind of loop:
 While
 Do while
 For

Dr.Mumtaz Zahoor Programming Fundamentals


8

IF STATEMENT
IF Statement & its Syntax
9

 The IF statement executes the statement(s) if the given


condition is true.
 But if the condition is false nothing happens.

 Syntax:
1. if( condition)
statement // statement will execute if the condition is true
2. If (condition)
{Statement 1
Statement 2
Statement n..}
Note: multiple statements are written in curly brackets.

Dr.Mumtaz Zahoor Programming Fundamentals


Flowchart
10

Dr.Mumtaz Zahoor Programming Fundamentals


Example & flowchart of IF Statement
11

#include <iostream>
using namespace std;
int main ()
{
int a = 10;
if( a < 20 )
cout << "a is less than 20“;
return 0;
}

Output
a is less than 20

Dr.Mumtaz Zahoor Programming Fundamentals


Flowchart of Examples…..
12

#include <iostream>
using namespace std;
int main ()
{
int marks;
cout<<“enter marks”;
cin>>marks;
if( marks >= 40 )
{
cout << “Congratulations\n“;
cout<<“You have passed”;
}
return 0;
}
Output
Congratulations
You have passed

Dr.Mumtaz Zahoor Programming Fundamentals


Example of Multiple IF Statement
13

Example 1 Example 2
#include <iostream> int main ()
using namespace std; {
int main () int n1, n2, n3, max;
cout<<“enter first number”;
{
cin>>n1;
int m; cout<<“enter second number”;
cout<<“enter number”; cin>>n2;
cin>>m; cout<<“enter third number”;
if( m< 0) cin>>n3;
cout << “m is negative number“; max=n1;
if( m>0) if(n2>max)
cout << “m is positive number“; max=n2;
If (n3>max)
if( m== 0)
max=n3;
cout << “m is zero“; cout<<“Maximum number is”<<max;
return 0; return 0;
} }

Dr.Mumtaz Zahoor Programming Fundamentals


Programming Practice
14

 Write a program that inputs three values and displays


minimum number.
 Write a program that inputs the marks of three subjects.
If the average is more then 80, it displays two messages
“You are above standard” and “admission granted”.

Dr.Mumtaz Zahoor Programming Fundamentals


15

IF ELSE STATEMENT

Dr.Mumtaz Zahoor Programming Fundamentals


IF Else Statement
16

 If else statement is another type of conditional


structure.
 it executes statement (s) when the condition is true
and other when it is false .
 In any situation , one block is executed and other is
skipped .
 Syntax:
1. if( condition)
statement
else
statement

Dr.Mumtaz Zahoor Programming Fundamentals


Syntax of IF Else Statement
17

If(condition)
{
// statement(s) will execute if the Boolean
expression is true
}
else
{
// statement(s) will execute if the Boolean expression
is false
}

Dr.Mumtaz Zahoor Programming Fundamentals


Flowchart
18

Dr.Mumtaz Zahoor Programming Fundamentals


Example & Flowchart of IF Else Statement
19

int main ()
{
int a = 100;
if( a < 20 )
cout << "a is less than 20" ;
else
cout << "a is not less than
20“ ;
return 0;
}

Dr.Mumtaz Zahoor Programming Fundamentals


Flowchart & Examples…..
20

int main ()
{
int m;
cout<<“enter number”;
cin>>m;
if( m%2==0)
cout << “m is even number“;
else
cout << “m is odd number“;
return 0;
}

Dr.Mumtaz Zahoor Programming Fundamentals


Programming Practice
21

 Write a program that inputs the year and find that it is


a leap year or not.
 Write a program that inputs the salary and pay scale .
If the pay scale is greater and equal to 15 it adds the
bonus of 20%, if the pay scale is less than 15 it adds the
bonus to 10%. And finally displays the net salary.

Dr.Mumtaz Zahoor Programming Fundamentals


22

MULTIPLE IF ELSE IF
STATEMENT
Multiple IF Else IF Structure
23

 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 he remaining else if's or else's
will be tested.

Dr.Mumtaz Zahoor Programming Fundamentals


Syntax of Multiple IF Else IF Statement
24

if(boolean_expression 1)
{
// Executes when the Boolean expression 1 is true
}
else if( boolean_expression 2)
{
// Executes when the Boolean expression 2 is true
}
else if( boolean_expression 3)
{
// Executes when the Boolean expression 3 is true
}
else
{
// executes when the none of the above condition is true.
}

Dr.Mumtaz Zahoor Programming Fundamentals


Example of Multiple IF Else IF Statement
25

Example 1 Example 2

int main () int main ()


{
{ int score;
int a = 100; cout<<“Enter Score”;
if( a == 10 ) cin>>score;
if( score>= 90 )
cout << "Value of a is 10"; cout << “A grade" ;
else if( a == 20 ) else if( score>=80 )
cout << "Value of a is 20“ ; cout << “B grade" ;
else if( score>= 70 )
else if( a == 30 ) cout << “C grade”;
cout << "Value of a is 30“ ; else if( score>=60 )
else cout << “D+ grade" ;
else if( score>= 50 )
cout << "Value of a is not matching“ ; cout << “D grade”;
return 0; else
} cout << “F grade“ ;
return 0;
}
Dr.Mumtaz Zahoor Programming Fundamentals
Flowchart of Example
26

Dr.Mumtaz Zahoor Programming Fundamentals


Programming Practice
27

 Write a program that inputs radius and choice. It calculates


the area of circle if user enters 1 as choice. It calculates
circumference of circle if user enters 2 as choice.
 Write a program that inputs the salary. If the salary is
20000 or more it deducts 7% of salary. If salary is 10000 or
more but less then 20000, it deducts 1000 from salary. If the
salary is less than 10000, it deducts nothing. It finally
displays the net salary.

Dr.Mumtaz Zahoor Programming Fundamentals


28

THANKS

You might also like