07selection_3014

07selection_3014 - Lecture File 07 COP 3014 January 7, 2008...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture File 07 COP 3014 January 7, 2008 A. Ford Tyson 1 COP 3014 Lecture File 07 ± Selection – Relational and Logical Operators and Expressions 1 – Control Structures & C++ Statements – if and if-else statements – nested if – switch statement Copyright 1997-present, Ann Ford Tyson Relational & Logical Operators ± Relational Operators < <= > >= == != 2 ± Logical Operators && ( and ) || ( or ) ! ( not ) First, why ? ± This is what we are heading for today: C++ code which allows us to make decisions and choices in a program 3 ± Example: if (course is open) then register student else print "can't add" message
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture File 07 COP 3014 January 7, 2008 A. Ford Tyson 2 Thought Exercise! ± How would we implement an IPOD menu like this: Music 4 Playlists Artists Songs Podcasts Extras Settings Shuffle Songs A Note about true and false ± we use the bool type to declare variables which can store the bool values true and false 5 ± in C++, when expressions are evaluated, any zero value is considered false, and any non-zero value is considered true bool undergrad = true; int age = 20; char gender = 'F'; EXPR VALUE SIDE EFFECT Example Relational Expressions 6 ± undergrad true no ± age < = 25 true no ± gender == 'M' false no ± gender = 'M' true ('M') yes gender 'M' ± (age % 2) == 0true no
Background image of page 2
Lecture File 07 COP 3014 January 7, 2008 A. Ford Tyson 3 ± AND is a binary logical operation, i.e., it has two operands. ± Letting A and B denote the operands, A AND B = true if and only if A = true AND Operation (&&) 7 AND B = true. ± Truth table: ABA AND B F F F F T F T F F T T T ± OR is also a binary logical operation. ± Letting A and B denote the operands, A OR B = true if and only if A = true OR Operation (||) 8 OR B = true. ± Truth table: AB A OR B F F F F T T T F T T T T ± NOT is a unary logical operation (i.e. there is one operand). ± Letting A denote this operand, NOT Operation 9 NOT A = true if and only if A = false. ± Truth table: A NOT A F T T F
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture File 07 COP 3014 January 7, 2008 A. Ford Tyson 4 OPERATOR ASSOCIATIVITY HIGH ( ) left to right function call left to right ! + - cast unary; right to left ++ -- Precedence Rules Recap 10 * / % left to right + - left to right < <= > >= left to right == != && left to right || left to right = right to left LOW EXPR VALUE ± !undergrad false ± undergrad && (age > 18) true ± undergrad && (age == 18) false Example Logical Expressions 11 ± (17 < age) && (age < 24) true compare to "17 < age < 24" ± undergrad || (age == 18) true inclusive OR (one or both true) ± (undergrad || age == 18) && !(undergrad && age == 18) true exclusive OR (exactly one true) From: The Idiot’s Guide to Dating, Computer Science Edition ± Person A says: “Would you like to go to a movie or to that new play tonight?” 12 ± Person B says: “Yes.” – Is there a problem?
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/14/2011 for the course COP 3014 taught by Professor Tyson during the Fall '10 term at FSU.

Page1 / 18

07selection_3014 - Lecture File 07 COP 3014 January 7, 2008...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online