Top Banner
Making Decisions in a Program Chapter Microsoft Visual Basic .NET: Reloaded 1
63

Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

Dec 14, 2015

Download

Documents

Dennis Haile
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

Making Decisions in a Program

Chapter Microsoft Visual Basic .NET: Reloaded

1

Page 2: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

2Microsoft Visual Basic .NET: Reloaded

Objectives

• Include the selection structure in pseudocode and in a flowchart

• Write an If…Then…Else statement

• Write code that uses comparison operators and logical operators

• Create a variable having block scope

• Use the line continuation character

Page 3: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

3Microsoft Visual Basic .NET: Reloaded

Objectives (continued)

• Concatenate strings

• Use the ControlChars NewLine constant

• Change the case of a string

• Determine whether an expression can be converted to a number

• Display a message in a message box

Page 4: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

4Microsoft Visual Basic .NET: Reloaded

Objectives (continued)

• Include a nested selection structure in pseudocode, flowchart, and code

• Code an If/Elseif/Else selection structure

• Include a Case selection structure in pseudocode, flowchart and code

Page 5: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

5Microsoft Visual Basic .NET: Reloaded

The Selection Structure

• Also called decision structure• Make a comparison and choose between 2

paths depending upon the result of comparison (condition)

Page 6: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

6Microsoft Visual Basic .NET: Reloaded

Writing Pseudocode for the If and If/Else Selection Structures

• If selection structure - 1 set of instructions which are processed if condition is true

• If/Else - 2 sets of instructions – the first when condition is true, the other when condition is false

Page 7: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

7Microsoft Visual Basic .NET: Reloaded

Flowcharting the If and If/Else Selection Structures

Page 8: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

8Microsoft Visual Basic .NET: Reloaded

Coding the If and If/Else Selection Structures

Page 9: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

9Microsoft Visual Basic .NET: Reloaded

Coding the If and If/Else Selection Structures (continued)

• Statement Block

• Set of instructions in “true” path

• (between keywords If and Then)

• Optional set of instructions in “false” path

• (between keywords Then and End If)

• Any items in square brackets are optional

Page 10: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

10Microsoft Visual Basic .NET: Reloaded

Comparison Operators• Also referred to as relational operators

Page 11: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

11Microsoft Visual Basic .NET: Reloaded

Comparison Operators (continued)

Page 12: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

12Microsoft Visual Basic .NET: Reloaded

Using Comparison Operators – Example 1

Page 13: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

13Microsoft Visual Basic .NET: Reloaded

Using Comparison Operators – Example 1 (continued)

Note: variable intTemp has block scope and can only be used in block in which declared

Page 14: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

14Microsoft Visual Basic .NET: Reloaded

Using Comparison Operators – Example 1 (continued)

Page 15: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

15Microsoft Visual Basic .NET: Reloaded

Using Comparison Operators – Example 1 (continued)

• Line continuation character

• Space followed by an underscore

• Used to break up long instructions onto 2 lines

• Concatenation Operator

• The ampersand ( & )

• Used to concatenate (connect or link) strings

Page 16: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

16Microsoft Visual Basic .NET: Reloaded

Using Comparison Operators – Example 1 (continued)

Page 17: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

17Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 18: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

18Microsoft Visual Basic .NET: Reloaded

Using Comparison Operators – Example 2

Page 19: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

19Microsoft Visual Basic .NET: Reloaded

Using Comparison Operators – Example 2 (continued)

Page 20: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

20Microsoft Visual Basic .NET: Reloaded

Using Comparison Operators – Example 2 (continued)

Page 21: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

21Microsoft Visual Basic .NET: Reloaded

Using the ToUpper and ToLower Methods

Page 22: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

22Microsoft Visual Basic .NET: Reloaded

Using the ToUpper and ToLower Methods (continued)

Page 23: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

23Microsoft Visual Basic .NET: Reloaded

Using the ToUpper and ToLower Methods (continued)

Page 24: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

24Microsoft Visual Basic .NET: Reloaded

Logical Operators

Page 25: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

25Microsoft Visual Basic .NET: Reloaded

Logical Operators (continued)

Page 26: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

26Microsoft Visual Basic .NET: Reloaded

Truth Tables

Page 27: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

27Microsoft Visual Basic .NET: Reloaded

Truth Tables (continued)

Note: short-circuit evaluation can occur with “And” operator. Both conditions will not be tested if first condition tests false

Page 28: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

28Microsoft Visual Basic .NET: Reloaded

Using the Truth Tables• Understanding order of precedence is critical

Page 29: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

29Microsoft Visual Basic .NET: Reloaded

Using the Truth Tables (continued)

Page 30: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

30Microsoft Visual Basic .NET: Reloaded

Using Logical Operators in an If…Then…Else Statement

Page 31: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

31Microsoft Visual Basic .NET: Reloaded

Modifying the Skate-Away Sales Application

Page 32: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

32Microsoft Visual Basic .NET: Reloaded

Modifying the Skate-Away Sales Application (continued)

• Modify to eliminate unwanted error message

Page 33: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

33Microsoft Visual Basic .NET: Reloaded

Modifying the Skate-Away Sales Application (continued)

Page 34: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

34Microsoft Visual Basic .NET: Reloaded

The IsNumeric Function

• Function:• A predefined procedure that performs a

specific task and returns a value after completing the task

• IsNumeric function checks if an expression can be converted to a number • Returns Boolean true if able to convert to

number

• Returns Boolean false if conversion cant be made

Page 35: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

35Microsoft Visual Basic .NET: Reloaded

The IsNumeric Function (continued)

Page 36: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

36Microsoft Visual Basic .NET: Reloaded

The MessageBox.Show method

Page 37: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

37Microsoft Visual Basic .NET: Reloaded

The MessageBox.Show method (continued)

• Arguments in the MessageBox.Show method

• Text - a string containing the text to display

• Caption - text displayed in title bar

• Buttons - buttons to display in message box

• Can be one of 6 different constants

• Icon – icon displayed in title bar

• Defaultbutton – button automatically selected if user presses the Enter key on keyboard

Page 38: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

38Microsoft Visual Basic .NET: Reloaded

The MessageBox.Show method (continued)

Page 39: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

39Microsoft Visual Basic .NET: Reloaded

The MessageBox.Show method (continued)

Page 40: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

40Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 41: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

41Microsoft Visual Basic .NET: Reloaded

Nested Selection Structures

• When at least one side of the selection structure’s path contains another selection structure

• Inner structure is referred to as “nested”

• “Else” is paired up with nearest preceding If regardless of indentation of code

• Use when you want to make one decision and then choose between two options depending upon the result of the first decision

Page 42: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

42Microsoft Visual Basic .NET: Reloaded

Nested Selection Structures (continued)

Page 43: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

43Microsoft Visual Basic .NET: Reloaded

Nested Selection Structures (continued)

Page 44: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

44Microsoft Visual Basic .NET: Reloaded

Nested Selection Structures (continued)

Page 45: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

45Microsoft Visual Basic .NET: Reloaded

Nested Selection Structures (continued)

Page 46: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

46Microsoft Visual Basic .NET: Reloaded

The If/ElseIf/Else Selection Structure

Page 47: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

47Microsoft Visual Basic .NET: Reloaded

The Case Selection Structure

PsuedoCode1. Get Grade

2. Grade value:A. Display “Excellent”

B. Display “Above Average”

C. Display “Average”

D,F Display “Below Average”

Other Display “Error”

Page 48: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

48Microsoft Visual Basic .NET: Reloaded

The Case Selection Structure (continued)

Page 49: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

49Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 50: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

50Microsoft Visual Basic .NET: Reloaded

Using To and Is in an ExpressionList

• To allows range of values in Case expression• Example

• Number of items ordered Price per item

1-5$25

6-10$23

• Is allows use of comparison operatorsMore than 10

$20

Page 51: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

51Microsoft Visual Basic .NET: Reloaded

Using To and Is in an ExpressionList (continued)

Page 52: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

52Microsoft Visual Basic .NET: Reloaded

Programming Example – Fat Calculator

• Application allows user to enter the total number of calories and grams of fat contained in a specific food

• Calculate and display 2 values: number of fat calories, and fat percentage

• Display appropriate message depending on level of fat percentage

Page 53: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

53Microsoft Visual Basic .NET: Reloaded

TOE Chart

Page 54: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

54Microsoft Visual Basic .NET: Reloaded

User Interface

Page 55: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

55Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings

Page 56: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

56Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings (continued)

Page 57: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

57Microsoft Visual Basic .NET: Reloaded

Tab Order

Page 58: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

58Microsoft Visual Basic .NET: Reloaded

Pseudocode

btnExit Click event procedureclose application

btnCalc Click event procedureif the txtCalories and txtFatGrams controls contain numbers

assign the numbers to variablescalculate the fat calories by multiplying total calories by 9calculate fat percentage by dividing fat by total caloriesdisplay fat calories and fat percentage in appropriate labelsif fat percentage is over 30% display “This food is high in fat” in message boxelse display “This food is not high in fat” in message boxend if

elsedisplay “The calories and fat grams must be numbers”

end if

Page 59: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

59Microsoft Visual Basic .NET: Reloaded

Code

Page 60: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

60Microsoft Visual Basic .NET: Reloaded

Code (continued)

Page 61: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

61Microsoft Visual Basic .NET: Reloaded

Summary

• Selection structure allows program to make a decision and then select one of 2 paths depending upon the results of that decision

• Visual Basic .NET has 4 selection structures• If If/Else

• If/ElseIf/Else Case

• Selection structures can be “nested” within other selection structures• Outer structure contains primary decision

• Inner structure contains secondary decision

Page 62: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

62Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• All expressions in a selection structure evaluate to either true or false

• Use line continuation character (space &) to break up long instruction into 2 physical lines

• Use concatenation operator (&) to connect (link) 2 strings together

• String comparisons are case sensitive

• Use ToUpper and ToLower functions to convert case

Page 63: Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1.

63Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• ControlChars.NewLine constant advances insertion point to the next line in a control

• Use logical operators to create compound conditions (And, Or, AndAlso)

• IsNumeric function verifies expression can be converted to a number

• MessageBox.Show allows communication with user in the form of a message box

• Use If/ElseIf/Else and Case selection structures to choose between multiple alternatives