Chapter 4 Flashcards
(36 cards)
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