Lecture 6
Lecture 6
1
Selection Structures
• The if selection structure can be used in three
different ways:
1. A simple if structure
2
if
• A simple if structure is called a single-
selection structure because it either selects or
ignores a single action.
• Syntax for the if selection structure is as
follows:
if ( this logical expression is true ) statement ;
• The if selection structure is often written as:
if ( this logical expression is true )
statement ; no semi-colon!
3
if / else
• The if / else structure is called a double-
selection structure because it selects between
two different actions.
• Syntax for the if / else selection structure :
if ( this logical expression is true ) statement ;
else statement ;
• if / else selection structure is often written as:
if ( this logical expression is true )
statement ;
else no semi-colon!
statement ; 4
if statement : Two alternatives
5
if / else if / else
• Nested if / else structures test for multiple
cases by placing if / else structures inside
other if / else structures.
• Syntax for the if / else if / else selection
structure is as follows:
if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else
statement ; 6
if / else if / else
• The actual syntax for the multiple if / else if / else
selection structure is as follows:
if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else
statement ;
7
if / else if / else
• Curly brackets {} can be used to put one or more
statements in a block.
if ( this logical expression is true )
{
Execute statements in this block ;
}
else if ( this logical expression is true )
{
Execute statements in this block ;
}
else
{
Execute statements in this block ;
}
8
Example 1
• One way of writing if/else: • Another way of writing
if/else:
#include <stdio.h>
int main ( )
{
int score ;
printf (“Enter your test score >") ;
scanf ("%d", &score) ;
11
Cont…
if (score >= 90)
printf ("Your score of %d is grade A \n", score) ;
else if (score >= 80)
printf ("Your score of %d is grade B \n", score) ;
else if (score >= 70)
printf ("Your score of %d is grade C \n", score) ;
else if (score >= 60)
printf ("Your score of %d is grade D \n", score) ;
else
printf ("Your score of %d is grade F \n", score) ;
}
12
The switch Statement
• This is another form of the multi way decision. It is
well structured, but can only be used in certain cases
where;
– Only one variable is tested, all branches must depend on
the value of that variable. The variable must be an integral
type (int, long, short or char).
– Each possible value of the variable can control a single
branch. A final, catch all, default branch may optionally be
used to trap all unspecified cases.
13
The switch Statement
• The syntax of the switch statement is as follows:
switch(<expression>)
{
case <constant expression> :
<statements>
break;
………
default:
<statements>
} 14
Example of switch
#include <stdio.h>
void main (void)
{
float n1, n2;
int option;
16