Lecture 9
Lecture 9
LECTURE # 9
IF STATEMENTS
What’s the use of relational and logical operators?
Examples:
Given a set of genes and their expressions, get all
genes with expression higher than a specific level.
In a pass fail course, give all students with total grade
>= 60 a letter grade P and students with total grade
< 60 a letter grade of F.
Schedule payday for employees based on the first
letter of their last names.
Set the room A/C temperature based on the time of the
day.
How to make use of relational and logical
operators?
Programming constructs /functions that makes use of
relational and logical operators
if statements
find function
loops
Today we will look at selections through
if statements
Input 1 Input 2
______ ______
______ ______
______ ______
______ ______
______ ______
______ ______
______ ______
______ ______
______ ______
The if statement syntax
if condition
commands
end
if statement example
Program LetterGrade
condition
command(s)
>> LetterGrade
Worksapce
if statement example
Program LetterGrade
>> LetterGrade
Worksapce
Input your total grade Grade = 85
if statement example
Program LetterGrade
true
>> LetterGrade
Worksapce
Input your total grade 85 Grade =85
grade 85
if statement example
Program LetterGrade
>> LetterGrade
Worksapce
Input your total grade 85 Grade =85
grade 85
Your letter grade is P
if statement example
Program LetterGrade
false
>> LetterGrade
Worksapce
Input your total grade 85 Grade =85
grade 85
Your letter grade is P
if statement example
Program LetterGrade
skip
>> LetterGrade
Worksapce
Input your total grade 85 Grade =85
grade 85
Your letter grade is P
if-else statement example
Program LetterGrade2 : a better version of LetterGrade
>> LetterGrade2
Worksapce
if-else statement example
Program LetterGrade2: a better version of LetterGrade
>> LetterGrade2
Worksapce
Input your total grade Grade = 85
if-else statement example
Program LetterGrade2: a better version of LetterGrade
true
>> LetterGrade2
Worksapce
Input your total grade 85 Grade =85
grade 85
if-else statement example
Program LetterGrade2: a better version of LetterGrade
>> LetterGrade2
Worksapce
Input your total grade 85 Grade =85
grade 85
Your letter grade is P
if-else statement example
Program LetterGrade2: a better version of LetterGrade
>> LetterGrade2
Worksapce
Input your total grade 85 Grade =85
grade 85
Your letter grade is P
if-else statement example
Program LetterGrade2: a better version of LetterGrade
>> LetterGrade2
Worksapce
Input your total grade 85 grade 85
Your letter grade is P
>> LetterGrade2
if-else statement example
Program LetterGrade2: a better version of LetterGrade
>> LetterGrade2
Worksapce
Input your total grade 85 Grade =85
grade 85
Your letter grade is P
>> LetterGrade2
Input your total grade
if-else statement example
Program LetterGrade2: a better version of LetterGrade
false
>> LetterGrade2
Worksapce
Input your total grade 85 Grade =55
grade 85
Your letter grade is P
>> LetterGrade2
Input your total grade 55
if-else statement example
Program LetterGrade2:a better version of LetterGrade
skip
>> LetterGrade2
Worksapce
Input your total grade 85 Grade =55
grade 85
Your letter grade is P
>> LetterGrade2
Input your total grade 55
if-else statement example
Program LetterGrade2: a better version of LetterGrade
>> LetterGrade2
Worksapce
Input your total grade 85 Grade =55
grade 85
Your letter grade is P
>> LetterGrade2
Input your total grade 55
Your letter grade is F
if-else statement example
Program LetterGrade2: a better version of LetterGrade
What if
someone entered an invalid input (-ve grade for
example)?
or
there were more than two cases to cover (you want to
give A, B, C, D, F grades)
if-elseif statement
What if
someone entered an invalid input (-ve grade for
example)?
or
there were more than two cases to cover (you want to
give A, B, C, D, F grades)
General if syntax
if condition1 Every if statement must
consequent1 have if, condition,
elseif condition2 consequent
consequent2
else
alternate
end
General if syntax
if condition1
consequent1
elseif condition2
consequent2
else
alternate
end You can have as many elseif
parts as you need.
General if syntax
if condition1
consequent1
elseif condition2
consequent2
else The final else part is not
alternate necessary, but always a
end good idea.
General if syntax
if condition1
consequent1
elseif condition2
consequent2
else
alternate
end The end word is necessary.
Exercise 1
if (a > b & b > 3) a = 16
c = 3; b = 4
else A) 3
c = 5;
B) 4
end;
disp(c) C) 5
Exercise 2
if (a > b & b > 3) a = 3
c = 3; b = 4
else A) 3
c = 5;
B) 4
end;
disp(c) C) 5
Exercise 3
if (a > b & b > 3) a = 3
c = 3; b = 6
c = 4; A) 3
else
B) 4
c = 5;
end; C) 5
disp(c)
Exercise 4
score = 95
if (score > 55)
disp( ‘D’ ) What will this code
display
elseif (score > 65)
disp( ‘C’) A) D
elseif (score > 80) B) A
disp( ‘B’ )
C) Not good…
elseif (score > 93)
disp( ‘A’ ) What’s wrong with this
else code???
disp( ‘Not good…’ )
end
Exercise 4 - modified
score = 95
if (score > 93)
disp( ‘A’ ) What will this code
display
elseif (score > 80)
disp( ‘B’) A) D
elseif (score > 70) B) A
disp( ‘C’ )
C) Not good…
elseif (score > 60)
disp( ‘D’ )
else
disp( ‘F’ )
end
if statements and vectors
We know the following
35
Exercise 5: Answer
36
Exercise 6:
• A student received three quiz scores stored in a vector:
grades = [75 85 95];
37
Exercise 6: Answer
38
39
Any Questions