# Lecture 3 – Selection

```Lecture 3 – Selection
Outline









Recall selection control structure
Types of selection
One-way selection
Two-way selection
Multi-selection
Compound statement
Nested if
Conditional operator
Switch structure
RECALL..
Selection structure


Used to choose among alternative
courses of action
C has three types: if, if..else, and
switch
The if selection structure

if structure is a single-entry/single-exit
structure
If student’s grade is greater than or equal to 60
Print “Passed”
false
true
print “Passed”
The if..else selection
structure

Specifies an action to be performed
both when the condition is true and
when it is false
If student’s grade is greater than or equal
to 60
Print “Passed”
else
Print “Failed”
false
print “Failed”
true
print “Passed”
Selection 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 (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
If student’s grade is greater than or equal to 60
Print “Passed”
false
true
print “Passed”
One-way Selection = if
…..
printf(“Passed\n”);
…..
…..
One-way Selection = if
Another example:
……

if(markah&gt;= 90)
……
…...
One-way Selection = if

Another example:
If temperature is greater than 70
degree and it is not raining, the
recommended activity is golfing
bool rain=false;
…
if((temp &gt; 70) &amp;&amp; !(rain))
printf(“recommended activity is golfing”);
One-way Selection = if

Two Common Mistakes

if score &gt;= 90 //no parentheses

if(score &gt;= 90);
//; not here
Exercises (if statement)
Write C codes for the following:
• if x is not equal to zero then divide y by x
and the result is stored at z
• adds x to a variable sum if x is positive
• if x is positive, adds x to sum but in
addition it adds 1 to a variable poscount
• Convert the pseudocode to C codes:
Prompt the user for age, display this
sentence &quot;Wow! You're really old!&quot; if the
age is greater than 40.
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
Two-way Selection =
if..else
If student’s grade is greater than or equal
to 60
Print “Passed”
else
Print “Failed”
false
print
“Failed”
60
true
print
“Passed”
Two-way Selection =
if..else
………
printf(“Passed\n”);
else
printf(“Failed\n”);
……
Two-way Selection =
if..else

Another example:
if (hours &gt; 40.0)
//Line 1
wages = 40.0 * rate +1.5 * rate * (hours - 40.0);
else
//Line 3
wages = hours * rate;
//Line 4


//Line 2
If hours is 50, then the statement at Line 2 is
executed/performed
If hours is 30, then the statement at Line 4 is
executed/performed
Exercises (if statement)
Write C codes for the following:
• if x is not equal to zero then divide y by x
and the result is stored at z, else
otherwise display an error message
• if x is positive, adds x to sum_pos but in
addition it adds 1 to a variable poscount.
if x is negative, adds x to sum_neg but in
addition it adds 1 to a variable negcount
• If a is greater than b, give appropriate
output, else give another message
Multi-selection = if-else if
• The syntax is:
if(exp1)
{stmt1;}
else if(exp2)
{stmt2;}
else if(exp3)
{stmt3;
…}
else
{stmt n;}
stmt x;
An if-else if control structure
shifts program control, step by
step,through a series of
statement blocks.
Example
int testscore = 76;
if (testscore &gt;= 90)
{
}
else if (testscore &gt;= 80)
{
}
else if (testscore &gt;= 70)
{
}
else if (testscore &gt;= 60)
{
}
else { grade = 'F'; }
Examples
1. Let us consider an example of grading the students
in an academic institution. The grading is done
according to the following rules:
80-100 Honours
60- 79 First Division
50- 59 Second Division
40- 49 Third Division
0- 39 Fail
2. Increments count1 if 0 &lt;= x &lt; 0.25, increments
count2 if 0.25 &lt;= x &lt; 0.5, increments count3 if 0.5
&lt;= x &lt; 0.75 and increments count4 if 0.75 &lt;= x&lt;
1. Assume that a real variable x is known to be
greater than or equal to zero and less than one.
Multi-selection = if-else if

E.g.
temp
&gt;30
0c
temp &gt;30
display
hot
false
temp &gt; 20
20-30 0c mild
false
10-19 0c cold
temp &gt;10
&lt;10 0c
very cold
false
Print “very cold”
true
true
true
Print “hot”
Print “mild”
Print “cold”
Multi-selection = if-else if
if(temp &gt; 30)
printf( “hot\n”);
else if((temp &gt;=20) &amp;&amp; (temp&lt;=30))
printf( “mild\n”);
else if(temp &gt;=10) &amp;&amp; (temp &lt;= 19))
printf(“cold\n”);
else
printf( “very cold\n”);
Comparison
Types of Selection
Examples
Structure
Single Selection
if x is not equal to zero then
if
divide y by x
Double Selection
if ..else
if x is not equal to zero then
divide y by x
else
display an error message
Multi Selection
if .. else..if
if x is
set
else if
set
else
set
less than zero then
y equal to -1
x is equal to zero then
y equal to 0
y equal to 1
Compound (Block of) Stmt

A compound statement (also called a
block of statements) takes the form







{
statement 1; statement 2;
.
.
.
statement n;
}
Compound (Block of) Stmt
Example:
if (age &gt; 18)
{

printf(&quot;Eligible to vote.\n“);
printf(&quot;No longer a minor.\n“);
}
else
{
}
printf(&quot;Not eligible to vote.\n“);
printf(“Still a minor\n”);
Nested if
•
When one control statement is within another, it is said to be nested
• if (test condition1)
{
if (test condition 2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
statement-x;
Nested if

Eg.
if (temperature &gt;= 50)
{
if (temperature &gt;= 80)
printf( &quot;Good day for swimming.\n”);
else
printf( &quot;Good day for golfing.\n“);
}
else
printf(&quot;Good day to play tennis.\n“);
Examples
1. Check the two numbers entered by the user,
the first number must be 1, and the second
number must be 2. Mission is accomplished
only if both are correct.
2. Write C code for the following:
If the input is ten or below ten, &quot;below 10&quot; will
be printed. If the input is above ten, the
program will go into the &quot;else statement&quot;. In the
&quot;else statement&quot; there is another &quot;if statement&quot;
(this is nesting). This &quot;if statement&quot; checks the
input again. If the input is below sixty, &quot;below
60&quot; will be printed.
Nested if

Another example
The Conditional Operator (? :)



The syntax of using the conditional operator
is: expression1 ? expression2 : expression3;
This is called a conditional expression.
The statement:
if (a &gt;= b)
max = a;
else
max = b;

Is equivalent to the statement:
max = (a &gt;= b) ? a : b;
switch Structures


Similar to if-else if control structure
The general form (syntax):
switch (expression)
{
}
case value1: statements1; break;
case value2: statements2; break;
.
.
.
case valuen: statementsn; break;
default: statements;
switch Structures



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.
switch Structures






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 label, so
you do not need to use braces to turn multiple
statements into a single compound statement.
The break statement may or may not appear after
each statement.
switch Structures

Example:
{
}


case 'A': printf(&quot;The grade is A.“); break;
case 'B': printf(&quot;The grade is B.“); break;
case 'C': printf(&quot;The grade is C.“); break;
case 'D': printf(&quot;The grade is D.“); break;
case 'F': printf(&quot;The grade is F.“); break;
default: printf(&quot;The grade is invalid.“);
where, grade is a variable of the type char. If the
value of grade is, say 'A', the output is
The grade is A.
switch Structures

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 from the switch
structure
Examples
1. Check if got any error here, if so, correct it/them.
2. Use switch to count the number of students earned
in each grade .
What’s wrong??
End Week 3
Q &amp; A!
```