5-Lecture-FoP-Control Structures
5-Lecture-FoP-Control Structures
Dr Ayesha Zeb
Email: [email protected]
Lecture Contents
• Pseudocodes
• Flow Charts
• Control structures
– Selection structures
• If-else statements
• Switch case statement
• Conditional operator
Pseudocodes
Symbol Description
TERMINAL - To start or end a flowchart
INPUT / OUTPUT - Used with Read, Input, Print and other I/O commands.
PROCESSING - Used for operations done inside the computer. Such as
calculations, storing and moving of data.
DECISION - Used to ask a question in programming. Questions are Yes/No
format (Used with the If Statement).
• Pseudocode:
Read in n
Is n<0 ?
If yes, n is negative
Print “Negative”
If no, is n==0?
Print “Zero”
If no, n is “Positive”
End Branch
End Branch
Flowchart example
Variables Algorithm
Start
S= X+Y+Z
A=S/3
P=X*Y*Z
Print S,A,P
Stop
Flowchart example
X X>=0
F(x) = { -X X<0
Flowchart example
Variables Algorithm
Start
Flowchart Read X
NO YES
X>=0
F=-X F=X
Print F
Stop
Flowchart practice exercises
• Algorithm: Read
• Step 1: Start a, b, c
• Step 2: Read a, b, c
• Step 3: d sqrt ( b b − 4 a c ) d sqrt(b x b – 4 x a x c)
• Step 4: x1 (–b + d) / (2 x a)
• Step 5: x2 (–b – d) / (2 x a) x1 (–b + d) / (2 x a)
• Step 6: Print x1, x2
• Step 7: Stop X2 (–b – d) / (2 x a)
Print X1, X2
STOP
Writing Programs
• If
• Else
• Else if
Control Structures: If… Else If … Else
T
The if statement
F
The if statement
• Pseudocode
IF speed is greater than 120km/hr
THEN print “over-speeding”
Any
The if statement statement
that results in
True or False
Syntax Logical or
Relational
if (condition)
{ statement1;
statement 2; T
F
….
statement n;
}
The if statement
if (speed>120)
{cout<<"over speeding";
cout << "reduce speed now";
}
…
T
… F
…
If - else
• if is a single-selection statement which performs an
indicated action only when the condition is TRUE;
otherwise the action is skipped.
• Syntax:
if (condition)
{statement1; statement 2;…. statement n;
}
else
{statement1; statement 2;….statement n;
}
• Pseudocode
• IF number is greater than or
equal to 0, THEN print
“positive”
• ELSE print “negative”
If - else
if (number>=0)
cout<<"number is positive";
else
cout<<"number is negative";
If - else
if (x%2==0)
y += x/2;
else
y -= x/2;
Some notes
• If there is only one statement following the if, then the curly
braces are optional.
• If there are multiple statements, the braces are necessary.
• Same is true for the statements following the else.
Some notes
– E.g.
if (a<b)
b = a+1;
b = b + 10;
cout<<"print A"<<b;
• Syntax:
if (condition)
{statement1; statement 2;…. statement n;
}
else if (condition 2)
{statement1; statement 2;….statement n;
}
else
{statement1; statement 2;….statement n;
}
The else if keyword
if (x==0)
{cout << " x is ZERO" <<endl;}
else if (x>0)
{cout << " x is positive" <<endl;}
else
{cout << " x is negative" <<endl;}
The else if keyword
• Additional alternative control paths
• Conditions evaluated in order until one is met; inner statement
(or statements) are then executed
• If multiple conditions are true, only first condition is executed,
the remaining are ignored
Nested if statements
• An if statement within the
executable block of another if
statement
• Use?
• Used where several conditions
need to be met for the execution
of an action.
• Is this completely true? How?
Nested if statements
• Conditions are checked in order,
i.e. the inner condition is checked
only if the outer condition is
satisfied.
– if condition is true -> inner condition
is checked
– if inner condition is true -> action is
done
Nested if statements
• Syntax:
if (condition)
{if (condition)
statement1;//(…. to n)
else
statement;
}
else
{statement1;statement 2;….
statement n;
}
Nested if statements
• Determine whether the number is
positive even, positive odd, or
zero.
• Pseudocode
IF number is greater than 0,
IF number is divisible by 2,
THEN print “positive even”
ELSE print “positive odd”
ELSE print “zero”
Nested if statements
if (num>0)
{if ((num%2)==0)
cout << "positive even" <<
endl;
else
cout << "positive odd" <<
endl;
}
else
cout << "zero" << endl;
Nested if statements
1. if (x%4 == 0)
{if (x%2 == 0)
y= 2;
To associate else with outer if statement: use
}
braces
else
y= 1;
2. if (x < 0)
if (y != 4)
z = y * x;
else
z = y / x;
else if (y > 4)
z = y + x;
else
z = y - x;
Pseudocode Example
START “murder of Jamal Khan”
“Solve murder of Jamal Khan”
DO WHILE there are suspects
IF Kashif’s fingerprints are on the sword
THEN Identify Kashif as the murderer
ELSE
Question Sarfraz
IF Sarfraz has an alibi THEN
Grill Asif
Give Miss Rifat the third degree
Identify fingerprints on sword
ENDIF
ENDIF
Arrest murderer
END DO
END “murder of Jamal Khan”
TRY ME
Here, each if condition will Here, the compiler will skip the
be checked even if the true remaining if conditions following the
one is found earlier on. true one.
Control Structures: If… Else If … Else
#include <iostream> else if ((x> y) && (x < z))
#include <string> {
using namespace std; cout << "x is greater
than y and less than z";
int main() { }
int x = 5; else {
int y = 10; cout << "x is greater
int z = 3; than y and z";
#include <iostream>
using namespace std;
int main(){
int x,y; char op;
cout << "Enter two integers: "; cin >> x >> y;
cout << "Enter an operator [+,-,*,/,%]: "; cin >> op;
switch (op){
case '+': cout << x + y << endl; break;
case '-': cout << x - y << endl; break;
case '*': cout << x * y << endl; break;
case '/': cout << x / y << endl; break;
case '%': cout << x % y << endl; break;
default: cout <<"Invalid Operator \n";
} return 0; }
Decision: Using the ? : statement
// demonstrates ? :
#include <iostream>
using namespace std;
int main(){
int m,n;
cout << "Enter two integers: ";
cin >> m >> n;
cout << ( m < n ? m : n ) << " is the minimum. \n";
return 0;
}// The conditional expression ( m<n ? m : n ) evaluates
// to m if m < n, and to n otherwise.
Acknowledgement/References
• Slides contents taken from Ma’am Fatima Faruq