Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)

Slides:



Advertisements
Similar presentations
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Advertisements

1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving,
Selection Structures (if & switch statements) (CS1123)
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Other Conditional Methods. Conditional Operator Conditional Operator (?:) Conditional operator ( ?: ) – Ternary operator: 3 arguments expression1 ? expression2.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a)Multi way selection by nested IF structure.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
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.
Chapter 05 (Part III) Control Statements: Part II.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Copyright 2003 Scott/Jones Publishing Making Decisions.
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.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
1 CS161 Introduction to Computer Science Topic #8.
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.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Chapter 4 Selection Structures: if and switch Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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):
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Branching statements.
Chapter 3 Selection Statements
Chapter 3 Control Statements
Selection (also known as Branching) Jumail Bin Taliba by
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.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 4: Making Decisions.
Other Conditional Methods
Selection CSCE 121 J. Michael Moore.
Chapter 7 Conditional Statements
Decision I (if Statement)
Chapter 4: Selection Structures: if and switch Statements
Selection Control Structure
ICS103: Programming in C 4: Selection Structures
Programming Fundamental
Presentation transcript:

Decision II

CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)

CSCE 1063 Table 4.7 English Conditions as C++ Expressions

CSCE 1064 Boolean Assignment  bool type values are true and false  Assignment statements have general form variable = expression;  E.g.: (for variable called same of type bool) same = true; same = (x == y);

CSCE 1065 Comparing Characters and Strings  Letters are in typical alphabetical order  Upper and lower case significant  Digit characters are also ordered as expected  String objects require string library  Compares corresponding pairs of characters

CSCE 1066 Table 4.8 Examples of Comparisons

CSCE 1067 Table 4.6 Operator Precedence

CSCE 1068 Example a flagxyz false x + y / z <= false

CSCE 1069 Example b flagxyz false ! flag || (y + z >= x - z) true

CSCE Short Circuit Evaluation (single == ‘y’ && gender == ‘m’ && age >= 18)  If single condition is false, gender and age conditions are not evaluated. (single == ‘y’ || gender == ‘m’ || age >= 18)  If single condition is true, gender and age conditions are not evaluated.

CSCE Additional Assignment Examples  inRange = (n > -10) && (n < 10);  isLetter = ((‘A’ <= ch) && (ch <= ‘Z’)) || ((‘a’ <= ch) && (ch <= ‘z’));  even = (n % 2 == 0);

CSCE Writing bool Values  Boolean values are represented by integer values in C++  0 for false  non-zero (typically 1) for true  Outputting (or inputting) bool type values is done with integers

CSCE Complements of Relational Operators = > <= = = !=

CSCE Examples ExpressionComplement Flag!Flag x>=1 !(x>=1) or x<1 x>5 !(x>5) or x<=5

CSCE Complements of Boolean Expressions (cont’d)  Complementing (or getting opposite of) boolean/logical expressions can be done in 2 ways:  using logical operator ! (not)  using DeMorgan’s Theorem

CSCE DeMorgan’s Theorem !(exp 1 && exp 2 ) is the same as !exp 1 || !exp 2 !(exp 1 || exp 2 ) is the same as !exp 1 && !exp 2 It explains how to complement a compound logical expression

CSCE Example Expression z <= x && x <= y Complement !(z <= x && x <= y) the equivalent using DeMorgan’s theorem !(z <= x) || !(x <= y) which is equivalent to (without the not “!”) z > x || x > y

CSCE Table 4.7 English Conditions as C++ Expressions

CSCE Multiple Decision Exercise Please write a multiple decision C++ segment to evaluate a letter grade, input by the user, and output the corresponding phrase according to the following table: Grade Output A or aExcellent B or bGood C or cFair D or d or F or fPoor anything elseInvalid grade

CSCE if (grade == ‘A’ || grade == ‘a’) cout << “Excellent” << endl; else if (grade == ‘B’ || grade == ‘b’) cout << “Good” << endl; else if (grade == ‘C’ || grade == ‘c’) cout << “Fair” << endl; else if (grade == ‘D’ || grade == ‘d’ || grade == ‘F’ || grade == ‘f’) cout << “Poor” << endl; else cout << “Invalid grade” << endl; switch (grade) { case ‘A’: case ‘a’: cout << “Excellent” << endl; break; case ‘B’: case ‘b’: cout << “Good” << endl; break; case ‘C’: case ‘c’: cout << “Fair” << endl; break; case ‘D’: case ‘d’:case ‘F’: case ‘f’: cout << “Fair” << endl; break; default: cout << “Invalid grade” << endl; } Code Segments switch selector case label optional

CSCE The switch Control Statement switch (selector ) { case label 1 : statements 1 ; break; case label 2 : statements 2 ; break;... case label 10 : statements 10 ; break; default: statements d ; // optional }

CSCE switch Control Statement (cont’d)  Used in C++ to select one of several alternatives.  Alternative to multiple if statements in some cases.  Especially useful when the selection (called the switch selector) is based on the value of a single variable or a simple expression.  switch selector must be of type int, char, or bool only.

CSCE switch Control Statement (cont’d)  switch selector value compared to each case label. When there is an exact match, statements for that case are executed.  If no case label matches the selector, the entire switch body is skipped unless it contains a default case label.  break is typically used between cases to avoid fall through.

CSCE Listing 4.5 switch statement to determine life expectancy of a lightbulb

CSCE switch Control Statement (cont’d)  You have to use a formula to convert ranges to an integer, so as to be able to use a switch statement.  For example, if you are asked to use a switch statement to give the following corresponding outputs for some given ranges: Final GPA Honorary Degree 3.80 <= GPA <= 4.00Highest Honours 3.60 <= GPA < 3.80High Honours 3.40 <= GPA < 3.60Honours 2.00 <= GPA < 3.40 Pass with no Honours

CSCE switch Control Statement (cont’d) Here is one example for a formula that works, together with its corresponding case statements, provided that you check the range of GPA when input before: int x = GPA *10; switch (x) { case 40: case 39: case 38: cout<<“Highest Honours”; break; case 37: case 36: cout<<“High Honours”; break; case 35: case 34: cout<<“Honours”; break; default: cout<<“Pass with no Honours”; break; }

CSCE Next lecture will be about looping construct in C++