Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 29

Introduction to Computing

control structure

1
Control Structures II

2
In this lecture you will study
 Nested if - else - end
 If … elseif …. elseif …. end

3
Nested if Statements

 Nested means that one complete statement is inside another


(if statement contains another if statements)
if condition 1 % first if statement
if condition 2 % second if statement
if condition 3 % third if statement
<Do Action A> % all three conditions are true
end
<Do Action B> % Condition 1 is true and Cond. 2 is false
end
<Do Action C> % Only the first condition is true
end

4
Example-1
 A postgraduate admission sytem
accepts a student application for a
Master degree if his gpa is above 3.6
and his age is below 30.
 Write a program, for this system, that
reads the gpa and the age, then
displays the message ‘Admitted’ if the
student satisifies the conditions
Example-1: Analysis
 Input: gpa, age
 Output : message : addmitted
nothing if not
 Process: check if the student can be
admitted
Example-1: Design
 Read gpa
 Read age
 If gpa > 3.6
 If age < 30
 Diplay (‘ Admitted in the Master program’)
 End
 End
Example: code
Example-2
 Write a Matlab program to determine a student’s
letter grade based on a given integer test score.
 If the score is between 90 and 100, the grade is ‘A’.
 If the score is between 80 and 89, the grade is ‘B’.
 If the score is between 70 and 79 the grade is ‘C’.
 If the score is between 60 and 69 the grade is ‘D’.
 If the score is between 0 and 59 the grade is ‘F’.

9
grade = input('Please enter your grade \n’');
if(grade >= 90)
if(grade <= 100)
fprintf('A\n'); Is there
end
a way
end
if(egrade >= 80) to link
if(grade < 90) more
fprintf( 'B\n'); than one
end condition
end
together
if(grade >= 70)
if(grade < 80)
in a
fprintf('C\n'); single
end if
end statement
if(grade >= 60)
?
if(grade < 70)
fprintf('D\n');
end
end
if(grade >=0 )
if(grade < 60)
fprintf('F\n');
end
end
If with Logical Operators
if (grade >= 90 && grade <= 100)
fprintf('A \n‘);
end
if (grade >= 80 && grade < 90)
fprintf('B \n‘);
end
if (grade >= 70 && grade < 80)
fprintf('C \n‘);
end
if (grade >= 60 && grade < 70)
fprintf('D \n‘);
end
if (grade >= 0 && grade < 60)
fprintf('F \n‘);
end

11
Nested If-else Statement

S T

Nested if-else satements test for multiple cases by


placing if-else selection statements inside other if-
else selection statements
12
Example-3
 A postgraduate admission sytem accepts a
student application for a Master degree if
his gpa is above 3.6 and his age is below
30.
 Write a program, for this system, that reads
the gpa and the age, then displays the
message ‘Admitted’ if the student satisifies
the conditions ‘Not admitted’ if not.
Example-3: Analysis
 Input: gpa, age
 Output : message : addmitted
or
Not admitted
 Process: check if the student can be
admitted
Example-3: Design
 Read gpa
 Read age
 If gpa > 3.6
 If age < 30

 Diplay (‘ Admitted in the Master program’)

 Else

 Diplay (‘Not admitted’)

 End

 Else
 Diplay (‘Not admitted’)

 End
Example-3: code
Example-4
A Blood test device that measures the sugar level classify and
display the state of the patient as follows

Lowl_evel if sugar level below or equal to 3


Normal_level if sugar level is above 3 and less or equal 6

High_level is surgar_level is above 6


Example-4: Analysis
 Input: sugar level
 Output: patient sate
 Process: Determine the patient state
Example-4: Design
 Read sugar_level
 If sugrar_level > 6
 Display(‘High_level’)
 Else
 If sugar_level > 3
 Display(‘Normal_level’)
 Else
 Display(‘Low_Level’)
 End
 End
Example-4: Code
Back to Example-2
 Write a Matlab program to determine a student’s
letter grade based on a given integer test score.
 If the score is between 90 and 100, the grade is ‘A’.
 If the score is between 80 and 89, the grade is ‘B’.
 If the score is between 70 and 79 the grade is ‘C’.
 If the score is between 60 and 69 the grade is ‘D’.
 If the score is between 0 and 59 the grade is ‘F’.
 The program must print ‘Invalid grade’ is the score is out of range
(out of [0 100])
2
function GPA()

grade = input('Please enter your grade\n’);

if( grade >=90 && grade <=100)


fprintf('A\n’); You cannot use the
end
if( grade >=80 && grade <90)
else here because it
fprintf('B\n’); will be part of the
end last if condition, and
if( grade >=70 && grade <80)
fprintf('C\n’);
if the user enter a
end value between 60
if( grade >=60 && grade <70) and 100, the
fprintf('D\n);
end
corresponding letter
if( grade >= 0 && grade <60) grade will be
fprintf('F\n’); displayed as well as
else
fprintf('Invalid grade\n‘);
the invalid grade
end message
end
22
function GPA()

grade = input('Please enter your grade\n’);

if( grade >=90 && grade <=100)


fprintf('A\n’);
end
if( grade >=80 && grade <90)
fprintf('B\n’);
end
if( grade >=70 && grade <80) To handle values
fprintf('C\n);
end outside our range
if( grade >=60 && grade <70) [0-100], we use
fprintf('D\n); this statement
end
if( grade >= 0 && grade <60) with OR (||)
fprintf('F\n); between the two
end conditions
if( grade > 100 || grade <0)
fprintf('Invalid grade\n‘);
end
end 23
grade = input('Please enter your
grade\n’); In this code,
all the if
if( grade >=90 && grade <=100)
fprintf('A\n’);
statements are
end executed,
if( grade >=80 && grade <90) however, the
fprintf('B\n’);
end
fprintf
if( grade >=70 && grade <80) statement is
fprintf('C\n); executed for
end
if( grade >=60 && grade <70)
only one of
fprintf('D\n); them depending
end on the value
if( grade >= 0 && grade <60)
fprintf('F\n);
of grade
end (inefficient!)
if( grade > 100 || grade <0)
fprintf('Invalid grade\n‘);
end
24
Nested If-else Statement solution
 .

The code above is


more efficient
because when one
statement
evaluates to true,
the rest (below
the true) are
skipped

This trailing else


catches invalid
grade scores

25
If-elseif Statement

Equivalent to if-else,
But:
-Does not require an end for each if

-Needs less indentation


-More compact nested if-else

26
If-elseif Statement

27
Exercise 1
 Write a Matlab program that prompt the
user to input three numbers then output
the maximum one.
1. Your code should not use the logical && or ||

2. Re-Write the code using && or ||

28
Exercise 2
 Write a program that read the temperature
(T) then displays

 'Hot' if T > 45
 'Warm' if 30 < T <= 45
 'Cool' if 18 < T <= 30
 'Cold' if 0 < T <= 18
 'Out of range, otherwise
29

You might also like