Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.

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.
Programming Logic and Design Eighth Edition
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
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.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Programming Logic and Design Fourth Edition, Introductory
Programming Logic and Design Sixth Edition
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.
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.
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 (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Programming Logic and Design, Second Edition, Comprehensive
Programming Logic and Design Fifth Edition, Comprehensive
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.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Chapter 5: Making Decisions
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
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.
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.
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.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
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)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
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
CSC113: Computer Programming (Theory = 03, Lab = 01)
JavaScript: Control Statements.
Microsoft Visual Basic 2005 BASICS
Making Decisions in a Program
Structured Program
Objectives After studying this chapter, you should be able to:
Chapter 8: More on the Repetition Structure
Microsoft Visual Basic 2005: Reloaded Second Edition
Boolean Expressions to Make Comparisons
PROGRAM FLOWCHART Selection Statements.
Presentation transcript:

Chapter 4: Making Decisions

Understanding Logic-Planning Tools and Decision Making Pseudocode – A 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 2Microsoft Visual C# 2012, Fifth Edition

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 3Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the if Statement (cont’d.) 4Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the if Statement (cont’d.) 5Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the if Statement (cont’d.) 6Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the if Statement (cont’d.) 7Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the if Statement (cont’d.) Nested if – One decision structure, or if statement, is contained within another – Decision structures can be nested to multiple levels – If an outer level if statement fails or returns a false result, all inner blocks of code are ignored – Creating too many levels can result in code that is difficult to understand and maintain 8Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the if Statement (cont’d.) 9Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the if Statement (cont’d.) 10Microsoft Visual C# 2012, Fifth Edition

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 11Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the if-else Statement (cont’d.) 12Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the if-else Statement (cont’d.) 13Microsoft Visual C# 2012, Fifth Edition

Using Compound Expressions in if Statements You can combine multiple decisions into a single if statement – Use a combination of AND and OR operators – This is an alternative to a nested if in some circumstances 14Microsoft Visual C# 2012, Fifth Edition

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 15Microsoft Visual C# 2012, Fifth Edition

Using the Conditional AND Operator (cont’d) 16Microsoft Visual C# 2012, Fifth Edition

17Microsoft Visual C# 2012, Fifth Edition Using the Conditional AND Operator (cont’d)

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 two pipes || – You must include a complete Boolean expression on each side of the operator 18Microsoft Visual C# 2012, Fifth Edition

Using the Conditional OR Operator (cont’d) 19Microsoft Visual C# 2012, Fifth Edition

20Microsoft Visual C# 2012, Fifth Edition Using the Conditional OR Operator (cont’d)

Combining AND and OR Operators 21Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the switch Statement switch structure – Tests a single variable against a series of exact matches Keywords – switch, case, break, and default A switch does not need a default case – Good programming practice to include one 22Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the switch Statement (cont’d.) “No fall through rule” – Not allowing code to reach the end of a case – Use a break statement at the end of each case 23Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the switch Statement (cont’d.) 24Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the switch Statement (cont’d.) 25Microsoft Visual C# 2012, Fifth Edition

Making Decisions Using the switch Statement (cont’d.) You can use multiple labels to govern a list of statements 26Microsoft Visual C# 2012, Fifth Edition

Using the Conditional Operator Conditional operator – Used as an abbreviated version of the if-else statement – A ternary operator that takes three parameters Syntax – testExpression ? trueResult : falseResult; Example – Console.WriteLine((testScore >= 60) ? " Pass " : " Fail " ); 27Microsoft Visual C# 2012, Fifth Edition

Using the NOT Operator NOT operator – Written as an exclamation point ( ! ) – Negates the result of any Boolean expression If the Boolean expression is true, ! makes it false If the Boolean expression is false, ! makes it true – Logic using the ! operator can be difficult to read and analyze – The ! operator has a higher precedence than && and || 28Microsoft Visual C# 2012, Fifth Edition

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 29Microsoft Visual C# 2012, Fifth Edition

Performing Accurate and Efficient Range Checks Range check – A series of if statements that determine whether a value falls within a specified range Problem 30Microsoft Visual C# 2012, Fifth Edition

Performing Accurate and Efficient Range Checks (cont’d.) Solution if(saleAmount >= 1000) commissionRate = 0.08; else if(saleAmount >= 500) commissionRate = 0.06; else commissionRate = 0.05; 31Microsoft Visual C# 2012, Fifth Edition

Using && and || Appropriately Problem – Print an error message when an employee’s hourly pay rate is less than $5.65 and when an employee’s hourly pay rate is greater than $60 Solution if(payRate 60) Console.WriteLine ("Error in pay rate"); 32Microsoft Visual C# 2012, Fifth Edition

Using the ! Operator Correctly Problem – Make sure that 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; 33Microsoft Visual C# 2012, Fifth Edition

Decision-Making Issues in GUI Programs Making a decision within a method in a GUI application is no different from making one in a console application You can use if, if…else, and switch statements in the same ways GUI programs use controls to make decisions – The user clicks on a Button, which causes an event to fire – The user chooses one of several RadioButtons – The user chooses an item from a ListBox 34Microsoft Visual C# 2012, Fifth Edition

Decision-Making Issues in GUI Programs (cont’d.) 35Microsoft Visual C# 2012, Fifth Edition

Decision-Making Issues in GUI Programs (cont’d.) 36Microsoft Visual C# 2012, Fifth Edition