Programming Principles and Techniques - Session 4
Programming Principles and Techniques - Session 4
nl
O
se
U
tre
en
C
h
ec
pt
Session 4 - Selection
rA
Constructs
Fo
y
nl
O
Explain IF statement
se
Explain IF…ELSE selection construct
U
Explain multiple selection statements
tre
en
Explain nested IF…ELSE statements
C
Explain case construct
h
ec
pt
rA
Fo
y
nl
O
A programmer may come
se
Start
across a condition in the Step Step
U
program, where the path of
tre
execution can branch into two Step Step Step
en
or more options.
C Step Step Stop
h
Such constructs are referred
ec
Step
to as programming, selection,
pt
conditional, or branching
rA
Step
constructs.
Fo
Step Step
y
nl
O
The IF construct is a basic selection construct.
se
Consider an example where the customer is given a
U
discount if purchases of over $100 are made.
tre
Each time a customer is billed, a part of the code
en
has to check to see if the bill amount exceeds $100.
C
If it does exceed the amount, then it must deduct
h
10% of the total amount, otherwise nothing must
ec
be deducted.
pt
rA
Fo
y
nl
O
The pseudocode for the scenario will be as
se
follows:
U
tre
IF customer purchases items worth more than $100
Give 10% discount
en
C
h
ec
pt
rA
Fo
y
nl
O
The general form of an IF statement or construct
se
is as follows:
U
tre
IF condition
en
Statements
CBody of the IF Construct
h
END IF
ec
pt
rA
Fo
y
nl
O
The example uses the IF construct to find
se
whether a number is even or not.
U
tre
BEGIN
en
INPUT number
C
rem = number MOD 2
h
IF rem=0
ec
END IF
rA
END
Fo
y
nl
O
A flowchart for the
se
pseudocode is shown in
U
the figure.
tre
en
C
h
ec
pt
rA
Fo
y
nl
O
The syntax for the IF statement in C language
se
is as follows:
U
tre
if (condition)
en
{
Statements; C
h
ec
}
pt
rA
Fo
y
nl
O
The example shows the pseudocode that would be written in
C.
se
U
/* A C program using the IF construct */
#include <stdio.h>
tre
void main ()
en
{
int number, rem;
C
printf (“Please enter a number: ”);
h
scanf (“%d”, &number);
ec
rem=number%2;
pt
if(rem==0)
{
rA
printf(“Even Number”);
Fo
}
}
y
nl
O
The IF…ELSE statement enables a programmer to make a
single comparison, and then execute the steps depending
se
on whether the result of the comparison is true or false.
U
tre
The general form of the IF…ELSE statement is as follows:
en
IF condition
Statement set1 C
h
ELSE
ec
Statement set2
pt
END IF
rA
Fo
y
nl
O
The syntax for the IF…ELSE construct in C
se
language is given as follows:
U
tre
if(condition)
en
{
statement set1;
} C
h
ec
else
pt
{
rA
statement set2;
}
Fo
y
nl
O
A more efficient code for the even number using the
IF…ELSE statement is shown in the following
se
example.
U
tre
BEGIN
INPUT number
en
rem=number MOD 2
IF rem=0 C
h
DISPLAY “Even Number”
ec
ELSE
pt
END IF
END
Fo
y
nl
O
The flowchart for the pseudocode is shown:
se
U
tre
en
C
h
ec
pt
rA
Fo
y
nl
O
The AND statement can be used in conjunction with
se
the IF statement for more than one condition.
U
To classify a supplier as a Most Valuable Supplier
tre
(MVS), the organization must check that the supplier
en
has been with them for the last 10 years.
C
h
And has done a total business of more than $500000.
ec
pt
rA
se
BEGIN
U
INPUT YearsWithUs
tre
INPUT BizDone
IF YearsWithUs >= 10 AND BizDone >= 500000
en
DISPLAY “Classified as an MVS”
ELSE
C
DISPLAY “A little more effort required”
h
ec
END IF
END
pt
rA
Fo
y
nl
O
The example shows the pseudocode that
se
would be written in C.
U
tre
/* C snippet depicting the AND operator in IF */
if(YearsWithUs >= 10 && BizDone >= 500000)
en
{
C
printf(“Classified as an MVS”);
h
}
ec
else
pt
{
printf(“A little more effort required”);
rA
}
Fo
y
nl
O
Another way to combine two conditions
se
without using the AND operator, is by using
U
nested IF…ELSE statements.
tre
en
A nested IF is an IF statement written inside
C
h
another IF statement.
ec
pt
rA
Fo
y
nl
O
Consider the earlier example to recognize the MVS status of a supplier
rewritten using nested IF.
se
BEGIN
U
INPUT YearsWithUS
tre
INPUT BizDone
IF YearsWithUs >= 10
en
IF BizDone >= 500000
DISPLAY “Classified as an MVS”
ELSE C
h
DISPLAY “A little more effort required”
ec
END IF
ELSE
pt
END IF
END
Fo
y
nl
O
The flowchart for the pseudocode is shown in the figure.
se
U
tre
en
C
h
ec
pt
rA
Fo
y
nl
O
The DO CASE…END CASE construct
se
is used when a variable is to be
successively compared against
U
different values.
tre
The DO CASE is known as ‘Switch
en
Case’ in C.
C
h
ec
pt
rA
Fo
y
nl
O
The syntax in C will be as follows:
se
switch (expression)
U
{
tre
case const-expr:
statement set;
en
break;
case const-expr:
statement set;
C
h
ec
break;
default
pt
statement set;
rA
}
Fo
y
nl
O
Conditions in a program where the path of execution may branch into two
or more options are referred to as programming, selection, conditional, or
se
branching constructs.
U
The basic selection construct is an IF construct.
tre
The IF…ELSE construct enables the programmer to make a single
comparison and then, execute some steps, based on the outcome.
en
The AND statement can be used in conjunction with the IF statement
C
when more than one condition is to be checked.
h
A nested IF is an IF statement inside another IF statement.
ec