Visual C# 2005 Decision Structures. Visual C# 20052 Objectives Understand decision making Learn how to make decisions using the if statement Learn how.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Chapter 5– Making Decisions Dr. Jim Burns. In Java, the simplest statement.. Is the if(..) statement if(someVariable==10) System.out.println(“The value.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
C++ for Engineers and Scientists Third Edition
CIS162AD - C# Decision Statements 04_decisions.ppt.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming Logic and Design Fourth Edition, Introductory
Programming Logic and Design Sixth Edition
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 3 Making Decisions
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Java Programming, Second Edition Chapter Five Input and Selection.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Programming Logic and Design, Second Edition, Comprehensive
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Chapter 5: Making Decisions
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.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Week 4 Program Control Structure
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Decision Statements, Short- Circuit Evaluation, Errors.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
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)
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Branching statements.
Java Programming Fifth Edition
More on the Selection Structure
Selections Java.
CSC113: Computer Programming (Theory = 03, Lab = 01)
JavaScript: Control Statements.
Microsoft Visual Basic 2005 BASICS
Structured Program
Objectives After studying this chapter, you should be able to:
Chapter 8: More on the Repetition Structure
Boolean Expressions to Make Comparisons
Week 3 – Program Control Structure
PROGRAM FLOWCHART Selection Statements.
Using C++ Arithmetic Operators and Control Structures
Presentation transcript:

Visual C# 2005 Decision Structures

Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how to make decisions using the if-else statement Use compound expressions in if statements

Visual C# Objectives (continued) Make decisions using the switch statement Use the conditional operator Use the NOT operator Learn to avoid common errors when making decisions

Visual C# Understanding Decision Making Pseudocode –Tool that helps programmers plan a program’s logic by writing plain English statements Flowchart –You write the steps in diagram form, as a series of shapes connected by arrows Sequence structure –One step follows another unconditionally –Sometimes, logical steps do not follow in an unconditional sequence

Visual C# Understanding Decision Making (continued)

Visual C# Understanding Decision Making (continued) Decision structure –Involves choosing between alternative courses of action based on some value within a program –All computer decisions are yes-or-no decisions When reduced to their most basic form

Visual C# Understanding Decision Making (continued)

Visual C# Making Decisions Using the if Statement If statement –Used to make a single-alternative decision Block –One or more statements contained within a pair of curly braces Nested if –One decision structure is contained within another

Visual C# Making Decisions Using the if Statement (continued)

Visual C# Making Decisions Using the if Statement (continued)

Visual C# Making Decisions Using the if Statement (continued)

Visual C# Making Decisions Using the if-else Statement Dual-alternative decisions –Have two possible outcomes If-else statement –Used to perform one action when a Boolean expression evaluates as true And an alternate action when it evaluates as false

Visual C# Making Decisions Using the if-else Statement (continued)

Visual C# Using Compound Expressions in if Statements You can combine multiple decisions into a single if statement –Using a combination of AND and OR operators

Visual C# Using the Conditional AND Operator Conditional AND operator –Determines whether two expressions are both true –Written as two ampersands ( && ) –You must include a complete Boolean expression on each side of the operator Short-circuit evaluation –Expressions in each part of an AND expression are evaluated only as much as necessary To determine whether the entire expression is true or false

Visual C# Using the Conditional AND Operator (continued)

Visual C# Using the Conditional OR Operator Conditional OR operator –Used when you want some action to occur even if only one of two conditions is true –Written as ||

Visual C# Using the Conditional OR Operator (continued)

Visual C# Using the Logical AND and OR Operators Boolean logical AND ( & ) and Boolean logical inclusive OR ( | ) operators –Work just like their && and || (conditional AND and OR) counterparts –They do not support short-circuit evaluation –Can lead to a side effect (unintended consequence) Avoid writing expressions that contain side effects

Visual C# Combining AND and OR Operators

Visual C# Making Decisions Using the switch Statement switch structure –Tests a single variable against a series of exact matches Keywords –switch, case, break, and default “No fall through rule” –Not allowing code to reach the end of a case –Not allowed in C# –Use a break statement at the end of each case

Visual C# Making Decisions Using the switch Statement (continued)

Visual C# Making Decisions Using the switch Statement (continued)

Visual C# Making Decisions Using the switch Statement (continued) A switch does not need a default case –It is good programming practice You can use multiple labels to govern a list of statements

Visual C# Making Decisions Using the switch Statement (continued)

Visual C# Using the Conditional Operator Conditional operator –Used as an abbreviated version of the if-else statement –A ternary operator Syntax testExpression ? trueResult : falseResult;

Visual C# Using the NOT Operator NOT operator –Written as an exclamation point ( ! ) –Negates the result of any Boolean expression

Visual C# Avoiding Common Errors When Making Decisions Most frequent errors include: –Using the assignment operator instead of the comparison operator –Inserting a semicolon after the Boolean expression in an if statement –Failing to block a set of statements with curly braces –Failing to include a complete Boolean expression on each side of an && or || operator

Visual C# Performing Accurate and Efficient Range Checks Range check –A series of if statements that determine whether a value falls within a specified range Problem if(saleAmount >= 1000) commissionRate = 0.08; if(saleAmount >= 500) commissionRate = 0.06; if(saleAmount <= 499) commissionRate = 0.05;

Visual C# Performing Accurate and Efficient Range Checks (continued) Solution if(saleAmount >= 1000) commissionRate = 0.08; else if(saleAmount >= 500) commissionRate = 0.06; else commissionRate = 0.05;

Visual C# Using AND and OR Appropriately Problem –Print an error message when an employee’s hourly pay rate is under $5.65 and when an employee’s hourly pay rate is over $60 Solution if(payRate 60) Console.WriteLine (“Error in pay rate”);

Visual C# Using NOT Correctly Problem –Make sure if the sales code is not ‘A’ or ‘B’, the customer gets a 10% discount Solutions if(salesCode != ‘A’ && salesCode != ‘B’) discount = 0.10; if(!(salesCode == ‘A’ || salesCode == ‘B’)) discount = 0.10;

Visual C# You Do It Activities to explore –Using if-else statements –Using AND and OR logic

Visual C# Summary A flowchart is a pictorial tool that helps you understand a program’s logic You use an if statement to make a single- alternative decision When you make a dual-alternative decision, you can use an if-else statement Conditional AND operator determines whether two expressions are both true Use the conditional OR operator when you want some action to occur when one or both of two conditions are true

Visual C# Summary (continued) AND operators take precedence The switch statement tests a single variable against a series of exact matches The conditional operator is used as an abbreviated version of the if-else statement You use the NOT operator to negate the result of any Boolean expression Common errors when making decisions