School of Business Eastern Illinois University IF Blocks
School of Business Eastern Illinois University IF Blocks (Week 6, Friday 2/28/2003) © Abdou Illia, Spring 2003
2 IF Blocks n Represent computers abilities to make decisions n Allow a program to decide on a course of action – n Based on whether a certain condition is true or false Simplest form (Single-alternative decision): Syntax If Condition Then Statement(s) End If Example If Credits < 10 Then Tuition = 100 * Credits End If n Condition is an expression that is evaluated by the computer n If Condition is true, Statement(s) are executed; otherwise Statement(s) are skipped
3 IF Blocks (Usual form) n Usual IF Blocks form (Compound alternative decision): Syntax If condition Then Statement 1 Else Statement 2 End If If condition is true, Statement 1 is executed. If condition is false, Statement 2 is executed. Example If Credits > 10 Then Tuition = 1000 Else Tuition = 100 * Credits End If
4 Nested IF Blocks n Nested IF Blocks (IF Blocks contained inside others): Syntax If condition 1 Then [Statement(s)] If condition 2 Then Statement(s) End If n Example If Credits > 10 Then Tuition = 1000 If Credits > 15 Then Message = "Over load" End If Note: In the nested If Blocks, each If must have its own End If statement Fill in this table indicating the values of variables Tuition and Message for each value of Credits 10 9 15 17 Tuition Message
IF Blocks with Else. If Clauses n What will be printed in pic. Box? 5 Allow for many alternatives: Syntax If condition 1 Then Statements 1 Else. If condition 2 Then Statements 2 Else. If condition 3 Then Statements 3 Else Statements 4 End If n Credits 10 9 0 17 Example If Credits > 10 Then Tuition = 1000 Else. If Credits = 0 Then Message = "No registration" Else. If Credits =< 10 Then Tuition = 100 * Credits Message = "Part-Time" Else pic. Box. Print Tuition; Message End If Select Case Statements usually better than using Else. If
Conditions n Expression involving: – – Relational operators (<, >, >=, …. ) Logical operators (And, Or, Not) letter. Grade = txt. Grade. Text If ( letter. Grade = "A") Or (letter. Grade = "B") Then pic. Output. Print "Good Work" Else. If (letter. Grade = "C") Then pic. Output. Print " Average Work" Else pic. Output. Print "Poor Work" End If 6
Relational operators n Applied to numbers. Also applied to strings 7
8 Relational operators n Computer uses a special coding system to compare character strings n String 1 is said to be less than String 2 if String 1 precede String 2 alphabetically according to ANSI table (See Appendix A). "Chase" < "Chaz" True " Cat" < "Cat" True "Pay" < "Pay " True "Jones" <> "James" True "Hope" < "Hopeful" True Example: Comparing character strings
Logical operators n The three Logical Operators are: Not, And, Or n Not: Negates a single expression. Example: Suppose answer = "Y" Not (answer = "y") is true n And: Combines two expressions; each expression must be True for the entire expression to be True. Example: Suppose answer = "Y" (answer = "Y") And (answer = "y") is False n Or: Combines two expressions; either expression (or both expressions) must be True for the entire expression to be True. Example: Suppose answer = "Y" (answer = "Y") Or (answer = "y") is True 9
VB operators hierarchy 10 n First, Arithmetic operations (^ , *, /, +, -) are carried out n Second, Expressions involving relational operators (>, <, =, …) are evaluated to True or False n Third, Logical operators are applied in the order Not, And, Or. n In the event of a tie, the leftmost operator is applied. Note: Expressions in parentheses evaluated first
11 Determine whether the following conditions are true or false? n 1 <= 1 Answer: True n "car" < "cat" Answer: True n "Dog" < "dog" Answer: True n ("Y" < > "X") And (143. 55 < 143. 55) Answer: False
12 Determine whether the following conditions are true or false? n (0 = 14) Or (6 ^ 2 - 3 <= 4 / 2 + 8) Answer: False n Not (6 = 7) And ( 44 > 33) Answer: True n In the next two cases, suppose that n = 4 n (2 < n) And (n < 6) Answer: True n (2 < n) Or (n = 6) Answer: true
13 Determine whether the following conditions are true or false? n In the next cases, assume a = 2 and b = 3 n 3*a=2*b Answer: True n (5 – a) * b < 7 Answer: False n b <= 3 Answer: True n a^b=b^a Answer: false
14 Determine whether the following conditions are true or false? n In the next cases, assume a = 2 and b = 3 n a ^ (5 – 2) > 7 Answer: True n (a < b) Or (b < a) Answer: True n (a * a < b) Or Not (a * a < a) Answer: True n Not (a < b) Or Not (a < (b + a)) Answer: False
Determine the output displayed in the picture box 15 when the command button is clicked. Private Sub cmd. Display_Click() Dim a As Single a=5 If 3 * a – 4 < 9 Then pic. Output. Print "Remember, " End If pic. Output. Print "Tomorrow is another day. " End Sub Answer: Tomorrow is another day.
Determine the output displayed in the picture box 16 when the command button is clicked. Private Sub cmd. Display_Click() Dim Gpa As Single Gpa = 3. 49 If Gpa >= 3. 5 Then pic. Output. Print "Honors"; End If pic. Output. Print "Student" End Sub Answer: Student
Determine the output displayed in the picture box 17 when the command button is clicked. Private Sub cmd. Display_Click() Dim a As Single a=5 If (a > 2) And (a = 3 Or a <7) Then pic. Output. Print "Hi" End If End Sub Answer: Hi
- Slides: 17