Chapter 4
Relational Operators
==, !=, >, <, >=, <=
if-else if
A decision structure used to decide among three or more actions.
The trailing else is an if/else if statement provides _________
A default action, or set of actions, when none of the if expressions are true and is often used to catch errors.
A _____ is a variable that signals whether or not some condition currently exists in a program.
A flag.
if statement
A line that determines whether or not you run a certain chunk of code
enumerated data type
A programmer-created data type with a fixed set of values.
Boolean
A single value of either TRUE or FALSE
decision structure
A statement that uses a condition to determine which set of statements to execute.
Relational Operators
All of the relational operators are binary. This means they use two operands. Here is an example of an expression using the greater than operator. X > Y
relational expression
Are Boolean expressions, which means their value can only be true OR false.
&& (AND) - logical operator
Connects two expressions into one. Both expressions must be true for the overall expression to be true.
|| (OR) - logical operator
Connects two expressions 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, and it does not matter which.
Relational Operators
Determine whether a specific relationship exists between two values
It is called an enumerated type because the named constants are enumerated, or listed, as part of the definition of the data type. See example :
Enum Roster { Tom, Sharon, Bill, Teresa, John}; This creates a data type named roster. Roster can be capitalized, but doesn't have to.
A _________ type is a programmer-defined data type whose only legal values are those associated with a set of named integer constants.
Enumerated data type
Then named integer constants associated with the enum Roster data type are called ______
Enumerators
Precedence of relational operators
Highest > >= < <= Lowest == !=
Unsure whether to use two separate if statements or a single if/else statement when two possible conditions exist.
If both conditions could be true or both could be false, use two separate if statements.
"Fall through"
If you don't use the break statement with the switch statement, the last case will execute.
isalnum
Returns true (a nonzero number) if the argument is a letter of the alphabet or a digit. Otherwise it returns 0. Or false
isspace
Returns true (a nonzero number) if the argument is a whitespace character. Whitespace characters are any of the following: space' ', vertical tab '\v', newline '\n', tab '\tab'.
! (NOT) - logical operator
Reverses the "truth" of an expression. It makes a true expression false and a false expression true.
The ___________ tests the value of an integer expression and then uses that value to determine which set of statements to branch to.
Switch statement
if-else statement
The ______ statement will execute one set of statements when the if condition is true and another set when the condition is false.
if statement
The common programming structure that implements "conditional statements".
constant expression
The expression following a case statement must be a(n) ___ ___. Used for switch statements
Semicolons do NOT mark the end of a line. They mark the end of a C++ statement.
The if construct is not complete without the one or more conditionally executed statements that come after it.
An else goes with the closest previous if statement that doesn't already have its own else.
The rule for matching each else with the proper if.
If (X = 2) is a true statement.
This assigns x to the value 2. Since it is not 0, the statement is true. All nonzero values are True.
A final else that is placed at the end of an if/else if statement is called a ____________
Trailing else
A condition that evaluates to true may have ANY nonzero value.
True
Bool variables hold the values true and false, they are the perfect type of variables to use for flags.
True
Integer variables may also be used as flags
True
Parentheses are strongly recommended anytime using && || operators in the same expression. Because && has higher precedence than ||.
True
True or false: braces are required in an if/else if whenever more than one statement in a conditionally executed block? Otherwise they are optional.
True
When a flag variable is set to false, it means that the condition does not exist, at least not yet.
True
Without the braces {} the if condition only determines whether or not the very next statement will be executed. Any following statements outside the braces will ALWAYS be executed.
True
enum colors { red, blue = 4, yellow } Red is 0, blue is 4, yellow is 5
True
if is a c++ key word and must be written in lower case.
True
A relational expression is either ______ or ______.
True or False
Relational Operators
Used to compare two values. Operators include =,<,>,<=,>=,<>
Local scope or Block Scope
Variables defined inside a set of braces. They may only be used in the part of the program between their definition and the block's closing brace.
short circuit evaluation
When a boolean expression is evaluated the evaluation starts at the left hand expression and proceeds to the right, stopping when it is no longer necessary to evaluate any further to determine the final outcome.
relational expression
Whereas < is called a relational operator, x < y is called a(n)________________
switch statement
a C++ statement that can be used to code a multiple-alternative selection structure
sequence structure
a set of statements that execute in the order that they appear
decision
code execution follows one path if a certain condition is met, or another path if a different condition is met
if (moreData == false) Can be written as
if (!moreData)
if (moreData == true) Can be written simply as
if (moreData)
Example of if/else indentation
if (number % 2 == 0) cout << number << " is even. \n"; else cout << number << " is odd. \n";
If two conditions are mutual exclusive, such that one must be true and the other false, an ____________ statement should be used
if/else
The________ is a chain of if statements. They perform their tests, one after another, until one of them is found to be true.
if/else if statement
The if/else if statement is actually a form of the __________ if statement.
nested
!=
not equal to operator
Conditionally executed
performed only when a certain condition exists
sequence structure
the control structure that directs the computer to process each instruction in the order listed in the program
input validation
the process of inspecting data given to a program by the user and determining if it is valid
isalpha()
true if alphabetic: a-z or A-Z
isdigit
true if digit: 0-9
if statement
written to carry out an action
When a relational expression is false, it has the value ________.
zero
The ! Operator has a higher precedence than many C++ operators, therefore it is a good idea to enclose its operand in parentheses, unless you intend to apply it to a variable or simple expression with no other operators. See example :
! ( x > 2 ) applies ! Operator to the expression, asking is x not greater than 2? !x > 2 applies ! Operator to x only, asking is the logical negation of x greater than 2 ? Suppose x is set to 5, since 5 is nonzero it would be considered true, but the ! Operator would reverse it to false which is 0.
logical operators
&& [and] || [or] ! [not]
conditional operator
An alternate way of coding an if-else statement using three operands. Example of a conditional expression : x < 0 ? Y = 10 : z = 20 X < 0 is the condition being tested Y = 10 executes if condition is true Z = 20 executes if condition is false
relational expression
An expression used to determine whether a specific relationship exists between two values.
Relational Operators
Are all binary operators with left to right associativity
How is true stored in memory?
As 1
If Construct
Contains two relevant parts: the condition being tested and the code that is executed when the condition evaluates to True
When relational expression is true
It does NOT always evaluate to 1, it can evaluate to any non zero value.
relational expression
It is used to determine whether X is greater than Y.
block {} braces
Lets the compiler know which statements are associated with the if statement. The opening brace must be located after the if condition. Only required whenever two or more actions are associated with an if statement.
A ______ is a screen displaying a set of choices the user selects from.
Menu
This construct called the _________ allows you to test more than one condition to determine which block of code should be executed.
Nested if statement
.isalpha()
Returns true (a nonzero number) if the argument is a letter of the alphabet. Returns 0 if the argument is not a letter.
islower
Returns true (a nonzero number) if the argument is a lowercase letter.
isprint
Returns true (a nonzero number) if the argument is a printable character (including a space).
ispunct
Returns true (a nonzero number) if the argument is a printable character other than a digit, letter, or space.
isupper
Returns true (a nonzero number) if the argument is a uppercase letter.
If you put a semicolon after the if condition potion of the statement, the compiler will prematurely terminate the if statement and always the if statement will ALWAYS execute. You scored an 80 That's a perfect score
True
break statement
a C++ statement used to tell the computer to leave a switch statement. The default section doesn't need a break statement, but you can use it for consistency.
conditional operator
a ternary operator written as ??:?; the three arguments explain what the condition is, what the result will be if the condition is true, and what the result will be if the condition is false
switch statement
allows multi-way branching. In many cases, using a switch statement can simplify a complex combination of if-else statements.
if-else statement
allows your program to perform one action if the Boolean expression evaluates to true and a different action if the Boolean expression evaluates to false.
Repetition
code execution repeats until a certain condition is met
In a decision structure, the action is _______________ executed because it is performed only when a certain condition is true.
conditionally
sequence
every line of code is executed one at a time
The if statement
•If (condition) is true, then the statement(s) in the body are executed. •If (condition) is false, then the statement(s) in the body are skipped.
Logical operator precedence order
! && ||