Conditional Statements If these were easy then everyone would them!
-
date post
19-Dec-2015 -
Category
Documents
-
view
215 -
download
0
Transcript of Conditional Statements If these were easy then everyone would them!
![Page 1: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/1.jpg)
Conditional StatementsConditional Statements
If these were easy then everyone would them!
![Page 2: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/2.jpg)
Wanna Buy This Veg-O-Matic Chopper?
Your typical reaction might be: “I don’t know. How much does it cost?”
We usually make those decisions on the basis of some criteria: cost, benefit, amount, need, or some other factor.
![Page 3: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/3.jpg)
Definition of Conditional Statements
Conditional Statements allow us to execute code selectively. So during a specific situation we can choose to execute some code
and not others. The situation is identified by a comparison expression
![Page 4: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/4.jpg)
How it Works
When the comparison returns a true, a selected portion of code is executed.
There are also versions that allow us to execute some code if the comparison is true, and other code if the comparison is false.
![Page 5: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/5.jpg)
StatementConditional
If Condition is TRUE
StatementStatementStatement
Execution Based on Condition
IF StatementIF then ELSE Statement
StatementConditional
Statement
If Condition is TRUE
If Condition is FALSE
![Page 6: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/6.jpg)
Syntax - A Definition
Not a tax on things that are bad for you! Syntax is the specification of a computer code. Such as:
Where operators go When, and if, you need a comma
![Page 7: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/7.jpg)
IF statement - The Syntax
The If statement in Visual Basic has a specific form:
If (condition) Then Statements to Execute if condition is true.
End If
![Page 8: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/8.jpg)
Examples of the IF Statement
If (grade >= 90) Then‘ if the grade is greater than or equal to 90 then
letter_grade = “A”‘ the letter grade is an “A”
End If
![Page 9: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/9.jpg)
Examples of the IF Statement
If (hours = 24) Then‘ if the hours are 24 then
days = days + 1‘ increase the number of days by one
hours = 1‘ set the hours back to 1
End If
![Page 10: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/10.jpg)
Question:
Given the following program, what is the output?
Dim A as Single
A = 5
If (A > 5) Then
MsgBox(A)
End If
A. A message box displaying the number 5
B. There is no output.
![Page 11: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/11.jpg)
IF then ELSE- The Syntax
The If then Else statement :
If (condition) Then
Statements if condition is true.
Else
Statements if condition is false.
End If
![Page 12: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/12.jpg)
Examples of the IF then Else Statement
If ((b^2-4*a*c) < 0 Then
MessageBox(“Roots are imaginary.”)
Else
r1 = (-b+sqrt(b^2-4*a*c)) / (2 * a)
MessageBox(“Root” & r1)
End If
![Page 13: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/13.jpg)
Another Example
Dim user_pi as Single
user_pi = InputBox(”Pi?")
If (user_pi < 3.1416 And user_pi > 3.1414) Then
MsgBox ("Very good!")
Else
MsgBox ("Sorry, the answer is 3.1415")
End If
![Page 14: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/14.jpg)
Question:
What is wrong with this statement?Dim A as Single
A = val(text1.text)
If (A <= 5)
MsgBox(“Greater than 5”)
Else
MsgBox(Cstr(A))
End If
A: There is a A: There is a syntax errorsyntax error
B: The wrong B: The wrong message is message is printedprinted
C: The variable A C: The variable A is the wrong is the wrong type.type.
D: A and BD: A and BE: None of the E: None of the
above.above.
![Page 15: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/15.jpg)
Nesting If Statements
If statements can be placed inside one another.
Getting to the innermost code means all conditions are true Which is precisely the same as And’ing them all
together!
![Page 16: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/16.jpg)
Example of Nesting
If (X < Y) ThenIf (X < Z) Then
If(X < A) ThenX = 0
ElseX = 1
End IfEnd If
End If
![Page 17: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/17.jpg)
Question:
If (A) ThenIf (B) Then
If (C) ThenX = 0
End IfElse
X = 1End If
End If
A: When A, B, and C A: When A, B, and C are trueare true
B: When A and C are B: When A and C are true and B is falsetrue and B is false
C: When A and B are C: When A and B are true and C is falsetrue and C is false
D: B and CD: B and CE: None of the aboveE: None of the above
When will X be set to 1?
![Page 18: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/18.jpg)
The Select Case Statement
The Select Case Statement is used instead of the If Statement when the decision is between many alternatives instead of two.
vs.
![Page 19: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/19.jpg)
Selecting a Case
The Select Case decides which block of code to execute based on an expression.
Each case is a potential value for a variable. When the number of the expression matches the case, that case is selected.
Sometimes its difficult to know all the cases that can come up so the Select Case gives us the Case Else. This essentially says, “Hey, if nothing else matches, do this!”
![Page 20: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/20.jpg)
Select Case Statement - The Syntax
Select Case Test ExpressionCase Expression List 1
First Statement BlockCase Expression List 2
Second Statement BlockCase Else
Default Statement BlockEnd Select
The Test Expression can be any numeric or string expression
![Page 21: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/21.jpg)
The Expression
The expression List comes in one of three forms: A number that must be matched exactly. Two numbers separated with the word ‘To’, which is a range
the number must be in. A comparison operator and a value. The number must be in the
comparison operators relationship to the value.
![Page 22: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/22.jpg)
Examples of the Select Case Statement
Select Case temp Case 0 MsgBox "You're frozen!" Case 1 To 90 MsgBox "Are you dead?" Case 98.6 MsgBox "You're fine" Case Is > 98.6 MsgBox "You have a fever!"End Select
![Page 23: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/23.jpg)
Another Example
Select Case TempCase < 98.6
Msgbox “You’re Cold”Case > 98.6
Msgbox “You’ve a Fever”Case Else
MsgBox "You should never see this.”End Select
![Page 24: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/24.jpg)
Question:
Select Case scoreCase < 60
MsgBox “Didn’t Study”Case < 70
MsgBox “Not Enough”Case < 80
MsgBox “More”Case > 80
MsgBox “Nice Job!”End Case
A: When the A: When the student studied student studied too much.too much.
B: When the B: When the student got an 80student got an 80
C: When the C: When the student got a 100student got a 100
D: B and CD: B and CE: None of the E: None of the
aboveabove
Which case is missing?
![Page 25: Conditional Statements If these were easy then everyone would them!](https://reader030.fdocuments.in/reader030/viewer/2022032704/56649d3f5503460f94a18319/html5/thumbnails/25.jpg)
Example Programs
Validating The Password Checking the Quadratic Formula