Microsoft Visual Basic 2005: Reloaded

Similar documents
Skill Area 306: Develop and Implement Computer Program

Microsoft Visual Basic 2012: Reloaded

Microsoft Visual Basic 2015: Reloaded

Lecture 5 Tao Wang 1

Chapter 4: Making Decisions

Chapter 4: Making Decisions

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

LECTURE 04 MAKING DECISIONS

Programming Logic and Design Sixth Edition

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

PLD Semester Exam Study Guide Dec. 2018

Chapter 4: Making Decisions


Computational Expression

Microsoft Visual Basic 2005: Reloaded

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Introduction to Programming

Structured Program Development

BIL101E: Introduction to Computers and Information systems Lecture 8

An overview about DroidBasic For Android

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Unit 3. Operators. School of Science and Technology INTRODUCTION

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 3: Program Statements

SMS 3515: Scientific Computing. Sem /2015

An Introduction to Programming with C++ Sixth Edition. Chapter 8 More on the Repetition Structure

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Chapter 4 The If Then Statement

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Introduction to C Programming

CT 229 Java Syntax Continued

Chapter 4: Control structures. Repetition

Microsoft Visual Basic 2015: Reloaded

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

LECTURE 5 Control Structures Part 2

Introduction. C provides two styles of flow control:

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

Decision Making in C

Chapter 4: Control structures

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

FRAC: Language Reference Manual

Using Boolean Expressions. Multiway Branches. More about C++ Loop Statements. Designing Loops. In this chapter, you will learn about:

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

Theory of control structures

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

ARG! Language Reference Manual

Individual research task. You should all have completed the research task set last week. Please make sure you hand it in today.

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

SFPL Reference Manual

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Conditionals and Loops

Test #2 October 8, 2015

Logical Operators and switch

CS110D: PROGRAMMING LANGUAGE I

Overview About KBasic

Decisions. Arizona State University 1

LCSL Reference Manual

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Introduction to Programming Using Java (98-388)

Chapter 4: Basic C Operators

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Chapter 2 Working with Data Types and Operators

C-LANGUAGE CURRICULAM

STUDENT LESSON A12 Iterations

Chapter 3 Structured Program Development

Microsoft Visual Basic 2015: Reloaded

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Subquery: There are basically three types of subqueries are:

Control Structures: if and while A C S L E C T U R E 4

Decision Structures. Start. Do I have a test in morning? Study for test. Watch TV tonight. Stop

Looping. Arizona State University 1

Lab 4: Decisions and Boolean Logic This lab accompanies Chapter 4 of Starting Out with Programming Logic & Design.

The PCAT Programming Language Reference Manual

Darrell Bethea May 25, 2011

Logical Operators and if/else statement. If Statement. If/Else (4.3)

5. Control Statements

Petros: A Multi-purpose Text File Manipulation Language

Euclid s algorithm, 133

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Programming for Engineers Iteration

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

The SPL Programming Language Reference Manual

Transcription:

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 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 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 Edition 2

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 Edition 3

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

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

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 Edition 7

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

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 Edition 9

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

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

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

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 Edition 13

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 Edition 14

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

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 Edition 16

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

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

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

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 Edition 20

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

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

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

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

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

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 Edition 26

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

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

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

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

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 Edition 31

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

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

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

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 Edition 35

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

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

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 Edition 38

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

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

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 Edition 41

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

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

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 Edition 44

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

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

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

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 Edition 48

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

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

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

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

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

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 Edition 54

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

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

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

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

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

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

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 Edition 61

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

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

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

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

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

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

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

The If/ElseIf/Else Selection Structure Need a procedure to display a message based on a letter grade: Letter grade A B C D F Message Excellent Above Average Average Below Average Below Average Microsoft Visual Basic 2005: Reloaded, Second Edition 69

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

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

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 Edition 72

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

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 Edition 74

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

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

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 Edition 77

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

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 Edition 79

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

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

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

Programming Tutorial Microsoft Visual Basic 2005: Reloaded, Second Edition 83

Programming Example Microsoft Visual Basic 2005: Reloaded, Second Edition 84

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 Edition 85

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 Edition 86

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