CS 111 CH 4
precedence of logical operators
!, &&, ||. NOT operator has highest precedence. use parentheses to avoid errors.
&&
(AND) connects two expressions into one. both expressions must be true for the overall expression to be true.
!
(NOT) reverses the "truth" of an expression. it makes a true expression false and vice versa
||
(OR) connects two expression into one. one or both expressions must be true for the overall expression to be true. it is only necessary for one to be true.
local scope
(block scope) variables defined inside a set of braces have ______ _______. they may only be used in the part of the program between their definition and the block's closing brace.
0
= false. any expression that haves a value of _____ is considered false by the if statement. this includes the bool value false, which is equivalent to _____.
1
= true. the bool value true = _____.
almost anywhere
C++ allows you to create variables _______ _____ in a program
avoid
_____ giving inner variables the same name as outer variables
numeric data
______ _____ is compared in C++ by using *rational operators*
flag
a bool variable that signals when some condition exists in the program. when ____ = false, it indicates that the condition does not exist. when ____ = true, it means the condition does exist. integers can also be _____s.
decision structure
a program to execute some statements only under certain circumstances. in a _____ _____'s simplest form, a specific action is taken only when a specific condition exists. if the condition does not match, the action is not performed.
menu
a screen displaying a set of choices the user selects from
binary
all of the relational operators are _____, which means they *use two operands*
null statement
an empty statement that does nothing. will prematurely terminate the if statement, which disconnects it from the following statements.
relational expression
an example of a _____ _____ is x > y. ____ ____s have a higher precedence than the assignment operator.
if/else statement
an expansion of the if statement. if the expression is true, a statement or block of statements is executed. else, if the statement is false, a separate group of statements is executed.
default
an optional _______ section comes after all the case statements. does not need a break statement.
non 0 value
any _____ _____ ____ is considered true by the if statement.
trailing else clause
appears at end of the if/else if statements, is optional, but in many citations, it is used to catch errors.
indenting and spacing
are for human readers, not the compiler.
logical operators
can combine two or more relational expressions into one
break
case statements show the program where o start executing in the block and the _____ statements show the program where to stop.
OR operator
complete expressions must be provided for the ____ ____. temperature > 100 || temperature < 0
conditional operator
consists of the question mark (?) and the colon (:) is a ternary operator
conditional expression
consists of three sub-expressions separated by the ? and : symbols. format expression ? expression : expression. the first expression is tested. if it is true, the second expression executes. if the first one is false, the third expression is executed.
==, =
do not confuse the equality operator (____ ____) with the assignment operator (______). e.g. the expression x = 2 is always true.
semicolons
do not mark the end of a line. but the end of a complete C++ statement
indented
do not write code that is not properly ______
relational operator
each _____ ____ determines whether a specific relationship exists between 2 values. all ____ _____s are *left-to-right associative*
one level
each time you press the tab key, you are indenting ____ _____
block of code
enclosing a group of statements inside a set of braces. all statements inside the braces should be indented
==
equal to. determines if the operand on its left is equal to the one on its rigtht.
>
greater than
>=
greater than or equal to. determines whether the operand on its left is greater than or equal to the operand on the right.
short circuit evaluation
if the sub-expression on the left side of an && operator is false, the right side will not be checked. if the sub-expression of the left side of an || operator is true, the right side will not be checked
top
it is a common practice to define all of a function's variables at the _____ of a function
logical operators have
left-to-right associativity
<
less than
<=
less than or equal to. determines whether the operand on its left is less than or equal to the operand on its right.
higher
lowercase letters have _____ ASCII codes than uppercase letters.
if/else if statement
makes certain types of nested decision logic simpler to write. braces needed if more than one statement is conditionally executed.
division by zero
mathematically impossible to perform, and causes a program to crash.
!=
not equal to. determines if the operand on its left is not equal to the one on its right.
imput validations
numbers are checked to ensure they are within a range of possible values. values are checked for their "reasonableness". items selected from a menu or others sets of choices are checked to ensure they are available options. variables are checked for values that might cause problems (such as division by 0)
case
on the next line s the beginning of a block containing several ______ statements. after the word _____ is a constant expression (must be int), followed by a colon. the constant expression may be an integer literal ir an integer named constant
if statement
one way to code a decision. if the value of the expression inside the parentheses is true, the statement is executed. otherwise, it is skipped. usu. span more than one line, but are technically one long statement.
continuously executed
performed only when a certain condition exists. it only executes under the condition that the expression in the parentheses is true.
boolean expressions
relational expressions are also known as ______ _____. their value can only be *true or false*
Not Operator
reverses truths or falsehoods. !true = false. !false = true.
nested
sometimes a if statement must be _____ inside another if statement
sequence structure
statements are executed in sequence without branching off in another direction
switch
tests the value of an integer expression and then uses that value to determine which set of statements to branch to. the first line of the statements starts with the word ____, followed by an integer expression inside parentheses
<< operator
the << operator has a higher precedence than the ?: operator
nested if/else statements disadvantages
the code can grow complex and become difficult to understand. can become too long to display on the computer screen without horizontal scrolling
if statement style rules
the conditionally executed statement should appear on the line after the if statement. the conditionally executed statement should be indented one "level" from the if statement.
conditionaly
the if statement can _____ execute a block of statements enclosed in braces
invalid validation
the process of inspecting data given to a program by the user and determining if it is valid
menu system
the switch statement is a natural mechanism for building ______ _______
name
when a block is nested inside another block, a variable defined in the inner block may have the same ______ as a variable defined in the outer block
comes into scope
when a program is running and it enters the section of code that constitutes a variable's scope, it is said that the variable _____ _____ _____
&& operator
when determining whether a number is inside a numeric range, it's best to use the ____ ____. e.g. x >= 20 && x <= 40
|| operator
when determining whether a number is outside a range use the ____ _____ e.g x < 20 || x > 40
string objects
when two ____ ____ are compared, it is actually the *ASCII* value of the characters making up the strings that are being compared. one by one, each character in the first operand is compared with the character in the corresponding position in the second operand.
ASCII
when two characters are compared, their _____ values are actually being compared. '0' - '9' = 48 - 54. 'A' - 'Z' = 65 - 90. 'a' - 'z' = 97 - 122. blank = 32. period = 46.
parentheses
when writing more complex comparisons, it helps to enclose the relational expression in ______
"fall through"
without break statements, the program "_____ _____"all of the statements below the one with the matching case expression. if you want the same set of statements for multiple case expressions.
complete expressions
you must provide _____ _____ on both sides of the && operator. e.g. temperature > 1 && temperature < 0
C++ does not allow
you to check numeric ranges with expressions such as 5< x <20