Presentation is loading. Please wait.

Presentation is loading. Please wait.

Making Decisions in a Program

Similar presentations


Presentation on theme: "Making Decisions in a Program"— Presentation transcript:

1 Making Decisions in a Program

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 Concatenate strings 11/16/2018

3 Objectives (continued)
Use the ControlChars.NewLine constant Change the case of a string Determine whether a string contains data Display a message in a message box Include a nested selection structure in pseudocode, a flowchart, and code Include a Case selection structure in pseudocode, a flowchart, and code Generate random numbers 11/16/2018

4 The Selection Structure
Selection structure (or decision structure): Used to select a path to take based on the outcome of a decision or comparison Condition: The decision to be made Results in a Boolean (True or False) answer Four forms of selection structure: If If/Else If/ElseIf/Else Case 11/16/2018

5 Writing Pseudocode for the If
If selection structure: contains one set of instructions to process when the condition is true 11/16/2018

6 Writing Pseudocode for the If/Else Selection Structures
Contains two sets of instructions One set is processed when the condition is true The other set is processed when the condition is false True path: path to follow when condition is true False path: path to follow when condition is false 11/16/2018

7 Flowcharting the If and If/Else Selection Structures
Selection/repetition symbol: Diamond shape Represents both selection and repetition structures One flowline entering and two flowlines leaving 11/16/2018

8 Flowcharting the If and If/Else Selection Structures (continued)
11/16/2018

9 Coding the If and If/Else Selection Structures
11/16/2018

10 Coding the If and If/Else Selection Structures (continued)
11/16/2018

11 Coding the If and If/Else Selection Structures (continued)
Else clause: an optional part of the If statement Statement block: set of statements terminated by an Else or End If 11/16/2018

12 Comparison Operators Comparison operators (or relational operators):
Used as part of the condition in an If statement Most commonly used comparison operators: Equal to: = Greater than: > Greater than or equal to: >= Less than: < Less than or equal to: <= Not equal to: <> 11/16/2018

13 Comparison Operators (continued)
Have no order of precedence Are evaluated from left to right in an expression Example: Evaluation Steps 4/2* 5 > 7 + 2 11/16/2018

14 Using Comparison Operators – Swapping Numeric Values
Pseudocode for a procedure that displays highest and lowest of two numbers: Store If display 11/16/2018

15 Using Comparison Operators – Swapping Numeric Values (continued)
Block scope: the scope of a variable created within a block Block-scope variable: can only be used within the statement block in which it was declared Concatenation operator (&): links two strings ControlChars.NewLine constant: Advances the insertion point to the next line 11/16/2018

16 Using Comparison Operators – Swapping Numeric Values (continued)
11/16/2018

17 Using Comparison Operators – Example 2
Pseudocode for a procedure to allow the user to display the sum or difference of two numbers: 11/16/2018

18 Using the ToUpper and ToLower Methods
String comparisons in Visual Basic are case-sensitive ToUpper method: converts a string to uppercase Syntax: String.toUpper() ToLower method: converts a string to lowercase ToUpper and ToLower can be used to permanently or temporarily convert a variable’s contents String.ToLower() 11/16/2018

19 Logical Operators Logical operators (or Boolean operators):
Used to combine one or more conditions Compound condition: a combination of conditions using logical operator(s) 11/16/2018

20 Logical Operators (continued)
11/16/2018

21 Using the Truth Tables (continued)
11/16/2018

22 Using the Truth Tables (continued)
11/16/2018

23 The String.IsNullOrEmpty Method
String.IsNullOrEmpty method: determine if a control’s Text property or String variable contains data Syntax: String.IsNullOrEmpty method(string) 11/16/2018

24 The MessageBox.Show Method
Display message box with text, buttons and an icon When a message box is displayed, the program waits until the user selects a button MessageBox.Show returns an integer value indicating which button the user selected 11/16/2018

25 The MessageBox.Show Method (continued)
11/16/2018

26 The MessageBox.Show Method (continued)
11/16/2018

27 The MessageBox.Show Method (continued)
11/16/2018

28 Nested Selection Structures
Nested selection structure: a selection structure that is completely contained within another selection structure Primary decision: decision made by the outer selection structure Secondary decision: decision made by the inner selection structure 11/16/2018

29 Nested Selection Structures (continued)
11/16/2018

30 Nested Selection Structures (continued)
11/16/2018

31 Nested Selection Structures (continued)
11/16/2018

32 Nested Selection Structures (continued)
11/16/2018

33 The If/ElseIf/Else Selection Structure
Need a procedure to display a message based on a letter grade: Letter grade Message A Excellent B Above Average C Average D Below Average F 11/16/2018

34 The Case Selection Structure
Used when there are many paths from which to choose Simpler and clearer than using If/ElseIf/Else 11/16/2018

35 The Case Selection Structure (continued)
11/16/2018

36 The Case Selection Structure (continued)
Case selection structure in a flowchart: Uses the diamond symbol One flowline into the diamond, but many flowlines out of the diamond Case selection structure evaluates an expression to determine which path to take Case selection structure: Begins with Select Case Ends with End Select Has one Case clause for each possible value 11/16/2018

37 The Case Selection Structure (continued)
11/16/2018

38 Using To and Is in an ExpressionList
TO and IS keywords: specify a range of values in a Case clause’s expression list TO: When you know both the upper and lower bounds of the range IS: When you know only one end of the range Used with a comparison operator 11/16/2018

39 Generating Random Integers
Pseudo-random number generator: produces a sequence of numbers that meets certain statistical requirements for randomness Random.Next method: Generates a random integer Can specify a minimum and maximum value 11/16/2018

40 Generating Random Integers (continued)
11/16/2018

41 Summary Selection structure is used for decisions
Four forms of selection structures: If, If/Else, If/ElseIf/Else, and Case Diamond symbol represents a decision in a flowchart Expressions with comparison operators will result in an answer of True or False Variables declared within a selection expression have block-level scope 11/16/2018

42 Summary (continued) Concatenation: linking two strings together
Use logical operators to create compound conditions String.IsNullOrEmpty method will determine if a string contains data MessageBox.Show method returns an integer indicating which button was chosen Selection structures can be nested 11/16/2018

43 Summary (continued) Use If/ElseIf/Else or Case structures when there are several possible alternative outcomes Use TO keyword to specify a range of valid values when both the lower and upper bound are known Use IS keyword with a comparison operator to specify a lower or upper bound but not both Use the pseudo-random number generator to generate random numbers Making Decisions in a Program 11/16/2018


Download ppt "Making Decisions in a Program"

Similar presentations


Ads by Google