Program Control Structures - Part 1
Program Control Structures - Part 1
CHAPTER 2
PROGRAM CONTROL STRUCTURES
(Part 1)
Selection/Conditional Statements
Lecturer: Mrs Nurul Izni Rusli
nurulizni@unimap.edu.my
UNICITI S2 BK5 Level 1
Programming Algorithm
Pseudo-Code
Flow Chart
Runtime, Logic)
Outline
Simple & Compound Statement
Types of Selection
One-way Selection
Two-way Selection
Multi-Selection
Nested If
Conditional Operator
Switch Structure
4
pair of braces { }
The initialization is performed for an initialization
Block of statement 1
}
else
{
printf ("Not eligible to vote\n);
printf (Still a minor\n);
7
Block of statement 2
Selection/Conditional Statements
Used to control the flow of a program
Also called as decision or branches
Branches are conditions or choices used to enable
Types of Selection
One-way selection = if
Two-way selection = if..else
Multi-selection
Nested if
Switch structure = switch
One-way Selection = if
In C, a condition is represented by a logical
10
(Boolean) expression
True and false are logical (Boolean) values
The syntax of one-way selection is:
if (expression) statement;
If the value of the expression is true, statement is
executed;
If false, statement is not executed and the
computer goes on to the next statement in the
program.
Print Pass
11
..
if (grade >= 60) {
printf (Pass);
}
..
..
12
char grade;
if (markah>= 90) {
grade = 'A';
...
printf (Grade is : %c\n, grade);
}
13
raining)
recommended activity is golfing
bool rain=false;
//no parentheses
grade = 'A';
if (score >= 90); //; not here
grade = 'A';
15
statement1;
else
statement2;
If the value of the expression is true, statement1 is
executed;
If false, statement2 is executed
16
17
if (grade >=60) {
printf (Pass);
}
else {
printf (Fail);
}
18
//Line 1
//Line 2
//Line 3
//Line 4
19
Multi-Selection = if..else..if
The syntax is:
if (exp1)
stmt1;
else if (exp2)
stmt2;
else if (exp3)
stmt3;
else
stmt n;
20
Multi-Selection = if..else..if
-cont.
Example
temp >30
temp
>30
display
0c
hot
20-30 0c mild
10-20
0c
<10 0c
cold
very cold
Print hot
false
true
Print mild
temp > 20
false
temp >10
false
21
true
true
Print cold
Multi-Selection = if..else..if
if (temp > 30)
printf (hot\n);
else if ((temp >=20) && (temp<=30))
printf (mild\n);
else if (temp >=10) && (temp < 20))
printf (cold\n);
else
printf (very cold\n);
22
-cont.
Nested if
When one control statement is within another, it is
said to be nested
if(exp1)
if(exp2)
statement1; OR
if(exp1)
{
statement1;
if(exp2)
statement2;
}
23
Nested if -cont.
Example 1
Nested if -cont.
Example 2
25
if (a >= b)
max = a;
else
max = b;
Is equivalent to the statement:
max = (a >= b) ? a : b;
26
Switch Structure
Similar to if-else if control structure
The general form (syntax):
switch (expression)
{
case value 1: statements1;
break;
case value 2: statements2;
break;
.
.
.
case value n: statements n;
break;
default: statements;
27
reserved words.
In a switch structure, first the expression is
28
integral.
The expression is sometimes called the
once.
One or more statements may follow a case
29
following rules:
When the value of the expression is matched against a case
switch structure.
31
from the
3
2
Pop Quiz
Calculate score based upon a letter grade
Get input from user: A, B, C, D, F
33
34
1 mark
Q1
- Answer
1 mark
Correct arrow & label 1 mark
1 mark
Y
N
1 mark
Y
N
1 mark
1 mark
Y
N
1 mark
Y
N
1 mark
35
1 mark
36
Q2 - Answer
37