Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

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.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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
Visual C++ Programming: Concepts and Projects
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Decision Structures and Boolean Logic
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Control Flow Statements
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
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)
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
If/Else Statements.
Java Programming Fifth Edition
Chapter 4: Making Decisions.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
Engineering Problem Solving with C++, Etter/Ingber
Chapter 4: Making Decisions.
JavaScript: Control Statements I
Topics The if Statement The if-else Statement Comparing Strings
Control Structures: Selection Statement
Chapter 4: Control Structures I (Selection)
The System.exit() Method
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Control Structures: Selection Statement
Presentation transcript:

Chapter 5 – Decision Making

Structured Programming Sequence Selection Repetition yesno yes no

Decision Statements (also known as Selection Statements) How to compare data values? Relational operators How to alter the sequence of program execution based on the result? if.. else statements How to deal with multiple choices? Switch statement

Example A teacher wants to assign letter grade based on percentage points. Percent range and the corresponding letter grade are as shown in the table below. Design a C++ solution (program) for this problem. Percent Range >=90>=80 < 90 >=70 <80>=60 <70<50 Letter Grade ABCDF

Relational Operators Operator Meaning < Less than ? > Greater than ? >= Greater than or equal to? <= Less than or equal to? == Equal to? != Not Equal to ? We will use relational operators to form relational expression of conditions.

Logical Operators !not &&and ||or These operators are used to combine more than one condition forming a complex condition.

Three Logical Operators Exclamation mark ! –NOT(negation) –unary Two ampersands && –AND(conjunction) –binary Two vertical pipes || –OR(inclusive disjunction) –binary Lesson 5.4

Logical NOT Operator Reverses result of relational expression Example: ! (x == y) Lesson 5.4 Evaluate relational expression, does 3 equal 7? 3 x 7 y ! ( false ) so negates to true 7 7 ! ( true ) so negates to false

Logical AND Operator Combines two relational expressions Combined expression true only if BOTH expressions are true Lesson 5.4 expression1 && expression2 true false false && true false && false true && true false true

Logical OR Operator Combines two relational expressions Combined expression false only if BOTH expressions are false Lesson 5.4 expression1 || expression2 true false true false || true false || false true || true true false true

Example: Assume: a = 4, b = -2, and c = 0 x = (a > b || b > c && a == b) x = ((a > b) || (b > c) && (a == b)) x = (TRUE || (FALSE && FALSE)) Group x = ((4 > -2) || (-2 > 0) && (4 == -2)) Group x = (TRUE || FALSE) x = (TRUE) x = 1 Lesson 5.5

if Statement An if statement allows a program to choose whether or not to execute a following statement. Syntax: (structure/format) if (condition) statement; Semantics: (meaning) condition is a Boolean expression: Something that evaluates to True or False. If condition is true then execute the statement.

The if statement : syntax if(expression) statement; //single statement executed //if expression is true if(expression) { //statements inside {} are //executed if expression is true statement1; statement2; … statement n; }

If -else Statement An if-else statement allows a program to do one thing if a condition is true and a different thing if the condition is false. Syntax: if ( condition ) statement1 else statement2 Statements to be executed for if and else can be a single statement or multiple statements enclosed in { }.

The if - else statement: syntax if(expression) statement; else statement; if(expression) { statement block } else { statement block }

Simple If/Else Statement Provides block of statements to be executed for either true OR false Braces optional if only one statement in block Lesson 5.2 if (expression) { statement1a; statement1b; … } else { statement1a; statement1b; … } if TRUEif FALSE

if Examples x = 3; if (x < 2) { cout << “x is smaller than 2”; } if (x < 12) cout << “smaller than twelve” else cout << “twelve or larger”; Simple if – statements only done on true, but when false, skips to end of if Full if – code for when true and also when false True False

if-else-if Form Lesson 5.6 if (relational_expression_1) { statement_block_1 } else if (relational_expression_2) { statement_block_2 }. else if (relational_expression_n-1) { statement_block_n-1 } else { statement_block_n } false else false else true This statement block would be executed!

The switch statement switch(expression) { case constant: statement(s); break; case constant: statement(s); break; /* default is optional*/ default: statement(s); }

The switch statement Expression must be of type integer or character The keyword case must be followed by a constant break statement is required unless you want all subsequent statements to be executed.

Practice! Convert these nested if/else statements to a switch statement : if (rank==1 || rank==2) cout << "Lower division \n"; else { if (rank==3 || rank==4) cout << "Upper division \n"; else { if (rank==5) cout << "Graduate student \n"; else cout << "Invalid rank \n"; }

Practice Solution! switch(rank) { case 1: case 2: cout << "Lower division \n"; break; case 3: case 4: cout << "Upper division \n"; break; case 5: cout << "Graduate student \n"; break; default: cout << "Invalid rank \n"; }//end switch

Summary In many applications, choices are to be made depending on some conditions related to the problem. Selection or decision structures are used to model such situations. C++ supports the implementation of “selection” through the “if” and “switch” statements. In this discussion we looked at various forms of selection statements.