Microsoft Visual Basic 2008: Reloaded Fourth Edition

Slides:



Advertisements
Similar presentations
Programming with Microsoft Visual Basic th Edition
Advertisements

1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
Selection Logic Building decision-making into programs.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
1.
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.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Chapter Four The Selection Structure
Chapter 4: The Selection Structure
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure.
Using the selection structure (Unit 7) Visual Basic for Applications.
Chapter 3 Making Decisions
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Lecture Set 5 Control Structures Part A - Decisions Structures.
Programming Logic and Design, Second Edition, Comprehensive
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
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.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
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.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
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 th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Four The Selection Structure.
Tutorial 4: The Selection Structure 1 Tutorial 4 The Selection Structure.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
Microsoft Visual Basic 2008: Reloaded Third Edition
Chapter 4: Decisions and Conditions
The Selection Structure
Chapter 4: The Selection Structure
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
Chapter 3: Introduction to Problem Solving and Control Statements
Objectives After studying this chapter, you should be able to:
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 5: The Selection Structure
Presentation transcript:

Microsoft Visual Basic 2008: Reloaded Fourth Edition Chapter Four Making Decisions in a Program

Objectives After studying this chapter, you should be able to: Include the selection structure in pseudocode and in a flowchart Explain the difference between single-alternative and dual-alternative selection structures Code a selection structure using the If…Then…Else statement Include comparison operators and logical operators in a selection structure’s condition Create a block-level variable Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Objectives (cont'd.) Concatenate strings Use the ControlChars.NewLine constant Change the case of a string Include a check box in an interface Generate random numbers Microsoft Visual Basic 2010: Reloaded, Fourth Edition

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 Single-alternative selection structure: performs a set of tasks only when the condition is true True path: the tasks to perform when the condition is true Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The Selection Structure (cont’d.) Dual-alternative selection structure: contains one set of tasks to perform when the condition is true and a different set of tasks to perform when the condition is false False path: the tasks to perform when the condition is false Pseudocode uses if…end if to denote a selection structure and else to denote the false path Indent instructions within the selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The Selection Structure (cont'd.) Figure 4-1: Selection structures you might use today Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The Selection Structure (cont'd.) Figure 4-1: Selection structures you might use today (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The Selection Structure (cont'd.) Figure 4-2: Problem specification for Mountain Biking Figure 4-3: Interface for the Mountain Biking application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The Selection Structure (cont'd.) Figure 4-4: Pseudocode containing only the sequence structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The Selection Structure (cont'd.) Figure 4-5: Modified problem specification and pseudocode containing a single-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 4-6: Single-alternative selection structure shown in a flowchart Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 4-7: Modified problem specification and pseudocode containing a dual-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 4-8: Dual-alternative selection structure shown in a flowchart Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Coding Single-Alternative and Dual-Alternative Selection Structures If…Then…Else statement: used to code single-alternative and dual-alternative selection structures Else clause: an optional part of the If statement Only used for the dual-alternative selection structure Condition must be a Boolean expression that evaluates to either True or False Can contain variables, literal constants, named constants, properties, methods, arithmetic operators, comparison operators, and logical operators Statement block: set of statements in the true path or the false path Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 4-9: How to use the If…Then…Else statement Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 4-9: How to use the If…Then…Else statement (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparison Operators Comparison operators (or relational operators): Used as part of the condition in an If…Then…Else statement to compare two values 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 2010: Reloaded, Fourth Edition

Figure 4-10: How to use comparison operators in a condition Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparison Operators (cont’d.) Figure 4-10: How to use comparison operators in a condition (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparison Operators (cont'd.) Have no order of precedence Are evaluated from left to right in an expression Are evaluated after any arithmetic operators in the expression All expressions containing comparison operators evaluate to True or False only Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparison Operators (cont'd.) Figure 4-11: Evaluation steps for an expression containing arithmetic and comparison operators Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparing Numeric Values Auction House application displays highest and lowest of two bids entered by the user Figure 4-12: Sample run of the Auction House application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparing Numeric Values (cont'd.) Figure 4-13: Pseudocode containing a single-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 4-14: Flowchart containing a single-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparing Numeric Values (cont'd.) Figure 4-15: Code entered in the Display button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparing Numeric Values (cont'd.) Figure 4-15: Code entered in the Display button’s Click event procedure (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparing Numeric Values (cont'd.) Block-level variables: declared within a statement block and remain in memory until the procedure ends Block scope: A block-scope variable can only be used within the statement block in which it was declared Concatenation operator (&): connects or links two strings together ControlChars.NewLine constant: Advances the insertion point to the next line Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparing Numeric Values (cont'd.) Figure 4-16: Illustration of the swapping concept Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparing Numeric Values (cont'd.) Figure 4-17: How to concatenate strings Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparing Strings Addition and Subtraction Calculator application: displays the sum or difference of two numbers Figure 4-18: Sample run of the Addition and Subtraction Calculator application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparing Strings (cont'd.) Figure 4-19: Pseudocode containing a dual-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 4-20: Flowchart containing a dual-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparing Strings (cont'd.) MaxLength property: text box property that specifies the maximum number of characters that can be entered CharacterCasing property: text box property that indicates if text should remain as typed or be converted to uppercase or lowercase Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 4-21: Calculate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

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 2010: Reloaded, Fourth Edition

The ToUpper and ToLower Methods (cont’d.) Figure 4-22: How to use the ToUpper and ToLower methods Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The ToUpper and ToLower Methods (cont’d.) Figure 4-22: How to use the ToUpper and ToLower methods (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The ToUpper and ToLower Methods (cont’d.) Figure 4-22: How to use the ToUpper and ToLower methods (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 4-23: Examples of using the ToUpper method in the calcButton Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 4-23: Examples of using the ToUpper method in the calcButton Click event procedure (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparing Boolean Values Check boxes: used to offer the user one or more independent and nonexclusive items from which to choose Figure 4-24: A different interface for the Addition and Subtraction Calculator application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparing Boolean Values (cont’d.) Figure 4-25: Click event procedures for the subtractionCheckBox and calcButton Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Comparing Boolean Values (cont’d.) Figure 4-25: Click event procedures for the subtractionCheckBox and calcButton (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Logical Operators Logical operators (or Boolean operators): Used to combine two or more conditions into one compound condition Compound condition: a combination of conditions using logical operator(s) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Logical Operators (cont'd.) Figure 4-26: How to use logical operators in a condition Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Logical Operators (cont'd.) Figure 4-26: How to use logical operators in a condition (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 4-26: How to use logical operators in a condition (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Logical Operators (cont'd.) Truth tables: used to evaluate logical operators in an expression Short-circuit evaluation: an evaluation in which the second condition may not be evaluated AndAlso evaluates to True only when both sub-conditions are True OrElse evaluates to False only when both sub-conditions are False AndAlso and OrElse operations do not evaluate the second condition if the first condition is false Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Logical Operators (cont'd.) Figure 4-27: Truth tables for the AndAlso and OrElse logical operators Microsoft Visual Basic 2010: Reloaded, Fourth Edition

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 Remember: logical operators are evaluated after arithmetic or comparison operators in an expression Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The Carroll Company Application Data validation: Process of verifying that the input data is within the expected range Figure 4-28: Two ways of writing the calcButton Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 4-28: Two ways of writing the calcButton Click event procedure (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The Carroll Company Application (cont'd.) Figure 4-29: Sample run of the Carroll Company application using valid data Figure 4-30: Sample run of the Carroll Company application using invalid data Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Summary of Operators Figure 4-31: Listing of arithmetic, concatenation, comparison, and logical operators Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Summary of Operators (cont’d.) Figure 4-31: Listing of arithmetic, concatenation, comparison, and logical operators (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Generating Random Integers Pseudo-random number generator: a device that produces a sequence of numbers that meets certain statistical requirements for randomness Random object: represents a pseudo-random number generator Random.Next method: Generates a random integer Can specify a minimum and maximum value Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 4-32: How to generate random integers Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Generating Random Integers (cont'd.) Figure 4-33: Sample run of the Random Integers application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Generating Random Integers (cont'd.) Figure 4-34: Generate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Programming Tutorial 1 Figure 4-36: MainForm for the Find the Mouse Game application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Programming Tutorial 2 Figure 4-44: MainForm for the Greenview Health Club application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Programming Example Figure 4-50: MainForm in the Fat Calculator application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Summary Selection structure allows a procedure to make a decision and then take the appropriate action Three types of selection structures: single-alternative, dual-alternative, and multiple-alternative Diamond symbol represents a decision in a flowchart Expressions with comparison operators will result in an answer of True or False Comparison operators are evaluated from left to right in expressions, after arithmetic operators Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Summary (cont'd.) Variables declared within a selection expression have block-level scope Concatenation: connecting or linking two strings together with the concatenation operator (&) ControlChars.Newline advances the insertion point to the next line in a control String comparisons are case-sensitive Use ToUpper and ToLower methods to temporarily convert the case of a string Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Summary (cont'd.) Use check boxes to provide the user with one or more independent and nonexclusive choices Use logical operators to create compound conditions An expression containing a logical operator will evaluate to either True or False Logical operators have an order of precedence and are evaluated after arithmetic and comparison operators Use the pseudo-random number generator to generate random numbers Microsoft Visual Basic 2010: Reloaded, Fourth Edition