1.

Slides:



Advertisements
Similar presentations
Programming with Microsoft Visual Basic th Edition
Advertisements

Chapter 3: Using Variables and Constants
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
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.
1.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
1.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Chapter Four The Selection Structure
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.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Working with option button, check box, and list box controls Visual Basic for Applications 13.
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.
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.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
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.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Four The Selection Structure.
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.
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.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
Chapter 4: Decisions and Conditions
More on the Selection Structure
Chapter 4 The If…Then Statement
The Selection Structure
Chapter 4: The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Programming with Microsoft Visual Basic 2008 Fourth Edition
An Introduction to Programming with C++ Fifth Edition
Making Decisions in a Program
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:
CIS 16 Application Development Programming with Visual Basic
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 5: The Selection Structure
Presentation transcript:

1

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 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 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 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) 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 Microsoft Visual Basic .NET: Reloaded

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

Coding the If and If/Else Selection Structures 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 Microsoft Visual Basic .NET: Reloaded

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

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

Using Comparison Operators – Example 1 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 Microsoft Visual Basic .NET: Reloaded

Using Comparison Operators – Example 1 (continued) 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 Microsoft Visual Basic .NET: Reloaded

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

HOW TO… Microsoft Visual Basic .NET: Reloaded

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

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

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

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

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

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

Logical Operators Microsoft Visual Basic .NET: Reloaded

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

Truth Tables 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 Microsoft Visual Basic .NET: Reloaded

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

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

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

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

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

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

The IsNumeric 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 Microsoft Visual Basic .NET: Reloaded

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

The MessageBox.Show method 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 Microsoft Visual Basic .NET: Reloaded

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

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

HOW TO… 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 Microsoft Visual Basic .NET: Reloaded

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

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

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

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

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

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

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

HOW TO… Microsoft Visual Basic .NET: Reloaded

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

Using To and Is in an ExpressionList (continued) 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 Microsoft Visual Basic .NET: Reloaded

TOE Chart Microsoft Visual Basic .NET: Reloaded

User Interface Microsoft Visual Basic .NET: Reloaded

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

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

Tab Order 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 display “The calories and fat grams must be numbers” Microsoft Visual Basic .NET: Reloaded

Code Microsoft Visual Basic .NET: Reloaded

Code (continued) 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 If If/Else If/ElseIf/Else Case Selection structures can be “nested” within other selection structures Outer structure contains primary decision Inner structure contains secondary decision 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 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 Microsoft Visual Basic .NET: Reloaded