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

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Select Case Statements and Selection Input.
Advertisements

30/04/ Selection Nested If structures & Complex Multiple Conditions.
VB Numbers and Strings School of Business Eastern Illinois University (Week 4, Monday 2/03/2003) © Abdou Illia, Spring 2003.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
1 CS 106, Winter 2009 Class 10, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
Conditional Statements If these were easy then everyone would them!
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Chapter 5 - VB.Net by Schneider
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Two-Dimensional Arrays School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 11, Friday 4/04/2003)
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 5 - Visual Basic Schneider1 Chapter 5 Decisions.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 51 Select Case block A decision-making structure that simplifies choosing among several actions. Avoids complex nested If constructs. If blocks.
Using the Select Case Statement and the MsgBox Function (Unit 8)
Chapter 4: The Selection Structure
Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)
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.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
Database Management School of Business Eastern Illinois University © Abdou Illia, Fall 2002 (Week 16, Monday 12/09/2002)
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
Chapter 4 - VB 2008 by Schneider1 Chapter 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks 5.4 A Case Study: Weekly.
Chapter five selected exercises with solutions. Exercises 5.1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
Flow of Control Part 1: Selection
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
MULTIPLE ALTERNATIVES IF… THEN… ELSEIF SELECT CASE.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
Programming with Microsoft Visual Basic th Edition
22/11/ Selection If selection construct.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Sequential files School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 12, Monday 4/07/2003)
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable.
1 Chapter 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks 4.4 Input via User Selection.
31/01/ Selection If selection construct.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Computer Science Up Down Controls, Decisions and Random Numbers.
Chapter 4 - VB 2008 by Schneider1 Chapter 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks.
1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks 5.4 A Case Study: Weekly Payroll.
Week 4 Relational and Logical Operators Dr. Jennifer Cunningham.
5.04 Apply Decision Making Structures
Chapter 4 - VB 2008 by Schneider
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Chapter 4 - Visual Basic Schneider
Selection Statements Chapter 4 Attaway MATLAB 4E.
If selection construct
Chapter 5 - Visual Basic Schneider
If selection construct
Chapter 5 Decisions.
Selection Statements Chapter 4 Attaway MATLAB 5E.
Presentation transcript:

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

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

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 but less than $100000; and 18% for more than $ – Displays the calculated reward in picReward. txtAmount cmdShow picReward

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:

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