CS31-2 - CS 31: Introduction to Computer Science I Topic 2...

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 2: Flow of Control 1/2010 John A. Rohr All Rights Reserved JAR 2-1 CS 31 : Introduction to Computer Science I Topic 2 Flow of Control
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 2: Flow of Control 1/2010 John A. Rohr All Rights Reserved JAR 2-2 Flow of Control Boolean Expressions Are also called conditional expressions Use logical operands and operators Similar to arithmetic expressions Evaluate to true or false Used for selection
Background image of page 2
CS 31: Introduction To CS I Topic 2: Flow of Control 1/2010 John A. Rohr All Rights Reserved JAR 2-3 Flow of Control Logical Operators Arithmetic comparison ==, !=, <, >, <=, >= NOTE: == Logical combination !, &&, | |
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 2: Flow of Control 1/2010 John A. Rohr All Rights Reserved JAR 2-4 Flow of Control Arithmetic Comparison Ops Compare two arithmetic values May be signed or unsigned Result is true or false Examples: 25 <= 40 true 59.374 == 95.81 false
Background image of page 4
CS 31: Introduction To CS I Topic 2: Flow of Control 1/2010 John A. Rohr All Rights Reserved JAR 2-5 Flow of Control Logical Combination Ops Unary: ! Not Binary: && And | | Or
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 2: Flow of Control 1/2010 John A. Rohr All Rights Reserved JAR 2-6 Flow of Control Logical Operators: ! (Not) Unary: Opposite of value !true = false !false = true
Background image of page 6
CS 31: Introduction To CS I Topic 2: Flow of Control 1/2010 John A. Rohr All Rights Reserved JAR 2-7 Flow of Control Logical Operators: && (And) Binary: True only if both operands true false && false= false false && true = false true && false = false true && true = true
Background image of page 7

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 2: Flow of Control 1/2010 John A. Rohr All Rights Reserved JAR 2-8 Flow of Control Logical Operators: | | (Or) Binary: True only if either operand is true false | | false = false false | | true = true true | | false = true true | | true = true
Background image of page 8
CS 31: Introduction To CS I Topic 2: Flow of Control 1/2010 John A. Rohr All Rights Reserved JAR 2-9 Flow of Control Precedence Hierarchy of operators Determines evaluation order without ( ) 18 levels in C++ (Depending on author) Use parentheses to override Use parentheses for clarity
Background image of page 9

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 2: Flow of Control 1/2010 John A. Rohr All Rights Reserved JAR 2-10 Flow of Control Precedence Levels (Partial) Postfix increment and decrement Prefix increment and decrement, Unary + and -, Not (!) Multiplicative operators (*, /, %) Additive operators (+, -) I/O operators (<<, >>) Magnitude comparison (<, >, <=, >=) Equality comparison (==, !=) And (&&) Or (| |) Assignment (=)
Background image of page 10
CS 31: Introduction To CS I Topic 2: Flow of Control 1/2010 John A. Rohr All Rights Reserved JAR 2-11 Flow of Control Short-Circuit Evaluation Used for && and | | operators May not evaluate second operand Evaluated only if needed Examples: false && anything false true | | anything true
Background image of page 11

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 2: Flow of Control 1/2010 John A. Rohr All Rights Reserved JAR 2-12 Flow of Control Integers as Boolean Values Used in C language Available in C++ 0 false Any nonzero value true Compiler generates 1 for true
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 65

CS31-2 - CS 31: Introduction to Computer Science I Topic 2...

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

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