Chapter 4 Flashcards

1
Q

Relational Operators

A
  • Used to compare numbers to determine relative order.
  • Operators: >, <, >=, <=. ==. !=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Relational Expressions

A
  • can be assigned to a variable.
  • assigns 0 for false, 1 for true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

if statement

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how an if statement works

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

block of code

A

code enclosed within { }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

if statement general format

A

if (expression)

statement;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

if/else statement

A
  • provides two possible paths of execution
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how does an if/else statement work

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

if/else statement general format

A

if (expression)

statement1;

else

statement2;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

nested if statements

A
  • an if statement that is nested inside another if statement
  • nested if statements can be used to test more than one condition
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

if/else if statement

A
  • tests a series of conditions until one is found to be true
  • often simpler than using nested if/else statements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

if/else if format

A

if (expression)

statement1;

else if (expression)

statement 2;

else

statement 3;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Flags

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

logical operators

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

short circuit evaluation

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Menu-driven program

A
  • program execution controlled by user selecting from a list of actions
17
Q

menu

A

list of choices on the screen

18
Q

how to implement menus

A

using if/else if statements

  1. display list of numbered or lettered choices for actions
  2. prompt user to make selection
  3. test user selection in expression (if a match, then execute code for action. if not, then go on to next expression)
19
Q

input validation

A
  • inspecting input data to determine whether it is acceptable
20
Q

what is bad input

A
  • bad output will be produced from bad input
21
Q

how to validate input

A
  • can perform various tests:
  • range
  • reasonableness
  • valid menu choice
  • divide by zero
22
Q

comparing characters and strings

A
  • 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’
23
Q

ASCII values from A to Z

A

65 - 90

24
Q

ASCII Values from a to z

A

97 - 122

25
Q

ASCII Values from 0 to 9

A

48 - 57

26
Q

the conditional operator

A
  • can use to create short if/else statements
27
Q

the conditional operator format

A

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
28
Q

switch statement

A
  • used to select among statements from several alternatives
  • can be used instead of if/else if statements
29
Q

variable shadowing

A
  • 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)
30
Q

local/block scope

A
  • variables defined inside { }
31
Q

when is the scope of a variable defined

A
  • at the beginning of the function
  • close to first use
32
Q

where is the scope of a variable

A

in the block in which it is defined, from the point of definition to the end of the block

33
Q

switch statement is good for -

A
  • 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
34
Q

switch statement format

A

switch (expression) {
case exp1: statement1;
break;
case exp2: statement2;
break;
default: statement3;
break;
}

35
Q

switch statement requirements

A
  1. expression must be an integer variable or an expressions that evaluates to an integer value
  2. exp1 through expn must be constant integer expressions or literals, and must be unique in the switch statement
  3. default is optional
  4. break is used to exit a switch statement, if it is left out the program “falls through” the remaining statements in the switch statement
36
Q

logical operator precedence

A
  • highest precedence to lowest precedence (!, &&, ||)