Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
- IF Blocks - Evaluating conditions
3 IF Statements n Allow a program to decide on a course of action – Based on whether a certain condition is true or false If condition Then Statement(s) Else Other Statement(s) End If Syntax n Condition is an expression that is evaluated to True or False n If condition is true, Statement(s) are executed. If condition is false, Other Statement(s) are executed. If Credits > 10 Then Tuition = 1000 Else Tuition = 100 * Credits End If Example
4 Conditions n Expressions involving: – Relational operators (, >=, =, ….) – Logical operators (And, Or, Not) – Arithmetic operations If (letterGrade = “A”) Or (letterGrade = “B”) Then picOutput.Print “Good Work” Else picOutput.Print “Grade is C or D” End If Example
5 Relational operators n Can be applied to: – numbers – strings
6 Relational operators applied to strings 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 (because of the spaces) “Pay” < “Pay “ True (because of the spaces) “Jones” <> “James” True “Hope” < “Hopeful” True Examples of comparing character strings
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 VB operators hierarchy n First, Arithmetic operations (^, *, /, +, -) are carried out – ^ executed first – * and / executed second – + and – executed third – Note: If parentheses are used, expressions in parentheses are performed first according to those precedence rules. 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.
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 ( < ) Answer: False (Because is not less than )
10 Determine whether the following conditions are true or false? n (0 = 14) Or (6 ^ <= 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
11 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
12 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
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 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 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
Do Loops Select Case Blocks
17 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)
18 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
19 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 ", " " " Ernie ", " " " Grover ", " " " Oscar ", " " Phone.txt
20 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)
21 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 = Do Until Population > Population = Population + (0.014 * Population) Year = Year + 1 Loop picOutput.Print " The answer is: " ; Year End Sub
22 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
23 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
24 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
25 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 A 80-89B 70-79C 60-69D 0-59F txtScore cmdShow picBox
26 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 A 80-89B 70-79C 60-69D 0-59F txtScore cmdShow picBox
27 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 but less than $100000; and 18% for more than $ – Displays the calculated reward in picReward. txtAmount cmdShow picReward
28 Problem 2: IRS Private Sub cmdShowReward_Click() Dim Amount As Single Amount = Val(txtAmount.Text) Select Case Amount Case Is <= Reward = 0.1 * Amount Case Is <= Reward = 0.15 * Amount Case Is > Reward = 0.18 * Amount End Select picReward.Print "The reward is "; Reward End Sub txtAmount cmdShow picReward
- One-dimensional arrays - Two-dimensional arrays - Sequential Files
30 One-dimensional arrays n See hard copy received to prepare for exam 3 n Copy available at:
31 Two-dimensional arrays n See hard copy received to prepare for exam 3 n Copy available at:
32 Sequential Files n See hard copy received to prepare for exam 3 n Copy available at:
List box Combo box
34 List box and Combo box n See Answer to Exercises received in class. n See Quiz 3 (available at