
Chapter 4 Controlling Program Flow with Decision Structures Key Concepts Case Study Controlling the flow of a program Pizza Order application Generating random numbers Writing algorithms Writing compound Boolean expressions Using message boxes and counters Selecting appropriate test data Creating and modifying problem solutions The If…Then Statement conditional control structure The I f...T h e n statement is a conditional control structure, also called a decision structure, which executes a set of statements when a condition is true. The I f...T h e n statement takes the form: If condition Then statements End If TIP The condition of an For example, in the following I f...T h e n statement guess = 7 is the condition, I f...T h e n statement should and there is one statement that will execute when this condition is true: never make an equality comparison between floating If guess = 7 Then point numbers because of the Me.lblMessage.Text = "You guessed it!" possibility of roundoff error. End If In the condition, the equal sign (=) is used as a relational operator to determine if the value of guess is equal to 7. If equal, then the Text prop- erty of lblMessage is changed. If not, program flow continues to the next statement after the End If. Boolean expression The condition of an I f...T h e n statement is a Boolean expression, which relational operators evaluates to either True or False. Relational operators can be used to form Boolean expressions. There are six relational operators: Operator Meaning TIP The equal sign (=) is used = equal to as a relational operator as well < less than as an assignment operator. <= less than or equal to > greater than >= greater than or equal to <> not equal to Chapter 4 Controlling Program Flow with Decision Structures 93 VB2010.indb 93 5/2/2011 11:03:48 PM The NumberGuess application demonstrates the I f...T h e n statement: Running the application, typing 5 in the text box, and then clicking Check Guess displays no message. Typing 7 and clicking Check Guess displays the message “You guessed it!” A Boolean variable may also be used as the condition of an I f...T h e n statement because its value is either True or False. For example, the I f... Then statement below uses a Boolean variable as its condition: Dim gameOver As Boolean = True If gameOver Then Application.Exit() End If Review: TestGrade – part 1 of 5 Create a TestGrade application that prompts the user for a test score and then displays “Good job!” for a test score greater than or equal to 70 when Check Grade is clicked. The applica- tion interface should look similar to that shown on the right after typing 75 and clicking Check Grade. Be sure to include a TextChanged event procedure for the text box. The If…Then…Else Statement The I f...T h e n statement can include an optional Else clause that is executed when the If condition evaluates to False. The I f...T h e n ...E l s e statement takes the following form: If condition Then statements Else statements End If 94 Chapter 4 Controlling Program Flow with Decision Structures VB2010.indb 94 5/2/2011 11:03:49 PM The NumberGuess application could be modified to include an I f... T h e n ...E l s e statement: If guess = SECRETNUMBER Then Me.lblMessage.Text = "You guessed it!" Else Me.lblMessage.Text = "Try again." End If Running the application, typing a number other than 7, and then clicking Check Guess displays the message “Try again.” Typing 7 and then clicking Check Guess displays the message “You guessed it!” The indentation used in the I f...T h e n ...E l s e statement is a code convention that makes the statement easier to read. It has no effect on the execution of the statement. The indentation also makes it easier for a programmer reading the code to follow the logic of the statement. Review: TestGrade – part 2 of 5 Modify the TestGrade application so that “Good job!” is displayed for a score greater than or equal to 70 and “Study more.” is displayed for a score less than 70. Review: CircleArea – part 2 of 2 Modify the CircleArea application created in Chapter 3 to display the message “Negative radii are illegal.”. when a negative radius value is entered, otherwise the area of the circle should be displayed. Nested If…Then…Else Statements An I f...T h e n ...E l s e statement can contain another I f...T h e n ...E l s e or If... nested Then statement, which is said to be nested. For example, the NumberGuess application could be modified to give the user a hint: If guess = SECRETNUMBER Then 'correct Me.lblMessage.Text = "You guessed it!" Else If guess < SECRETNUMBER Then 'too low TIP When you click a keyword Me.lblMessage.Text = "Too low." in a control structure, all of the Else 'too high keywords in the structure are Me.lblMessage.Text = "Too high." highlighted. End If End If Nested statements should be indented as good programming style. Chapter 4 Controlling Program Flow with Decision Structures 95 VB2010.indb 95 5/2/2011 11:03:49 PM The If…Then…ElseIf Statement TIP There is no space in The I f...T h e n ...E l s e I f statement is used to decide among three or more ElseIf. actions and takes the form: If condition Then statements ElseIf condition Then statements ... Else statements End If There can be multiple ElseIf clauses, and the last Else clause is optional. For example, there are three possible decisions in the I f...T h e n ...E l s e I f Commenting Complex statement below: Decision Structures If guess = SECRETNUMBER Then 'correct Decision structures with many Me.lblMessage.Text = "You guessed it!" branches can quickly become ElseIf guess < SECRETNUMBER Then 'too low difficult to understand. Brief Me.lblMessage.Text = "Too low." inline comments can make Else 'too high code much more readable. Me.lblMessage.Text = "Too high." This is especially important for End If the last branch of a decision structure, which usually The logic used in developing an I f...T h e n ...E l s e I f statement is important. does not include an explicit For example, when testing a range of numbers, If conditions must be instruction. properly ordered because statements are executed only for the first true condition and then program flow continues to the End If. When choosing between nested I f...T h e n ...E l s e statements and a single I f...T h e n ...E l s e I f statement, the I f...T h e n ...E l s e I f is easier to read and understand and is considered better programming style. Review: TestGrade – part 3 of 5 Modify the TestGrade application so that “Great!” is displayed for a test score greater than or equal to 90, “Good job!” for a test score greater than or equal to 70 and less than 90, and “Study more.” otherwise. The Select…Case Statement The S e le c t...C a s e statement is a conditional control structure that uses the result of an expression to determine which statements to execute. The S e le c t...C a s e statement is sometimes preferable to the I f...T h e n ...E l s e I f statement because code may be easier to read. The S e le c t...C a s e statement takes the form: Select expression Case value statements ... Case Else statements End Select 96 Chapter 4 Controlling Program Flow with Decision Structures VB2010.indb 96 5/2/2011 11:03:49 PM The expression must evaluate to a built-in data type. There can be multiple Case clauses, and the Case Else clause is optional. The value type should match the expression type and can be a single value, a list separated by commas, or a range separated by the keyword To. End Select is required to complete the S e le c t...C a s e statement. The S e le c t...C a s e statement below uses the value of a score to determine the message to display: Select Case score Case 0, 10 'score is 0 or 10 Me.lblMessage.Text = "Nice try." Case 20 To 23 'score is 20, 21, 22, or 23 Me.lblMessage.Text = "Great!" Case Else 'score not 0, 10, 20, 21, 22, 23 Me.lblMessage.Text = "Invalid score." End Select Review: Hurricane The Saffir-Simpson Hurricane Scale provides a rating (a category) depending on the current intensity of a hurricane. Create a Hurricane application that prompts the user for a wind speed and then displays the hurricane category. Display the speed in miles per hour (mph), knots (kts), and kilometers per hour (km/hr). Refer to the Saffir-Simpson Hurricane Scale below for wind speeds. The application interface should be similar to that shown on the right after typing 100 and clicking Category. Category 1: 74-95 mph or 64-82 kt or 119-153 km/hr Category 2: 96-110 mph or 83-95 kt or 154-177 km/hr Category 3: 111-130 mph or 96-113 kt or 178-209 km/hr Category 4: 131-155 mph or 114-135 kt or 210-249 km/hr Category 5: greater than 155 mph or 135 kt or 249 km/hr The Select…Case Is Statement The S e le c t...C a s e I s statement compares the result of an expression to a range of values when a relational operator is part of the value.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages36 Page
-
File Size-