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
Menu-driven program
- program execution controlled by user selecting from a list of actions
menu
list of choices on the screen
how to implement menus
using if/else if statements
- display list of numbered or lettered choices for actions
- prompt user to make selection
- test user selection in expression (if a match, then execute code for action. if not, then go on to next expression)
input validation
- inspecting input data to determine whether it is acceptable
what is bad input
- bad output will be produced from bad input
how to validate input
- can perform various tests:
- range
- reasonableness
- valid menu choice
- divide by zero
comparing characters and strings
- characters are compared using their ASCII values
- ‘A’ < ‘B’: (65) < (66)
- ‘1’ < ‘2’: (49) < (50)
- lowercase letters have higher ASCII codes than uppercase letters: ‘a’ > ‘Z’
ASCII values from A to Z
65 - 90
ASCII Values from a to z
97 - 122
ASCII Values from 0 to 9
48 - 57
the conditional operator
- can use to create short if/else statements
the conditional operator format
expr ? expr : expr;
- the value of the conditional expression is: the second expression if the first expression is true OR the third expression if the first expression is false
- ( ) may be needed in an expression due to precedence of condition operator
switch statement
- used to select among statements from several alternatives
- can be used instead of if/else if statements
variable shadowing
- when inside a block within another block, can define variables with the same name as in the outer block (when in inner block, outer definition is not available; not a good idea)
local/block scope
- variables defined inside { }
when is the scope of a variable defined
- at the beginning of the function
- close to first use
where is the scope of a variable
in the block in which it is defined, from the point of definition to the end of the block
switch statement is good for -
- switch statement is a natural choice for menu-driven programs
- display the menu
- get user’s selection
- use user input as expression in switch statement
- use menu choices as expr in case statements
switch statement format
switch (expression) {
case exp1: statement1;
break;
case exp2: statement2;
break;
default: statement3;
break;
}
switch statement requirements
- expression must be an integer variable or an expressions that evaluates to an integer value
- exp1 through expn must be constant integer expressions or literals, and must be unique in the switch statement
- default is optional
- break is used to exit a switch statement, if it is left out the program “falls through” the remaining statements in the switch statement
logical operator precedence
- highest precedence to lowest precedence (!, &&, ||)