Relational and Logical Operators in Scientific Computing

undefined
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
>> LetterGrade
Worksapce
Program LetterGrade
 
condition
 
command(s)
 
if
 statement example
>> LetterGrade
Input your total grade
Worksapce
Grade = 85
Program LetterGrade
Worksapce
Red arrow points to 
current command
 
if
 statement example
>> LetterGrade
Input your total grade 85
Worksapce
Grade = 85
Program LetterGrade
true
Worksapce
grade    85
 
if
 statement example
>> LetterGrade
Input your total grade 85
Your letter grade is  P
Worksapce
Grade = 85
Program LetterGrade
Worksapce
grade    85
 
if
 statement example
>> LetterGrade
Input your total grade 85
Your letter grade is  P
Worksapce
Grade = 85
Program LetterGrade
Worksapce
grade    85
false
 
if
 statement example
>> LetterGrade
Input your total grade 85
Your letter grade is  P
Worksapce
Grade = 85
Program LetterGrade
Worksapce
grade    85
skip
 
if-else
 statement example
>> LetterGrade2
Worksapce
Program LetterGrade2 : 
a better version of LetterGrade
>> LetterGrade2
Input your total grade
Worksapce
Grade = 85
Program LetterGrade2: 
a better version of LetterGrade
Worksapce
 
if-else
 statement example
>> LetterGrade2
Input your total grade 85
Worksapce
Grade = 85
Program LetterGrade2: 
a better version of LetterGrade
true
Worksapce
grade    85
 
if-else
 statement example
 
if-else
 statement example
>> LetterGrade2
Input your total grade 85
Your letter grade is  P
Worksapce
Grade = 85
Program LetterGrade2: 
a better version of LetterGrade
Worksapce
grade    85
 
if-else
 statement example
>> LetterGrade2
Input your total grade 85
Your letter grade is  P
Worksapce
Grade = 85
Program LetterGrade2: 
a 
better
 version of LetterGrade
Worksapce
grade    85
Automatically 
       skip
No condition to check, i.e. faster code
 
if-else
 statement example
>> LetterGrade2
Input your total grade 85
Your letter grade is  P
>> LetterGrade2
Worksapce
grade    85
Program LetterGrade2: 
a better version of LetterGrade
>> LetterGrade2
Input your total grade 85
Your letter grade is  P
>> LetterGrade2
Input your total grade
Worksapce
Grade = 85
Program LetterGrade2: 
a better version of LetterGrade
Worksapce
grade    85
 
if-else
 statement example
>> LetterGrade2
Input your total grade 85
Your letter grade is  P
>> LetterGrade2
Input your total grade  55
Worksapce
Grade = 85
Program LetterGrade2: 
a better version of LetterGrade
false
Worksapce
grade    55
 
if-else
 statement example
 
if-else
 statement example
Worksapce
Grade = 85
Program LetterGrade2:
a better version of LetterGrade
Worksapce
grade    55
>> LetterGrade2
Input your total grade 85
Your letter grade is  P
>> LetterGrade2
Input your total grade  55
skip
>> LetterGrade2
Input your total grade 85
Your letter grade is  P
>> LetterGrade2
Input your total grade  55
Your letter grade is  F
Worksapce
Grade = 85
Program LetterGrade2: 
a better version of LetterGrade
Worksapce
grade    55
 
if-else
 statement example
Program LetterGrade2: 
a better version of LetterGrade
 
if-else
 statement example
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
 
consequent1
elseif 
condition2
 
consequent2
else
 
alternate
end
Every if statement must
have 
if
, 
condition
,
consequent
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
 
alternate
end
The final else part is not
necessary, but always a
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)
  
c = 3;
elseif (b > 5)
  
c = 4;
else
  
c = 5;
end;
disp(c)
a = 16
b = 4
What is the value of c
that will be displayed?
A) 3
B) 4
C) 5
Exercise 2
if (a > b & b > 3)
  
c = 3;
elseif (b > 5)
  
c = 4;
else
  
c = 5;
end;
disp(c)
a = 
3
b = 4
What is the value of c
that will be displayed?
A) 3
B) 4
C) 5
Exercise 3
if (a > b & b > 3)
  
c = 3;
end
if (b < 5)
  
c = 4;
else
  
c = 5;
end;
disp(c)
a = 16
b = 4
What is the value of c
that will be displayed?
A) 3
B) 4
C) 5
Exercise 4
if (score > 55)
 
disp( 
D
 )
elseif (score > 65)
 
disp( 
C
)
elseif (score > 80)
 
disp( 
B
 )
elseif (score > 93)
 
disp( 
A
 )
else
 
disp( 
Not good…
 )
end
score = 95
What will this code
display
A) D
B) A
C) Not good…
What’s wrong with this
code???
Exercise 4 - modified
if (score > 93)
 
disp( 
A
 )
elseif (score > 80)
 
disp( 
B
)
elseif (score > 70)
 
disp( 
C
 )
elseif (score > 60)
 
disp( 
D
 )
else
 
disp( 
F
 )
end
score = 95
What will this code
display
A) D
B) A
C) Not good…
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
 
Any Questions
35
Slide Note
Embed
Share

Learn about the importance and use cases of relational and logical operators in scientific computing through examples like gene expression analysis, grading systems, and workplace scheduling. Discover how to effectively implement these operators in programming constructs and functions such as if statements and loops.

  • Scientific Computing
  • Operators
  • Programming Constructs
  • Logical Operators
  • Relational Operators

Uploaded on Oct 03, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. SCIENTIFIC COMPUTING LECTURE # 9 IF STATEMENTS

  2. Whats 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.

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

  4. Today we will look at selections through if statements Input 1 ______ ______ ______ ______ ______ ______ ______ ______ ______ Input 2 ______ ______ ______ ______ ______ ______ ______ ______ ______

  5. The if statement syntax if condition commands end

  6. if statement example Program LetterGrade condition command(s) >> LetterGrade Worksapce

  7. if statement example Program LetterGrade Red arrow points to current command >> LetterGrade Input your total grade Worksapce Grade = 85 Worksapce

  8. if statement example Program LetterGrade true >> LetterGrade Input your total grade 85 Worksapce Grade = 85 grade 85 Worksapce

  9. if statement example Program LetterGrade >> LetterGrade Input your total grade 85 Your letter grade is P Worksapce Grade = 85 grade 85 Worksapce

  10. if statement example Program LetterGrade false >> LetterGrade Input your total grade 85 Your letter grade is P Worksapce Grade = 85 grade 85 Worksapce

  11. if statement example Program LetterGrade skip >> LetterGrade Input your total grade 85 Your letter grade is P Worksapce Grade = 85 grade 85 Worksapce

  12. if-else statement example Program LetterGrade2 : a better version of LetterGrade >> LetterGrade2 Worksapce

  13. if-else statement example Program LetterGrade2: a better version of LetterGrade >> LetterGrade2 Input your total grade Worksapce Grade = 85 Worksapce

  14. if-else statement example Program LetterGrade2: a better version of LetterGrade true >> LetterGrade2 Input your total grade 85 Worksapce Grade = 85 grade 85 Worksapce

  15. if-else statement example Program LetterGrade2: a better version of LetterGrade >> LetterGrade2 Input your total grade 85 Your letter grade is P Worksapce Grade = 85 grade 85 Worksapce

  16. if-else statement example Program LetterGrade2: a better version of LetterGrade Automatically skip No condition to check, i.e. faster code >> LetterGrade2 Input your total grade 85 Your letter grade is P Worksapce Grade = 85 grade 85 Worksapce

  17. if-else statement example Program LetterGrade2: a better version of LetterGrade >> LetterGrade2 Input your total grade 85 Your letter grade is P >> LetterGrade2 Worksapce grade 85

  18. if-else statement example Program LetterGrade2: a better version of LetterGrade >> LetterGrade2 Input your total grade 85 Your letter grade is P >> LetterGrade2 Input your total grade Worksapce Grade = 85 grade 85 Worksapce

  19. if-else statement example Program LetterGrade2: a better version of LetterGrade false >> LetterGrade2 Input your total grade 85 Your letter grade is P >> LetterGrade2 Input your total grade 55 Worksapce Grade = 85 grade 55 Worksapce

  20. if-else statement example Program LetterGrade2:a better version of LetterGrade skip >> LetterGrade2 Input your total grade 85 Your letter grade is P >> LetterGrade2 Input your total grade 55 Worksapce Grade = 85 grade 55 Worksapce

  21. if-else statement example Program LetterGrade2: a better version of LetterGrade >> LetterGrade2 Input your total grade 85 Your letter grade is P >> LetterGrade2 Input your total grade 55 Your letter grade is F Worksapce Grade = 85 grade 55 Worksapce

  22. 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)

  23. 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)

  24. General if syntax Every if statement must have if, condition, consequent if condition1 consequent1 elseif condition2 consequent2 else alternate end

  25. General if syntax if condition1 consequent1 elseif condition2 consequent2 else alternate end You can have as many elseif parts as you need.

  26. General if syntax if condition1 consequent1 elseif condition2 consequent2 else alternate end The final else part is not necessary, but always a good idea.

  27. General if syntax if condition1 consequent1 elseif condition2 consequent2 else alternate end The end word is necessary.

  28. Exercise 1 if (a > b & b > 3) c = 3; elseif (b > 5) c = 4; else c = 5; end; disp(c) a = 16 b = 4 What is the value of c that will be displayed? A) 3 B) 4 C) 5

  29. Exercise 2 if (a > b & b > 3) c = 3; elseif (b > 5) c = 4; else c = 5; end; disp(c) a = 3 b = 4 What is the value of c that will be displayed? A) 3 B) 4 C) 5

  30. Exercise 3 if (a > b & b > 3) c = 3; end if (b < 5) c = 4; else c = 5; end; disp(c) a = 16 b = 4 What is the value of c that will be displayed? A) 3 B) 4 C) 5

  31. Exercise 4 score = 95 if (score > 55) disp( D ) elseif (score > 65) disp( C ) elseif (score > 80) disp( B ) elseif (score > 93) disp( A ) else disp( Not good ) end What will this code display A) D B) A C) Not good What s wrong with this code???

  32. Exercise 4 - modified score = 95 if (score > 93) disp( A ) elseif (score > 80) disp( B ) elseif (score > 70) disp( C ) elseif (score > 60) disp( D ) else disp( F ) end What will this code display A) D B) A C) Not good

  33. if statements and vectors We know the following >> X = [70 60 50] >> X >= 60 ans = 1 1 0

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

  35. 35 Any Questions

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#