Chapter 4, cont: Control Structures if else nested if switch.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Chapter 4: Making Decisions.
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 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
C++ for Engineers and Scientists Third Edition
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Fundamental Programming Fundamental Programming More on Selection.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions Starting Out with C++ Early Objects Seventh Edition.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Control Structures Selection or Decision or Branching.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Control Structures RepetitionorIterationorLooping Part I.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
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.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
A First Book of C++ Chapter 4 Selection.
Branching statements.
Chapter 3 Control Statements
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.
Decisions Given hours worked and pay rate, calculate total pay
Chapter 2.1 Control Structures (Selection)
Chapter 7 Conditional Statements
Chapter 4: Control Structures I (Selection)
Control Structures Selection or Decision Branching.
Control Statements Paritosh Srivastava.
Branching statements Kingdom of Saudi Arabia
Presentation transcript:

Chapter 4, cont: Control Structures if else nested if switch

Conditional The Conditional Statement Syntax if (expression) statement1 else statement2 if (a > b) max = a; else max = b; * * * Use block or compound statements for either or both

Conditional The Conditional Statement if (a > b) max = a; else max = b; end of statement for if clause else => not done with the if yet end of statement for else clause and end of the entire if statement if (expression) tells compiler to look for a complete statement single (semicolon) or block {braces} Then look for else – if an else is not found, continue with next statement If else is found, this is the other alternative to the last if - So, look for the required statements

Finding the Minimum of Three Values * * * int x, y, z, min; cout << “Input three integers: “; cin >> x >> y >> z; if (x < y) min = x; else min = y; if (z < min) min = z; cout << “The minimum value is “ << min << ‘\n’;

Output: Input three integers: Interactive Program Finding the minimum of three values * The minimum value is -12 _

if Nested if Statements A nested if statement is an if statement that is included within another if statement. Syntax if (expression1) { if (expression2) statement } If (a < b) If a > 3 cout << a; Braces are essential

if Nested if Example cout <<“Enter guess”; cin >> number if (number != secretnumber) { cout << “Sorry, that’s not the number.\n”; if (number > secretnumber) cout << “You guessed too high.\n”; else cout << “You guessed too low.\n”; } *

2 statement1 3 else if (expression2) 4 statement else if (expressionN) 7 statementN 8 else 9 last statement 10 next statement * if...else Chained if...else Example Syntax 1 if (expression1)

if...else Chained if...else Example if (monthlySales >= 50000) income = * monthlySales; else if (monthlySales >= 40000) income = * monthlySales; else if (monthlySales >= 30000) income = * monthlySales; else if (monthlySales >= 20000) income = * monthlySales; else if (monthlySales >= 10000) income = * monthlySales; else income = * monthlySales ; next statement *

switch The switch Statement « Similar to cascading if statements « Can list any number of branches « Used in place of nested if statements « Avoids confusion of deeply nested ifs « For cases that compare the value of an integer expression to a specific value

switch The switch Statement Syntax switch (expression) { case value1: statement1; break; case value2: statement2; break;  case valueN: statementn; break; default: statement; } no ; use : * case value2: case value3: statement2; break; Multiple cases can have same action.

switch The switch Statement Syntax switch (expression) { case value1: statement1; break; case value2: statement2; break;  case valuen: statementn; break; default: statement; } no ; use : If none of the cases matched…

switch The switch Statement switch (letterGrade) { case ‘A’: cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; break; case ‘C’: cout << “Grade is between 70 & 79”; break; cont.

switch The switch Statement case ‘D’: cout << “Grade is between 60 & 69”; break; case ‘E’: cout << “Grade is between 0 & 59”; break; default: cout << “You entered an invalid grade.”; } next statement

switch The switch Statement switch (letterGrade) { case ‘A’: cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; break; case ‘C’: cout << “Grade is between 70 & 79”; break; case ‘D’: cout << “Grade is between 60 & 69”; break; case ‘E’: cout << “Grade is between 0 & 59”; break; default: cout << “You entered an invalid grade.”; } Mutually exclusive, ordinal values

break The break Statement switch (letterGrade) { case ‘A’: cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; break; case ‘C’: cout << “Grade is between 70 & 79”; break; case ‘D’: cout << “Grade is between 60 & 69”; break; case ‘E’: cout << “Grade is between 0 & 59”; break; default: cout << “You entered an invalid grade.”; } Assume letterGrade = ‘D’ Print this  Then this 

break The break Statement switch (letterGrade) { case ‘A’: case ‘B’:cout << “Good Work”; break; case ‘C’:cout << “Average Work”; break; case ‘D’: case ‘E’:cout << “Poor Work”; }

break The break Statement switch (letterGrade) { case ‘A’: case ‘a’: case ‘B’: case ‘b’:cout << “Good Work”; break; case ‘C’: case ‘c’:cout << “Average Work”; break; etc. } Remember: use default clause to catch programming and input errors

switch The switch Statement * * * * Menu * * * * 1. NY Yankees 2. Indians 3. Dodgers Choose either 1, 2, or 3:

switch The switch Statement switch (choice) { case 1: cout << “World Champs”; case 2: cout << “Good Guys”; case 3: cout << “Da Bums”; } What will be the output?

switch The switch Statement What will be the output when the user enters 1 World ChampsGood GuysDa Bums 2 Good GuysDa Bums 3 Da Bums * * * * 4 skips the switch

switch The switch Statement * switch (choice) { case 1: cout << “World Champs”; case 2: cout << “Good Guys”; case 3: cout << “Da Bums”; } switch (choice) { case 1: cout << “World Champs”; break; case 2: cout << “Good Guys”; break; case 3: cout << “Da Bums”; break; default: cout << “Enter a 1, 2, or 3”; }

Example from text, page 157 Switch(Choice) { case ‘a’: case ‘e’: case ‘i’: case ‘o’: case ‘u’: cout << “\nThe character in Choice is a vowel” <<endl; break; default: cout << “\nThe character in Choice is not a vowel” <<endl; break; //this break is optional } // end of switch statement

else The Dangling else if (avg >= 60.0) if (avg < 70.0) cout << “Passing, but marginal”; else cout << “Failing”; * if (avg >= 60.0) { if (avg < 70.0) cout << “Passing, but marginal”; } else cout << “Failing”; else will be paired with nearest if

Common Errors Using = in place of == Improper braces in nested ifs Too deeply nested ifs Missing “cases” Dangling “else” Missing break statements in switch statement Copyright © 1999 by Freedom TLC, Inc.

Debugging Syntax errors vs. Logic error Prevention - plan first! Display values C++ Debugger: man gdb Copyright © 1999 by Freedom TLC, Inc.

“I discovered I always have choices, and sometimes it’s only a choice of attitude” Judith M. Knowlton Copyright © 1999 by Freedom TLC, Inc.