Chapter 4: Making Decisions

Similar documents
Decision Making in C

Flow Control. CSC215 Lecture

ControlStructuresI:Selection

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

Lecture 5 Tao Wang 1

Chapter 2: Input, Processing, and Output

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Microsoft Visual Basic 2005: Reloaded

Making Decisions. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

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

Programming Logic and Design Sixth Edition

Introduction. C provides two styles of flow control:

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

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

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Microsoft Visual Basic 2015: Reloaded

Lesson 04. Control Structures I : Decision Making. MIT 31043, VISUAL PROGRAMMING By: S. Sabraz Nawaz

IDENTIFY WAYS OF REPRESENTING ALGORITHMS.

(4-2) Selection Structures in C H&K Chapter 4. Instructor - Andrew S. O Fallon CptS 121 (September 12, 2018) Washington State University

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Quiz 1: Functions and Procedures

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Chapter 6: Using Arrays

In this chapter, you will:

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

Introduction to C/C++ Lecture 3 - Program Flow Control

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Introduction to C Programming

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

Making Decisions Chp. 5

8. Control statements

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

ARG! Language Reference Manual

MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1. March 28-30, 2007

5. Selection: If and Switch Controls

Lecture 7 Tao Wang 1

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

Microsoft Visual Basic 2012: Reloaded

Software Design & Programming I

DECISION MAKING STATEMENTS

Course Outline. Introduction to java

BRANCHING if-else statements

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Chapter 3: Decision Structures

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

STUDENT LESSON A12 Iterations

Index COPYRIGHTED MATERIAL

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Chapter 3: Decision Structures

Introduction to Programming

Recognize the correct ordering of decisions in multiple branches Program simple and complex decision

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

Chapter 3: Decision Structures

CSE 114 Computer Science I

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

3 The L oop Control Structure

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

Following is the general form of a typical decision making structure found in most of the programming languages:

Chapter 3 Structured Program Development

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Unit 1 Lesson 4. Introduction to Control Statements

Unit 3. Operators. School of Science and Technology INTRODUCTION

Oct Decision Structures cont d

Information Science 1

Computer Science & Engineering 150A Problem Solving Using Computers

Information Science 1

CS302: Self Check Quiz 2

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

LAB 5: SELECTION STATEMENTS

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Chapter 4. Flow of Control

Introduction to Java & Fundamental Data Types

Object-Oriented Programming in Java

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

Computational Expression

Chapter 8 Statement-Level Control Structures

Chapter Goals. 3.1 The if Statement. Contents 1/30/2013 DECISIONS

FRAC: Language Reference Manual

Problem Solving and Algorithms

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Programming Lecture 4

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

Structured Program Development

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 3 Selection Statements

Quiz Determine the output of the following program:

Typescript on LLVM Language Reference Manual

PLD Semester Exam Study Guide Dec. 2018

Transcription:

Chapter 4: Making Decisions

Understanding Logic-Planning Tools and Pseudocode Decision Making 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 2

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 3

Making Decisions Using the if Statement (cont d.) 4

Making Decisions Using the if Statement (cont d.) 5

Making Decisions Using the if Statement (cont d.) 6

Making Decisions Using the if Statement (cont d.) 7

Making Decisions Using the if Statement Nested if (cont d.) 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 8

Making Decisions Using the if Statement (cont d.) 9

Making Decisions Using the if Statement (cont d.) 10

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 11

Making Decisions Using the if-else Statement (cont d.) 12

Making Decisions Using the if-else Statement (cont d.) 13

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 14

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 15

Using the Conditional AND Operator (cont d) 16

Using the Conditional AND Operator (cont d) 17

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 18

Using the Conditional OR Operator (cont d) 19

Using the Conditional OR Operator (cont d) 20

Combining AND and OR Operators 21

Making Decisions Using the switch switch structure Statement 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 22

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 23

Making Decisions Using the switch Statement (cont d.) 24

Making Decisions Using the switch Statement (cont d.) 25

Making Decisions Using the switch Statement (cont d.) You can use multiple labels to govern a list of statements 26

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"); 27

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 28

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 29

Performing Accurate and Efficient Range Checks Range check A series of if statements that determine whether a value falls within a specified range Problem 30

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

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 < 5.65 payrate > 60) Console.WriteLine ("Error in pay rate"); 32

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; 33

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 34

Decision-Making Issues in GUI Programs (cont d.) 35

Decision-Making Issues in GUI Programs (cont d.) 36