PLT 101 Computer Programming
CHAPTER 2
PROGRAM CONTROL STRUCTURES
(Part 1)
Selection/Conditional Statements
Lecturer: Mrs Nurul Izni Rusli
nurulizni@unimap.edu.my
UNICITI S2 BK5 Level 1
Previously in Chapter 1..
Computer Hardware & Software Components
Programming Language (Machine,
Assembly, High-Level)
Programming Algorithm
Pseudo-Code
Flow Chart
Variable/Identifiers & Reserved words
Program Comments
Preprocessor Directives or Headers
Data Types & Declaration
Program Operators
Program Debugging & Error Type (Syntax,
2
Runtime, Logic)
Example of C Reserved Words
Outline
Simple & Compound Statement
Types of Selection
One-way Selection
Two-way Selection
Multi-Selection
Nested If
Conditional Operator
Switch Structure
4
Simple Statements & Compound
Statements
Compound Statement a block of code enclosed with a
pair of braces { }
The initialization is performed for an initialization
statement each time the declaration is reached in the
order of execution.
Eg:
Compound (Block of ) Statement
A compound statement (also called a block of
statements) takes the form of
{
statement 1; statement 2;
.
.
.
statement n;
}
It is considered a single syntactic unit
6
Compound (Block of ) Statement -cont.
Example:
if (age > 18)
{
pintf ("Eligible to vote\n);
printf ("No longer a minor\n);
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
selection of program flow
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.
One-way Selection = if -cont.
Example 1
If students grade is greater than or equal to 60
Print Pass
11
One-way Selection = if -cont.
..
if (grade >= 60) {
printf (Pass);
}
..
..
12
One-way Selection = if -cont.
Example 2
char grade;
if (markah>= 90) {
grade = 'A';
...
printf (Grade is : %c\n, grade);
}
13
One-way Selection = if -cont.
Example 3
if (temperature is greater than 70 degree and it is not
raining)
recommended activity is golfing
bool rain=false;
if ((temp > 70) && !(rain)) {
printf (recommended activity is golfing);
}
14
One-way Selection = if -cont.
Common Errors
if score >= 90
//no parentheses
grade = 'A';
if (score >= 90); //; not here
grade = 'A';
15
Two-way Selection = if..else
The syntax of two-way selection is:
if (expression)
statement1;
else
statement2;
If the value of the expression is true, statement1 is
executed;
If false, statement2 is executed
16
Two-way Selection = if..else -cont.
Example 1
If students grade is greater than or equal to 60
print Pass
else
print Fail
17
Two-way Selection = if..else -cont.
if (grade >=60) {
printf (Pass);
}
else {
printf (Fail);
}
18
Two-way Selection = if..else -cont.
Example 2
if (hour > 40.0)
wages = 40.0 * rate +1.5 * rate * (hour - 40.0);
else
wages = hour * rate;
//Line 1
//Line 2
//Line 3
//Line 4
If hour is 50, then the statement at Line 2 is executed
If hour is 30, then the statement at Line 4 is executed
19
Multi-Selection = if..else..if
The syntax is:
if (exp1)
stmt1;
else if (exp2)
stmt2;
else if (exp3)
stmt3;
else
stmt n;
20
An if..else..if control structure
shifts program control, step by
step, through a series of
statement blocks.
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
Print very cold
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
if (temperature >= 50)
{
if (temperature >= 80)
printf ("Good day for swimming.\n);
else
printf ("Good day for golfing.\n);
}
else
printf ("Good day to play tennis.\n);
24
Nested if -cont.
Example 2
25
The Conditional Operator (? :)
The syntax of using the conditional operator is:
expression1 ? expression2 : expression3;
This is called a conditional expression.
The statement:
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
Switch Structure -cont.
The break statement has a special meaning
and may or may not appear after each
statement.
In C, switch, case, break, and default are
reserved words.
In a switch structure, first the expression is
evaluated. The value of the expression is then
used to perform the corresponding action.
28
Switch Structure -cont.
The expression is usually an identifier.
The value of the expression can be only
integral.
The expression is sometimes called the
selector. Its value determines which statement
is selected for execution.
A particular case value should appear only
once.
One or more statements may follow a case
29
label, so you do not need to use braces to turn
multiple statements into a single compound
statement.
Switch Structure -cont.
Example:
where, grade is a variable of the type char. If the
value of grade is, say 'A', the output is
The grade is A.
30
Switch Structure -cont.
The switch statement executes according to the
following rules:
When the value of the expression is matched against a case
value (also called a label), the statements execute until
either a break statement is found or the end of the switch
structure is reached.
If the value of the expression does not match any of the
case values, the statements following the default label
execute. If the switch structure has no default label, and if
the value of the expression does not match any of the case
values, the entire switch statement is skipped.
A break statement causes an immediate exit
switch structure.
31
from the
End Week 2 Session 1
Q & A!
3
2
Pop Quiz
Calculate score based upon a letter grade
Get input from user: A, B, C, D, F
Check for valid input
Print Score in a 4.0 to 0.0 scale
grade A > score = 4.0
grade B > score = 3.0
grade C > score = 2.0
grade D > score = 1.0
grade F > score = 0.0
33
Q1: Based on the program written below, draw a
complete flow chart for the program
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
Q2: Part A MTE 2014/2015
36
Q2 - Answer
37