Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review for Exam 2 School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 10, Friday 3/21/2003) - IF Blocks - Do Loops - Select.

Similar presentations


Presentation on theme: "Review for Exam 2 School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 10, Friday 3/21/2003) - IF Blocks - Do Loops - Select."— Presentation transcript:

1 Review for Exam 2 School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 10, Friday 3/21/2003) - IF Blocks - Do Loops - Select Case Blocks

2 2 IF Blocks n Represent computers abilities to make decisions n Allow a program to decide on a course of action – Based on whether a certain condition is true or false n Simplest form (Single-alternative decision): If Condition Then Statement(s) End If Syntax If Credits < 10 Then Tuition = 100 * Credits End If Example 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 3 Nested IF Blocks n Nested IF Blocks (IF Blocks contained inside others): If condition1 Then [Statement(s)] If condition2 Then Statement(s) End If Syntax If Credits > 10 Then Tuition = 1000 If Credits > 15 Then Message = "Over load" End If Example n Note: In the nested If Blocks, each If must have its own End If statement CreditsTuitionMessage 10 9 15 17 Fill in this table indicating the values of variables Tuition and Message for each value of Credits

4 4 Conditions n Expression involving: – Relational operators (, >=, ….) – Logical operators (And, Or, Not) letterGrade = txtGrade.Text If ( letterGrade = "A") Or (letterGrade = "B") Then picOutput.Print "Good Work" ElseIf (letterGrade = "C") Then picOutput.Print " Average Work" Else picOutput.Print "Poor Work" End If

5 5 Relational operators n Applied to numbers. Also applied to strings

6 6 Relational operators n Computer uses a special coding system to compare character strings n String1 is said to be less than String2 if String1 precede String2 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

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

8 8 VB operators hierarchy 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

9 9 Determine whether the following conditions are true or false? n 1 <= 1 Answer: True n "car" < "cat" Answer: True (because alphabetically, "car" precedes "cat") n "Dog" < "dog" Answer: True (because Uppercase precedes lowercase) n ("Y" "X") And (143.55 < 143.55) Answer: False

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

11 11 Determine whether the following conditions are true or false? 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

12 12 Determine whether the following conditions are true or false? 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

13 13 Determine the output displayed in the picture box when the command button is clicked. Private Sub cmdDisplay_Click() Dim a As Single a = 5 If 3 * a – 4 < 9 Then picOutput.Print "Remember, " End If picOutput.Print "Tomorrow is another day." End Sub Answer: Tomorrow is another day.

14 14 Determine the output displayed in the picture box when the command button is clicked. Private Sub cmdDisplay_Click() Dim Gpa As Single Gpa = 3.49 If Gpa >= 3.5 Then picOutput.Print "Honors"; End If picOutput.Print "Student" End Sub Answer: Student

15 15 Determine the output displayed in the picture box when the command button is clicked. Private Sub cmdDisplay_Click() Dim a As Single a = 5 If (a > 2) And (a = 3 Or a <7) Then picOutput.Print "Hi" End If End Sub Answer: Hi

16 16 Do Loops (using While) n Used to repeat a sequence of statements while a condition is True Do While Condition Statement(s) Loop Syntax 1 Private Sub cmdDisplay_Click() Dim Num As Integer ' Display the numbers from 1 to 10 Num = 1 Do While Num <= 10 picNumbers.Print Num; Num = Num + 1 Loop End Sub Example Do Statement(s) Loop While Condition Syntax 2 n Condition involve: – Relational operators (, >=, ….) – Logical operators (And, Or, Not) Same rules than Condition in IF Blocks (seen before Spring Break)

17 17 Do Loops (using Until) n Used to repeat a sequence of statements until a condition becomes True Do Statement(s) Loop Until condition Syntax 4 Private Sub cmdDisplay_Click() Dim Num As Integer ' Display the numbers from 1 to 10 Num = 1 Do Until Num > 10 picNumbers.Print Num; Num = Num + 1 Loop End Sub Example Do Until conditon Statement(s) Loop Syntax 3 n Note: To get the same result using While and Until, Conditions should be opposites. For instance, While Num 10

18 18 Do Loops n Used to display all or selected items from a file – Using EOF function Private Sub cmdDisplay_Click() Dim Name As String, Phone As String picPhones.Cls Open " A:\Phone.txt " For Input As #1 Do While Not EOF(1) Input #1, Name, Phone picPhones.Print Name, Phone Loop Close #1 End Sub Example " Bert ", " 123-4567 " " Ernie ", " 987-6543 " " Grover ", " 246-8321 " " Oscar ", " 135-7900 " Phone.txt

19 19 Replace each statement containing Until with an equivalent statement containing a While, and vice versa. n Loop Until AccountBalance >= 1200 Answer: Loop While AccountBalance < 1200 n Do While Amount = 100 Answer: Do Until Amount 100 n Do Until Name = "Bob" Answer: Do While Name "Bob" n Do While (a > 1) And (a < 3) Answer: Do Until (a = 3) n Do Until (Answer "Y") Or (n >= 7) Answer: Do While (Answer = "Y") And (n < 7)

20 20 Identify the errors n Private Sub cmdDisplay_Click() Dim Num As Integer ' Display the numbers from 1 to 10 Do While Num >= 10 Num = 1 picOutut.Print Num; Num = Num + 1 Loop End Sub Answer: Program never stops. Statement Num = 1 should appear before Do Loop n Private Sub cmdDisplay_Click() Dim q As Single q = 1 Do While q > 0 q = 3 * q - 1 picOutput.Print q; Loop End Sub Answer: Program never stops because q = 2 within the loop all time.

21 21 Identify the errors n Private Sub cmdDisplay_Click() Dim Num As Single Open “DATA.TXT” For Input As #1 Do While (Not EOF (1)) And (Num > 0) Input #1, Num picOutut.Print Num; Close #1 End Sub Note: Assume that DATA.TXT contains: 7, 6, 0, -1, 2 Answer: Loop statement is missing. Also, loop cannot be entered because the (default) value of Num is 0 n Private Sub cmdDisplay_Click() Dim Num As Integer ' Display numbers Do While 1 < Num < 5 Num = 1 picOutut.Print Num; Num = Num + 1 Loop End Sub Answer: Invalid condition. Condition should be (1 < Num) And (Num < 5)

22 22 Simple program using a Do loop n The world population reached 6 billion people in 1999 and was growing at the rate of 1.4 percent each year. Assuming that the population will continue to grow at the same rate, write a program to determine when the population will exceed 10 billion. Private Sub cmdCalculate_Click( ) Dim Year As Integer, Population As Single Year = 1999 Population = 6 Do Until Population > 10 Population = Population + (0.014 * Population) Year = Year + 1 Loop picOutput.Print " The answer is: " ; Year End Sub

23 23 For …Next n Repeats a group of statements a specified number of times n For…Next are used when we know exactly how many times a loop should be executed. Private Sub cmdDisplay_Click() Dim i As integer picTable.Cls For i = 1 To 5 picTable.Print i; i^2 Next i End Sub Example For counter = start To end Statement(s) Next [counter] Where: counter is a numeric variable used as a loop counter start is the initial value of counter end is the final value of counter Private Sub cmdDisplay_Click() Dim i As integer picTable.Cls i = 1 Do While i <= 5 picTable.Print i; i^2 i = i + 1 Loop End Sub Same example using Do loop

24 24 Select Case Blocks n Very efficient decision-making structures: – Simplifies choosing among several actions n Avoid using Nested IF and ElseIf statements n Actions based on the value of an expression called selector Select Case Selector Case ValueList1 Action1 Case ValueList2 Action2 : Case Else Action of last resort End Select Syntax Age = Val(txtAge.Text) Select Case Age Case 5 Category = "5-year old" Case 13 To 19 Category = "Teenager" Case Is > 65 Category = "Senior" Case Else Category = "Everyone else" End Select Example

25 25 Select Case Statements n Selectors can be: – Numeric or string variables. Example: Select Case FirstName – Expressions. Example: Select Case Left(EmployeeName, 1) Select Case Selector Case ValueList1 Action1 Case ValueList2 Action2 : Case Else Action of last resort End Select Syntax n ValueList items must be separated by commas n ValueList can be: – A constant – An expression – An inequality sign preceded by Is and followed by a constant, a variable, or an expression – A range expressed in the form a To b. x = 2 : y = 3 Num = Val(txtNumber.Text) Select Case Num Case y – x, x picOutput.Print "Buckle my shoes" End Select Grade = Val(txtBox.Text) Select Case Grade Case Is >= 90 picOutput.Print "A" End Select Grade = Val(txtBox.Text) Select Case Grade Case 60 To 89 picOutput.Print "Pass" End Select

26 26 Problem 1: Assigning letter grades (To be done in class) n Write a Click event procedure associated to the command button cmdShow that asks the user to type in an exam score and: – Assigns a letter grade based on the scale in the table below. The variable to be assigned the letter grade as value should be named Grade – Displays the score typed by the user and the letter grade in picBox. ScoreLetter grade 90-100A 80-89B 70-79C 60-69D 0-59F txtScore cmdShow picBox

27 27 Problem 1: Assigning letter grades Private Sub cmdShow_Click() Dim Score As Single Score = Val(txtScore.Text) Select Case Score Case 90 To 100 Grade = "A" Case 80 To 89 Grade = "B" Case 70 To 79 Grade = "C" Case 60 To 69 Grade = "D" Case 0 To 59 Grade = "F" End Select picBox.Print Score; Grade End Sub ScoreLetter grade 90-100A 80-89B 70-79C 60-69D 0-59F txtScore cmdShow picBox

28 28 Problem 2: IRS n IRS informants are paid cash rewards based on the value of the money recovered. n Write a Click event procedure associated to the command button cmdReward that asks the user to type in the Amount recovered and: – Determine the Reward based on the following rule: 10% for up to $75000 recovered, 15% for a recovered amount greater than 75000 but less than $100000; and 18% for more than $100000. – Displays the calculated reward in picReward. txtAmount cmdShow picReward

29 29 Problem 2: IRS Private Sub cmdShowReward_Click() Dim Amount As Single Amount = Val(txtAmount.Text) Select Case Amount Case Is <= 75000 Reward = 0.1 * Amount Case Is <= 100000 Reward = 0.15 * Amount Case Is > 100000 Reward = 0.18 * Amount End Select picReward.Print "The reward is "; Reward End Sub txtAmount cmdShow picReward

30 30 Identify the errors. Private Sub cmdDisplay_Click() Dim num As Single num = 2 Select Case num picOutput.Print "Two" End Select End Sub Answer: Case statement is missing.

31 31 Identify the errors. Private Sub cmdDisplay_Click() Dim num As Single num = 5 Select Case num Case 5, Is <>5 picOutput.Print "Five" Case Is > 5 picOutput.Print "Greater than 5" End Sub Answer: - First Case statement is executed every time, no matter the value of num - Should have an End Select.

32 32 Suppose the selector of a Select Case statement, word, evaluates to a string value. Determine whether the Case clause is valid. n Case Left("abc", 1) Answer: Valid n Case word "No" Answer: Invalid. Should be Case Is "No" n Case 0 To 9 Answer: Invalid (because 0 To 9 doesn’t refer to a string value) n Case "Hello", Is < "goodbye" Answer: Valid n Case "un" & "til" Answer: Valid (because "un" & "til" will give until)


Download ppt "Review for Exam 2 School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 10, Friday 3/21/2003) - IF Blocks - Do Loops - Select."

Similar presentations


Ads by Google