Conditional Statements Lesson 12
Conditional Statements Lesson 12
1
Conditional Statements
Allow different sets of instructions to be executed depending on
truth or falsity of a logical condition
Also called Branching statements, they are used to have a
program execute different statements depending on certain
conditions.
In a sense, they make a program “smarter” by allowing different
choices to be made.
How do we specify conditions?
Using expressions
non-zero value means condition is true
value 0 means condition is false
Usually logical expressions, but can be any expression
The value of the expression will be used
2
CONDITIONAL/ CONTROL STATEMENTS
3
if Statements
if statements are used in some programs that may
require certain logical test to be carried out at some
particular point.
The if statement may be implemented in different
forms depending on the complexity of conditions to
be tested.
1. Simple if statement
2. if…..else statement
3. Nested if…..else statement
4. elseif ladder
4
Branching: if Statement
if (expression)
statement;
if (expression) {
Block of statements;
}
5
Branching: if Statement
if (expression)
statement;
if (expression) {
Block of statements;
}
print “Passed”
true print “Good
marks >=
40
luck”
false
7
A decision can be
made on any
print “Passed” expression.
true print “Good luck”
marks >= 40 zero - false
nonzero - true
false
8
A decision can be
made on any
print “Passed” expression.
true print “Good luck”
marks >= 40 zero - false
nonzero - true
false
9
Program Example
10
Branching: if-else Statement
11
Branching: if-else Statement
if (expression) {
Block of
statements;
}
else {
Block of
statements;
}
12
13
Find the larger of two numbers
START
READ X, Y
YES IS NO
X>Y?
OUTPUT X OUTPUT Y
STOP STOP
14
Find the larger of two numbers
START
READ X, Y
YES IS NO
X>Y?
OUTPUT X OUTPUT Y
STOP STOP
15
Nesting of if-else Structures
It is possible to nest if-else statements, one
within another
All “if” statements may not be having the “else”
part
Confusion??
Rule to be remembered:
An “else” clause is associated with the closest
preceding unmatched “if”
16
Nested if…..else statement
17
General Format
18
Program to find the greatest of the three numbers
19
elseif ladder
if (expression) {
Block of statements;
}
else if (expression) {
Block of statements;
}
else {
Block of statements;
} 20
21
Grade Computation
22
The switch Statement
An alternative to writing lots of if-else in
some special cases
This causes a particular group of
statements to be chosen from several
available groups based on equality tests
only
Uses switch statement and case labels
23
24
Syntax
switch (expression) {
case const-expr-1: S-1
case const-expr-2: S-2
:
case const-expr-m: S-m
default: S
}
expression is any integer-valued expression
const-expr-1, const-expr-2,…are any constant of
integer-valued expressions
Values must be distinct
S-1, S-2, …,S-m, S are statements/compound
statements
Default is optional, and can come anywhere (not
necessarily at the end as shown) 25
Behavior of switch
expression is first evaluated
It is then compared with const-expr-1, const-
expr-2,…for equality in order
If it matches any one, all statements from that
point till the end of the switch are executed
(including statements for default, if present)
Use break statements if you do not want this (see
example)
Statements corresponding to default, if present,
are executed if no other expression matches
26
Switch Statement
If x = 5 is entered, this will print
Not one or two
switch-2.c 27
The break Statement
Used to exit from a switch or terminate from a loop
With respect to “switch”, the “break” statement
causes a transfer of control out of the entire
“switch” statement, to the first statement following
the “switch” statement
Can be used with other statements also …(will
show later)
28
The Conditional Operator ?:
This makes use of an expression that is either non-0 or 0. An appropriate value is
selected, depending on the value of the expression
Example: instead of writing
if (balance > 5000)
{
interest = balance * 0.2;
}
else
{
interest = balance * 0.1;
}
We can just write
interest = (balance > 5000) ? interest = balance * 0.2 : interest = balance * 0.1;
29
Example of conditional statement
30
End of Lesson
31