Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

1.
Programming with Microsoft Visual Basic th Edition
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
Selection Logic Building decision-making into programs.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
Chapter 4: Control Structures: Selection
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Chapter Four The Selection Structure
CIS162AD - C# Decision Statements 04_decisions.ppt.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Chapter 4 The If…Then Statement
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
Lecture Set 5 Control Structures Part A - Decisions Structures.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Flowcharts! January 13, 2005 These are today’s notes! Do you think we will get more snow?
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Programming Logic and Design, Second Edition, Comprehensive
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.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Python Selection. All the programs you have been developing so far have been sequential, this means that each instruction is executed in a set order.
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 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 14 Do It, Then Ask Permission.
110 F-1 Decisions and Conditions Chapter 4: We can design a form We can calculate To make our programs more powerful, we need to be able to make choices.
Controlling Program Flow with Decision Structures.
Tutorial 4: The Selection Structure 1 Tutorial 4 The Selection Structure.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth 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.
Chapter 4: Decisions and Conditions
More on the Selection Structure
Chapter 4 The If…Then Statement
The Selection Structure
Chapter 4: The Selection Structure
Numbering System TODAY AND TOMORROW 11th Edition
Chapter 4: Decision Structures and Boolean Logic
Programming with Microsoft Visual Basic 2008 Fourth Edition
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005 BASICS
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
Objectives After studying this chapter, you should be able to:
Chapter 4: Decision Structures and Boolean Logic
Chapter 8: More on the Repetition Structure
Visual Basic – Decision Statements
Chapter 5: Control Structure
Microsoft Visual Basic 2005: Reloaded Second Edition
Relational Operators.
Chapter 5: The Selection Structure
Chapter 4: Decision Structures and Boolean Logic
Presentation transcript:

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program

Microsoft Visual Basic 2005: Reloaded, Second Edition2 Objectives After studying this chapter, you should be able to: 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

Microsoft Visual Basic 2005: Reloaded, Second Edition3 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

Microsoft Visual Basic 2005: Reloaded, Second Edition4 Objectives (continued) Code an If/ElseIf/Else selection structure Include a Case selection structure in pseudocode, a flowchart, and code Generate random numbers

Microsoft Visual Basic 2005: Reloaded, Second Edition5 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

Microsoft Visual Basic 2005: Reloaded, Second Edition6 The Selection Structure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition7 Writing Pseudocode for the If and If/Else Selection Structures If selection structure: contains one set of instructions to process when the condition is true If/Else selection structure: –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

Microsoft Visual Basic 2005: Reloaded, Second Edition8 Writing Pseudocode for the If and If/Else Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition9 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

Microsoft Visual Basic 2005: Reloaded, Second Edition10 Flowcharting the If and If/Else Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition11 Coding the If and If/Else Selection Structures

Microsoft Visual Basic 2005: Reloaded, Second Edition12 Coding the If and If/Else Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition13 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

Microsoft Visual Basic 2005: Reloaded, Second Edition14 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: <>

Microsoft Visual Basic 2005: Reloaded, Second Edition15 Comparison Operators (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition16 Comparison Operators (continued) Comparison operators: –Have no order of precedence –Are evaluated from left to right in an expression

Microsoft Visual Basic 2005: Reloaded, Second Edition17 Comparison Operators (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition18 Using Comparison Operators – Swapping Numeric Values Pseudocode for a procedure that displays highest and lowest of two numbers:

Microsoft Visual Basic 2005: Reloaded, Second Edition19 Using Comparison Operators – Swapping Numeric Values (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition20 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

Microsoft Visual Basic 2005: Reloaded, Second Edition21 Using Comparison Operators – Swapping Numeric Values (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition22 Using Comparison Operators – Swapping Numeric Values (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition23 Using Comparison Operators – Swapping Numeric Values (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition24 Using Comparison Operators – Swapping Numeric Values (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition25 Using Comparison Operators – Swapping Numeric Values (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition26 Using Comparison Operators – Example 2 Pseudocode for a procedure to allow the user to display the sum or difference of two numbers:

Microsoft Visual Basic 2005: Reloaded, Second Edition27 Using Comparison Operators – Example 2 (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition28 Using Comparison Operators – Example 2 (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition29 Using Comparison Operators – Example 2 (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition30 Using Comparison Operators – Example 2 (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition31 Using the ToUpper and ToLower Methods String comparisons in Visual Basic are case-sensitive ToUpper method: converts a string to uppercase ToLower method: converts a string to lowercase ToUpper and ToLower can be used to permanently or temporarily convert a variable’s contents

Microsoft Visual Basic 2005: Reloaded, Second Edition32 Using the ToUpper and ToLower Methods (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition33 Using the ToUpper and ToLower Methods (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition34 Using the ToUpper and ToLower Methods (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition35 Logical Operators Logical operators (or Boolean operators): –Used to combine one or more conditions Compound condition: a combination of conditions using logical operator(s)

Microsoft Visual Basic 2005: Reloaded, Second Edition36 Logical Operators (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition37 Logical Operators (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition38 Logical Operators (continued) Truth tables: used to evaluate logical operators in an expression Short-circuit evaluation: an evaluation in which the second condition may not be evaluated And and Or operations always evaluate both conditions AndAlso and OrElse operations do not evaluate second condition if the first condition is false

Microsoft Visual Basic 2005: Reloaded, Second Edition39 Logical Operators (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition40 Logical Operators (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition41 Using the Truth Tables Use And or AndAlso when both conditions must be true to give a true result Use Or or OrElse when one or both conditions must be true to give a true result Use XOr when exactly one condition must be true to give a true result Logical operators are evaluated after arithmetic or comparison operators in an expression

Microsoft Visual Basic 2005: Reloaded, Second Edition42 Using the Truth Tables (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition43 Using the Truth Tables (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition44 Using Logical Operators in an If…Then…Else Statement Data validation: –Verifying that the input data is within the expected range Use an If…Then…Else statement to validate input data

Microsoft Visual Basic 2005: Reloaded, Second Edition45 Using Logical Operators in an If…Then…Else Statement (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition46 Using Logical Operators in an If…Then…Else Statement (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition47 Using Logical Operators in an If…Then…Else Statement (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition48 The String.IsNullOrEmpty Method String.IsNullOrEmpty method: determine if a control’s Text property or String variable contains data

Microsoft Visual Basic 2005: Reloaded, Second Edition49 The String.IsNullOrEmpty Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition50 Modifying the Skate-Away Sales Application

Microsoft Visual Basic 2005: Reloaded, Second Edition51 Modifying the Skate-Away Sales Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition52 Modifying the Skate-Away Sales Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition53 Modifying the Skate-Away Sales Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition54 The MessageBox.Show Method 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

Microsoft Visual Basic 2005: Reloaded, Second Edition55 The MessageBox.Show Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition56 The MessageBox.Show Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition57 The MessageBox.Show Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition58 The MessageBox.Show Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition59 The MessageBox.Show Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition60 The MessageBox.Show Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition61 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

Microsoft Visual Basic 2005: Reloaded, Second Edition62 Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition63 Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition64 Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition65 Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition66 Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition67 Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition68 Nested Selection Structures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition69 The If/ElseIf/Else Selection Structure Need a procedure to display a message based on a letter grade: Letter gradeMessage AExcellent BAbove Average CAverage DBelow Average F

Microsoft Visual Basic 2005: Reloaded, Second Edition70 The If/ElseIf/Else Selection Structure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition71 The If/ElseIf/Else Selection Structure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition72 The Case Selection Structure Case selection structure: –Used when there are many paths from which to choose –Simpler and clearer than using If/ElseIf/Else

Microsoft Visual Basic 2005: Reloaded, Second Edition73 The Case Selection Structure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition74 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

Microsoft Visual Basic 2005: Reloaded, Second Edition75 The Case Selection Structure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition76 The Case Selection Structure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition77 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

Microsoft Visual Basic 2005: Reloaded, Second Edition78 Using To and Is in an Expression List (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition79 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

Microsoft Visual Basic 2005: Reloaded, Second Edition80 Generating Random Integers (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition81 Generating Random Integers (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition82 Generating Random Integers (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition83 Programming Tutorial

Microsoft Visual Basic 2005: Reloaded, Second Edition84 Programming Example

Microsoft Visual Basic 2005: Reloaded, Second Edition85 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

Microsoft Visual Basic 2005: Reloaded, Second Edition86 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

Microsoft Visual Basic 2005: Reloaded, Second Edition87 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