0% found this document useful (0 votes)
3 views

Lecture 9

This document discusses the use of if statements in scientific computing, focusing on relational and logical operators. It provides examples of how to implement if, if-else, and if-elseif statements in programming, particularly for grading systems and decision-making based on conditions. Additionally, it includes exercises to reinforce understanding of these concepts.

Uploaded by

janazahrani2120
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 9

This document discusses the use of if statements in scientific computing, focusing on relational and logical operators. It provides examples of how to implement if, if-else, and if-elseif statements in programming, particularly for grading systems and decision-making based on conditions. Additionally, it includes exercises to reinforce understanding of these concepts.

Uploaded by

janazahrani2120
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

SCIENTIFIC COMPUTING

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

Red arrow points to


current command

>> 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

No condition to check, i.e. faster code Automatically


skip

>> 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

elseif (b > 5) What is the value of c


c = 4; that will be displayed?

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

elseif (b > 5) What is the value of c


c = 4; that will be displayed?

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

end What is the value of c


if (b < 5) that will be displayed?

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

>> X = [70 60 50]


>> X >= 60
ans =
1 1 0
if statements and vectors

 true will not be displayed because the condition is


not true for every element in the vector
 if statements are best fit for scalar values
Exercise 5:
• You are given a vector X that stores the grades of 5 students:
X = [80 45 65 90 55];

Write a MATLAB if statement to check whether all students passed the


course (passing grade is 60 or above).

• If all passed, display: 'All students passed.'


• Otherwise, display: 'Not all students passed.'

35
Exercise 5: Answer

36
Exercise 6:
• A student received three quiz scores stored in a vector:
grades = [75 85 95];

Write a MATLAB program that:


1. Calculates the average of the grades.
2. Uses an if-elseif-else structure to assign a letter grade based on the
average:
• A if average ≥ 90
• B if average ≥ 80
• C if average ≥ 70
• F otherwise

37
Exercise 6: Answer

38
39

Any Questions

You might also like