= 90 picOutput.Print "A" End Select Grade = Val(txtBox.Text) Select Case Grade Case 60 To 89 picOutput.Print "Pass" End Select"> = 90 picOutput.Print "A" End Select Grade = Val(txtBox.Text) Select Case Grade Case 60 To 89 picOutput.Print "Pass" End Select">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Select Case Blocks School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 8, Monday 3/03/2003)

Similar presentations


Presentation on theme: "Select Case Blocks School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 8, Monday 3/03/2003)"— Presentation transcript:

1 Select Case Blocks School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 8, Monday 3/03/2003)

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

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

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

5 5 Problem 2: IRS (To be done in class) 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

6 6 Consider that user’s response are 8.5 and 17. Determine the output displayed in the picture box when the command button is clicked. Private Sub cmdDisplay_Click() Dim Age As Single, Price As Single Age = Val(InputBox("What is your age ?")) Select Case Age Case Is < 6 Price = 0 Case 6 To 17 Price = 3.75 Case Is >= 17 Price = 5 End Select picOutput.Print "The price is "; FormatCurrency(Price) End Sub Answer: 3.75 3.75

7 7 Consider that user’s response is 2. Determine the output displayed in the picture box when the command button is clicked. Private Sub cmdDisplay_Click() Dim num1 As Single, word As String, num2 As Single num1 = 3 word = "Hello" num2 = Val(InputBox("Enter a number")) Select Case 2 * num2 - 1 Case num1 * num1 picOutput.Print "Less is more" Case Is > Len(word) picOutput.Print "Time keeps everything from happening at once." Case Else picOutput.Print "The less things change, ……." End Select End Sub Answer:

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

9 9 Identify the errors. Private Sub cmdDisplay_Click() Dim num1 As Single, num2 As Single num1 = 5 num2 = 7 Select Case num1 Case 3 <= num1 <= 10 picOutput.Print "Between 3 and 10" Case num2 To 5; 4 picOutput.Print "Near 5" End Select End Sub Answer: First Case statement should be Case 3 To 10 In the 2 nd Case statement, should be 5 To num2.And "," instead of ";".

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

11 11 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 n Case "Hello", Is < "goodbye" Answer: Valid n Case "un" & "til" Answer: Valid


Download ppt "Select Case Blocks School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 8, Monday 3/03/2003)"

Similar presentations


Ads by Google