Making Decisions in a Program

Slides:



Advertisements
Similar presentations
1.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Programming with Microsoft Visual Basic th Edition
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
C++ for Engineers and Scientists Third Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter Four The Selection Structure
CIS162AD - C# Decision Statements 04_decisions.ppt.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure.
Decision Structures and Boolean Logic
Using the selection structure (Unit 7) Visual Basic for Applications.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Selection Structure If... Then.. Else Case. Selection Structure Use to make a decision or comparison and then, based on the result of that decision or.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Four The Selection Structure.
Tutorial 4: The Selection Structure 1 Tutorial 4 The Selection Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
Random Functions Selection Structure Comparison Operators Logical Operator
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Decisions and Conditions
Exam 1 Review Jeff has 100 students in his MIS120 class. He is giving a 50 question exam worth 100 points. The following commands are available to you.
Chapter 4: Decisions and Conditions
More on the Selection Structure
Chapter 4 The If…Then Statement
Chapter 4: Making Decisions.
The Selection Structure
Chapter 4: The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Chapter 4: Making Decisions.
JavaScript: Control Statements I
Programming with Microsoft Visual Basic 2008 Fourth Edition
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005 BASICS
Topics The if Statement The if-else Statement Comparing Strings
Chapter 3: Introduction to Problem Solving and Control Statements
Objectives After studying this chapter, you should be able to:
Chapter 4: Decision Structures and Boolean Logic
Chapter 8: More on the Repetition Structure
Microsoft Visual Basic 2005: Reloaded Second Edition
The Selection Structure
Chapter 5: The Selection Structure
Chapter 4: Decision Structures and Boolean Logic
Presentation transcript:

Making Decisions in a Program

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Logical Operators (continued) 11/16/2018

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

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

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

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

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

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

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

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

Nested Selection Structures (continued) 11/16/2018

Nested Selection Structures (continued) 11/16/2018

Nested Selection Structures (continued) 11/16/2018

Nested Selection Structures (continued) 11/16/2018

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

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

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

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

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

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

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

Generating Random Integers (continued) 11/16/2018

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

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

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