Download presentation
Presentation is loading. Please wait.
Published byDwain Gray Modified over 6 years ago
1
OF COURSE I DON'T LOOK BUSY... I DID IT RIGHT THE FIRST TIME
2
Select…Case
3
Using Select Case Visual Basic provides a statement especially for multi-way decisions, called Select Case Statement
4
Definition A statement in which you specify a variable to test and then list a number of cases that you want to test for.
5
Syntax: Select Case intValue Case 1 [do this] Case Is < 2 Case 3, 4
Case Else End Select
6
How it works! In a Select Case statement:
You specify a variable to test. List a number of cases that you want to test for. VB will: Evaluate each Case statement. If the value matches (or evaluates as True) the value contained in the original variable then the block of code between the Case statement is executed. The Select Case statement ends with an End Select statement. As a default, the code under the Case Else statement will be applied if no other Case statement catches it first. If the value in the variable matches more than one Case statement, only the first match will be executed.
7
Example: 'Select type of vehicle to rent Select Case intPassengers
Case 1 To 2 lblOutput.Caption = "You should rent a compact car." Case 3 To 4 lblOutput.Caption = "You should rent a full size car." Case 5 To 7 lblOutput.Caption = "You should rent a minivan." Case 8 To 15 lblOutput.Caption = "You should rent a 15 passenger van." Case Is > 15 lblOutput.Caption = "You should rent a bus." Case Else lblOutput.Caption = "Incorrect data" End Select
8
Visual Basic Program Open VB and create a new form that looks like the following:
9
Dim intPassengers As Integer
Double click the OK button and enter the following code: Dim intPassengers As Integer intPassengers=Val(txtPassengers.Text)
10
'Select type of vehicle to rent Select Case intPassengers
Case 1 To 2 lblOutput.Caption = "You should rent a _ compact car." Case 3 To 4 lblOutput.Caption = "You should rent a full _ size car." Case 5 To 7 lblOutput.Caption = "You should rent a _ minivan."
11
Case 8 To 15 lblOutput.Caption = "You should rent a _ 15 passenger van." Case Is > 15 lblOutput.Caption = "You should rent a _ bus." Case Else lblOutput.Caption = "Incorrect data" End Select
12
Close the Code window and run the program.
Key 14 as the number of passengers and click the OK button. What is output? Save your program. Call me when your program is running and I’ll check into my book.
13
Summary The Select Case statement allows you to make multi-way selections. The Case statements in a Select Case can test a range or use conditional operators. Conditional operators in a Case statement must include the Is keyword. As a default, the Case Else statement is applied if no other Case is true.
14
Homework! Answer True/False:
___ To use a conditional operator in a Case statement, you must use the keyword Is. ___ The Select Case statement ends with an End Select statement. ___ Code under the Case Else statement is the default case in a Select Case statement.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.