Chapter 4 Flashcards
Relational Operators
- Used to compare numbers to determine relative order.
- Operators: >, <, >=, <=. ==. !=
Relational Expressions
- can be assigned to a variable.
- assigns 0 for false, 1 for true
if statement
- allows statements to be conditionally executed or skipped over
- to execute more than one statement as part of an if statement, enclose them in {} (creates a block of code)
how an if statement works
- if the expression is true, then statement is executed
if the expressions if false, then statement is skipped
- place statement on a separate line after (expression)
- be careful testing floats and doubles for equality
- 0 is false, any other value is true
block of code
code enclosed within { }
if statement general format
if (expression)
statement;
if/else statement
- provides two possible paths of execution
how does an if/else statement work
- if the expression is true, then statement1 is executed and statement2 is skipped.
- if the expression if false, then statement1 is skipped and statement2 is executed
if/else statement general format
if (expression)
statement1;
else
statement2;
nested if statements
- an if statement that is nested inside another if statement
- nested if statements can be used to test more than one condition
if/else if statement
- tests a series of conditions until one is found to be true
- often simpler than using nested if/else statements
if/else if format
if (expression)
statement1;
else if (expression)
statement 2;
else
statement 3;
Flags
- variable that signals a condition
- usually implemented as a bool variable
- can also be an integer (the value 0 is considered false, any nonzero value is considered true)
- must be assigned an initial value before it is used
logical operators
- used to create relational expressions from other relation expressions
- && (AND): true if both expressions are true
- || (OR): true if either expression is true
- ! (NOT): true expression becomes false, false expression becomes true
short circuit evaluation
if the value of an expressions can be determined by evaluating just the sub-expression on the left side of a logical operator, then the sub-expression on the right side will not be evaluated