Chapter 4: Control Structures: Selection

Slides:



Advertisements
Similar presentations
Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
 Control structures  Algorithm & flowchart  If statements  While statements.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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)
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Lesson Objective: I can…
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.
Decision Structures and Boolean Logic
Chapter 4: The Selection Structure
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Selection Structures (if & switch statements) (CS1123)
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
Problem Solving with Decisions
Programming Logic and Design, Second Edition, Comprehensive
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
1 © 2002 John Urrutia. All rights reserved. Qbasic Chapter 4 IF Statements and READ & DATA.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Tutorial 4: The Selection Structure 1 Tutorial 4 The Selection Structure.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
CSIT 108 Review Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 3: Decisions and Loops
Topics The if Statement The if-else Statement Comparing Strings
Microsoft Visual Basic 2005 BASICS
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
2-1 Making Decisions Sample assignment statements
Structured Program
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

Chapter 4: Control Structures: Selection Visual Basic .NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design Objectives Write and interpret logical expressions Write one-way selection statements Write two-way selection statements Write multi-way selection statements Visual Basic .NET Programming: From Problem Analysis to Program Design

Writing And Interpreting Logical Expressions Make decisions in selection statement by writing logical expressions Logical expression Specifies condition that evaluates to: True Or False Use to compare two values Visual Basic .NET Programming: From Problem Analysis to Program Design

Using the VB .NET Relational Operators Use to make comparison in logical expression Visual Basic .NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design

Example 4-1: Using the >= relational operator examScore Integer variable Contains value 86 Expression: examScore >= 90 Is value contained in exam-score greater than or equal to 90? Answer: False Visual Basic .NET Programming: From Problem Analysis to Program Design

Using the VB .NET Logical Operators Use to combine logical expressions Frequently used logical operators: Not Negates an expression And Joins two expressions Visual Basic .NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design

Using the VB .NET Logical Operators And operator Joins two expressions Forms compound expression If both expressions evaluate to true Then compound expression is true Otherwise, it is false Visual Basic .NET Programming: From Problem Analysis to Program Design

Example 4-4: Using the And logical operator Variables: examScore Integer Value: 86 engineeringStudent Boolean Value: True Visual Basic .NET Programming: From Problem Analysis to Program Design

Example 4-4: Using the And logical operator Expression: examScore >= 90 And engineeringStudent First expression (examScore >= 90) Evaluates to false Second (engineeringStudent) Evaluates to true Result is false Visual Basic .NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design

Using the VB .NET Logical Operators Or operator Joins two expressions Returns true if either or both expressions are true Xor operator Returns true if one and only one expression is true Otherwise, returns false Visual Basic .NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design

Using the VB .NET Logical Operators AndAlso, OrElse Correspond to And and Or Employ short-circuit evaluation technique Visual Basic .NET Programming: From Problem Analysis to Program Design

Example 4-8: Using the OrElse logical operator Statement: 1 < 2 OrElse 2 < 3 If first expression is true Compound expression true Eliminates need to evaluate second expression Visual Basic .NET Programming: From Problem Analysis to Program Design

Writing One-way Selection Statements Evaluates logical expression Executes statements only if expression is true Two-way selection statement: Executes statements if it is true Executes different statements if it is false Visual Basic .NET Programming: From Problem Analysis to Program Design

Writing One-way Selection Statements Flowchart Graphical representation of logic Use symbols to represent logical components of algorithm Symbols: Diamond Rectangle Circle Flow lines Visual Basic .NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design

Writing One-way Selection Statements Single-line If syntax: If (logical expression) Then statement Multi-line If syntax: If (logical expression) Then statement . End If Visual Basic .NET Programming: From Problem Analysis to Program Design

Writing One-way Selection Statements Multi-line if: Statements written on separate lines Keyword End If must be used to terminate If statement Visual Basic .NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design

Writing Two-way Selection Statements Write two-way selection statement When you want to execute one or more statements if logical expression is true But also want to execute one or more different statements if it is false Nested If If statement written inside another If statement Can replace compound expression with nested If Visual Basic .NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design

Writing Two-way Selection Statements Syntax: If (logical expression) Then statement(s) Else End If ElseIf Combines Else and If Visual Basic .NET Programming: From Problem Analysis to Program Design

Example 4-17: Determining a grade using ElseIf statements 1. If examScore >= 90 Then 2. grade = “A” 3. ElseIf examScore >= 80 Then 4. grade = “B” 5. ElseIf examScore >= 70 Then 6. grade = “C” 7. ElseIf examScore >= 60 Then 8. grade = “D” 9. Else grade = “F” 10. End If Visual Basic .NET Programming: From Problem Analysis to Program Design

Writing Multi-way Selection Statements Acts like multi-way If statement By transferring control to one or more statements Depending on value of a variable Sometimes called case structure Uses keywords: Select Case Visual Basic .NET Programming: From Problem Analysis to Program Design

Example 4-18: Determine a Grade Using Select Case Statements 1. Select Case examScore 2. Case Is >= 90 3. grade = “A” 4. Case 80 To 89 5. grade = “B” 6. Case 70 To 79 7. grade = “C” … Visual Basic .NET Programming: From Problem Analysis to Program Design

Example 4-18: Determine a Grade Using Select Case Statements … 8. Case 60 To 69 9. grade = “D” 10. Case Else 11. grade = “F” 12. End Select Visual Basic .NET Programming: From Problem Analysis to Program Design

Programming Example: Payroll Calculation Input Overtime exempt “Y” or “N” Hours worked Hourly pay rate Output The employee’s Regular pay Overtime pay Total pay Visual Basic .NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design Summary Make decisions in selection statement by writing logical expression Evaluates to either true or false Logical operators join two logical expressions to form compound expression One-way selection statement Evaluates logical expression Executes one or more statements only if expression is true Visual Basic .NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design Summary (continued) Two-way selection statement Evaluates logical expression Executes one or more statements if it is true Executes one or more different statements if it is false One-way selection: One-line and multi-line If statements Visual Basic .NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design Summary (continued) Two-way selection: If and Else statements Multi-way selection structure Keywords Select Case Visual Basic .NET Programming: From Problem Analysis to Program Design