Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Making Decisions in a Program Chapter Microsoft Visual Basic.NET: Reloaded 1."— Presentation transcript:

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

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

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

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

5 5 Microsoft 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)

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

7 7 Microsoft Visual Basic.NET: Reloaded Flowcharting the If and If/Else Selection Structures

8 8 Microsoft Visual Basic.NET: Reloaded Coding the If and If/Else Selection Structures

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

10 10 Microsoft Visual Basic.NET: Reloaded Comparison Operators Also referred to as relational operators

11 11 Microsoft Visual Basic.NET: Reloaded Comparison Operators ( continued )

12 12 Microsoft Visual Basic.NET: Reloaded Using Comparison Operators – Example 1

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

14 14 Microsoft Visual Basic.NET: Reloaded Using Comparison Operators – Example 1 ( continued )

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

16 16 Microsoft Visual Basic.NET: Reloaded Using Comparison Operators – Example 1 ( continued )

17 17 Microsoft Visual Basic.NET: Reloaded HOW TO…

18 18 Microsoft Visual Basic.NET: Reloaded Using Comparison Operators – Example 2

19 19 Microsoft Visual Basic.NET: Reloaded Using Comparison Operators – Example 2 ( continued )

20 20 Microsoft Visual Basic.NET: Reloaded Using Comparison Operators – Example 2 ( continued )

21 21 Microsoft Visual Basic.NET: Reloaded Using the ToUpper and ToLower Methods

22 22 Microsoft Visual Basic.NET: Reloaded Using the ToUpper and ToLower Methods ( continued )

23 23 Microsoft Visual Basic.NET: Reloaded Using the ToUpper and ToLower Methods ( continued )

24 24 Microsoft Visual Basic.NET: Reloaded Logical Operators

25 25 Microsoft Visual Basic.NET: Reloaded Logical Operators ( continued )

26 26 Microsoft Visual Basic.NET: Reloaded Truth Tables

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

28 28 Microsoft Visual Basic.NET: Reloaded Using the Truth Tables Understanding order of precedence is critical

29 29 Microsoft Visual Basic.NET: Reloaded Using the Truth Tables ( continued )

30 30 Microsoft Visual Basic.NET: Reloaded Using Logical Operators in an If…Then…Else Statement

31 31 Microsoft Visual Basic.NET: Reloaded Modifying the Skate-Away Sales Application

32 32 Microsoft Visual Basic.NET: Reloaded Modifying the Skate-Away Sales Application ( continued ) Modify to eliminate unwanted error message

33 33 Microsoft Visual Basic.NET: Reloaded Modifying the Skate-Away Sales Application ( continued )

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

35 35 Microsoft Visual Basic.NET: Reloaded The IsNumeric Function ( continued )

36 36 Microsoft Visual Basic.NET: Reloaded The MessageBox.Show method

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

38 38 Microsoft Visual Basic.NET: Reloaded The MessageBox.Show method ( continued )

39 39 Microsoft Visual Basic.NET: Reloaded The MessageBox.Show method ( continued )

40 40 Microsoft Visual Basic.NET: Reloaded HOW TO…

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

42 42 Microsoft Visual Basic.NET: Reloaded Nested Selection Structures ( continued )

43 43 Microsoft Visual Basic.NET: Reloaded Nested Selection Structures ( continued )

44 44 Microsoft Visual Basic.NET: Reloaded Nested Selection Structures ( continued )

45 45 Microsoft Visual Basic.NET: Reloaded Nested Selection Structures ( continued )

46 46 Microsoft Visual Basic.NET: Reloaded The If/ElseIf/Else Selection Structure

47 47 Microsoft Visual Basic.NET: Reloaded The Case Selection Structure PsuedoCode 1.Get Grade 2.Grade value: A. Display “Excellent” B. Display “Above Average” C. Display “Average” D,F Display “Below Average” Other Display “Error”

48 48 Microsoft Visual Basic.NET: Reloaded The Case Selection Structure ( continued )

49 49 Microsoft Visual Basic.NET: Reloaded HOW TO…

50 50 Microsoft Visual Basic.NET: Reloaded Using To and Is in an ExpressionList To allows range of values in Case expression Example Number of items orderedPrice per item 1-5$ $23 Is allows use of comparison operators More than 10$20

51 51 Microsoft Visual Basic.NET: Reloaded Using To and Is in an ExpressionList ( continued )

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

53 53 Microsoft Visual Basic.NET: Reloaded TOE Chart

54 54 Microsoft Visual Basic.NET: Reloaded User Interface

55 55 Microsoft Visual Basic.NET: Reloaded Objects, Properties, and Settings

56 56 Microsoft Visual Basic.NET: Reloaded Objects, Properties, and Settings ( continued )

57 57 Microsoft Visual Basic.NET: Reloaded Tab Order

58 58 Microsoft Visual Basic.NET: Reloaded Pseudocode btnExit Click event procedure close application btnCalc Click event procedure if the txtCalories and txtFatGrams controls contain numbers assign the numbers to variables calculate the fat calories by multiplying total calories by 9 calculate fat percentage by dividing fat by total calories display fat calories and fat percentage in appropriate labels if fat percentage is over 30% display “This food is high in fat” in message box else display “This food is not high in fat” in message box end if else display “The calories and fat grams must be numbers” end if

59 59 Microsoft Visual Basic.NET: Reloaded Code

60 60 Microsoft Visual Basic.NET: Reloaded Code ( continued )

61 61 Microsoft 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 IfIf/Else If/ElseIf/ElseCase Selection structures can be “nested” within other selection structures Outer structure contains primary decision Inner structure contains secondary decision

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

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


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

Similar presentations


Ads by Google