Other Conditional Methods. Conditional Operator Conditional Operator (?:) Conditional operator ( ?: ) – Ternary operator: 3 arguments expression1 ? expression2.

Slides:



Advertisements
Similar presentations
The if-else Statements
Advertisements

Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
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)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
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.
Chapter 4 Making Decisions
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
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.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
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.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Flow of Control Part 1: Selection
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
TK 1914 : C++ Programming Control Structures I (Selection)
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Chapter Making Decisions 4. Relational Operators 4.1.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Control statements Mostafa Abdallah
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Branching statements.
Chapter 4: Control Structures I (Selection)
Chapter 3 Control Statements
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 4: Making Decisions.
Expressions and Control Flow in JavaScript
Control Structures – Selection
Chapter 4: Control Structures I (Selection)
Other Conditional Methods
Alternate Version of STARTING OUT WITH C++ 4th Edition
Selection CSCE 121 J. Michael Moore.
Chapter 7 Conditional Statements
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Branching statements Kingdom of Saudi Arabia
Chap 7. Advanced Control Statements in Java
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Presentation transcript:

Other Conditional Methods

Conditional Operator

Conditional Operator (?:) Conditional operator ( ?: ) – Ternary operator: 3 arguments expression1 ? expression2 : expression3 If expression1 is – true, result is expression2 – false, result is expression3

Conditional Operator (?:) Example: int max = (a >= b) ? a : b; Short for: int max; if (a >= b) max = a; else max = b;

Common Use Pick one of two values inside larger expression:

Switch Statement

Comparing if…else with ifs Method 1: if( month == 1) cout << "Jan"; if( month == 2) cout << "Feb"; if( month == 3) cout << "Mar"; if( month == 4) cout << "Apr"; …

Comparing if…else with ifs Method 2: if( month == 1) cout << "Jan"; else if( month == 2) cout << "Feb"; else if( month == 3) cout << "Mar"; else if( month == 4) cout << "Apr"; …

Comparing if…else with ifs Method 2 is more efficient… but messy

Switch Statement switch choses from list of integer options – If 1 do this – If 2 do this – If 3 do this …

switch Structures switch expression is evaluated first – Must be integral value

switch Structures switch expression evaluated – Must be integral value Jump to matching value

switch Structures switch expression is evaluated first – Must be integral value Jump to matching value Execute any instructions in that case

switch Structures switch expression is evaluated first – Must be integral value Jump to matching value Execute any instructions in that case Continue until break – break jumps to }

switch version Switch month select: int month = magic value switch(month) { case 1: cout << "Jan"; break; case 2: cout << "Feb"; break; … }

switch version int month = magic value switch(month) { case 1: cout << "Jan"; cout << "1st month"; break; case 2: … }

switch version Default is the "else" switch(month) { … case 11: cout << "Nov"; break; case 12: cout << "Dec"; break; default: cout << "Unknown month"; }

switch version No break – continues executing statements! – Jan and Feb!!! int month = magic value switch(month) { case 1: cout << "Jan"; case 2: cout << "Feb"; break; … }

switch version Fall through used well: switch (day) { case 1: // Fall to through to the next case case 2: // Fall to through to the next case case 3: // Fall to through to the next case case 4: // Fall to through to the next case case 5: cout << "Weekday"; break; case 0: // Fall to through to the next case case 6: cout << "Weekend"; }

Switch Gotchas No way to represent ranges: switch (day) { case <= 5: cout << "Weekday"; //NO Easy to mess up – forget break statement

Logical Operators

Boolean Operators Compound condition: If you make over $35000 and are married Boolean/Logic Operators combine/manipulate Boolean values

Not ! ! : Not : Logical unary negation operator – Evaluates to opposite of input False  True and True  False Examples (assume age = 24, weight = 140) ExpressionValueExpressionValue age > 18True!(age > 18)False weight == 150False!( weight == 150)True

OR || || : OR : Logical Or operator – True if either or both operands is true Examples (assume age = 24, weight = 140) A || BValueExpression AValueExpression BValue (age > 34) || (weight <= 140)Trueage > 34Falseweight <= 140True (age > 34) || (weight >= 150)Falseage > 34Falseweight >= 150False

AND && && : AND : Logical And operator – True if both operands are true Examples (assume age = 24, weight = 140) A && BValueExpression AValueExpression BValue (age > 18) && (weight <= 140)Trueage > 18Trueweight <= 140True (age > 18) && (weight > 140)Falseage > 18Trueweight >= 150False

When to use: Yuck: Same code Expressed Twice!!!

When to use: Better: Conditional expresses logic for leap year in one expression

When to use: Yuck: Same information Expressed Twice!!!

When to use: Better: Each boundary only defined once

Order of ops important! 1.( ) 2.! 3.relational operators ==,, etc… 4.&& 5.II Left to right in each category if (!2 < 4)if (!(2 < 4))

Order of ops important! 1.( ) 2.! 3.relational operators ==,, etc… 4.&& 5.II Left to right in each category if (!2 < 4)if (!(2 < 4)) if ( false < 4 )if ( !(true) ) if ( 0 < 4 )if ( false ) if ( true )

This does not work: 0 <= num <= 10 num = 20 Compound Warning

Each half of compound conditional MUST stand alone: NO!!! if(60 <= grade < 90) YES if(grade = 60)

Short Circuit C++ only evaluates right half of logical statement if needed – Short Circuit evaluation bool result = x > 5 && y == 2; Not evaluated if x <= 5

Short Circuit Short circuit used to "guard" an operation: bool result = fileExists && fileContains("assign")… Don't read if doesn't exist

Short Circuit Short circuit can cause bugs: – Avoid side effects in second statement bool result = x > 2 && y++ > 1; x > 2, we add one to y x <= 2, nothing happens to y

Logical Equivalencies Many ways to write same thing !( x > 5)x <= 5 Use easiest form – No: !( x != 5) – Yes: x == 5

DeMorgan's Can convert AND  OR – Not both means not one or not the other !(x && y) == !x || !y – Not either means not one and not the other !(x || y) == !x && ! y Use to simplify logic: !(x != 5 || y > 2) !(x !=5) && !(y > 2) x == 5 && y <= 2

Precedence What goes when? – Left to right in category

Parens Parentheses are Good TM (true || true) && falsetrue || true && false

Parens Parentheses are Good TM (true || true) && falsetrue || true && false (true) && falsetrue || false && before || without parens!

Parens Parentheses are Good TM (true || true) && falsetrue || true && false (true) && falsetrue || false false true Parens make desired ordering obvious