Chapter If statement, Python
What types of relationships between values can you test with relational operators?
> greater than < less than >= greater than or equal to <= less than or equal to == equal to != not equal to
What is a flag variable?
A Boolean variable that when the flag value is true it means the condition exists, while a false value of the flag means the condition does not exist.
What values can you assign to a bool variable?
A bool variable can only store one of two possible values: True or False. Example: x = true; y = false
What is a compound Boolean expression?
A combination of two or more Boolean expressions using logical operators such as "and", ""or", and "not".
The following shows a combination of the values true and false connected by a logical operator. Indicate whether the result of such a combination is true or false. False and False
False
The following shows a combination of the values true and false connected by a logical operator. Indicate whether the result of such a combination is true or false. False and True
False
The following shows a combination of the values true and false connected by a logical operator. Indicate whether the result of such a combination is true or false. False or False
False
The following shows a combination of the values true and false connected by a logical operator. Indicate whether the result of such a combination is true or false. True and False
False
The following shows a combination of the values true and false connected by a logical operator. Indicate whether the result of such a combination is true or false. not True
False
You can write any program using only sequence structures. T or F
False
What is a flag and how does it work?
Flags are Boolean variables that indicate whether a specific condition exists or not. When the flag value is true it means the condition exists, while a false value of the flag means the condition does not exist.
Explain what is meant by the term "conditionally executed."
Is a set of statements that are executed only when a certain condition is true. If the condition is false they will not be executed. Also called a single alternative decision structure.
flag
a variable that signals when some condition exists in the program
The following shows a combination of the values true and false connected by a logical operator. Indicate whether the result of such a combination is true or false. True or True
True
The following shows a combination of the values true and false connected by a logical operator. Indicate whether the result of such a combination is true or false. not False
True
Dual Alternative Decision Structure
has two possible paths of execution one path is taken if the condition is true and another is taken if the condition is false
You use a(n) _____ statement to write a single alternative decision structure. a. test-jump b. if c. if-else d. if-call
if
What happens when the if statement executes?
the condition is tested if it is true the statements in the block are executed if it is false the statements in the block are skippped
What is used to write a single alternative decision structure?
the if statement
What values can be assigned to a bool variable?
true or false
What are the following operators? 1. greater than 2. less than 3. greater than or equal to 4. less than or equal to 5. equal to 6. not equal to
1. greater than > 2. less than < 3. greater than or equal to >= 4. less than or equal to <= 5. equal to == 6. not equal to !=
Control structure
a logical design that controls the order in which a set of statements execute
Sequence Structure
a set of statements that execute in the order that they appear
Briefly describe how the "and" operator works.
An expression formed by the "and" operator is true only when both the 'first' and 'second' sub expressions are true. If the left sub expression is false then it will not check the right sub expression because the overall expression has already been determined as false, this is called "short cut evaluation".
Briefly describe how the "or" operator works.
An expression formed by the "or" operator is false only when both the 'first' and 'second' sub expressions are false. If the left sub expression is true then it will not check the right sub expression because the overall expression has already been determined as true, this is called "short cut evaluation".
A(n) _____ expression has a value of either true or false. a. binary b. decision c. unconditional d. Boolean
Boolean
What is a Boolean expression?
Boolean expressions check the truth of an expression, it checks whether the expression is true or false.
What is a decision structure?
Decision structures are control structures which provide a different set of instructions to be executed based on different choices made by the user.
How does a dual alternative decision structure work?
Dual alternative decision structure is a control structure with two possible paths of execution. The program will follow one path if condition equals true and another if condition equals false.
You need to test a condition and then execute one set of statements if the condition is true. If the condition is false, you need to execute a different set of statements. What structure will you use?
Dual alternative decision structure.
A program can be made of only one type of control structure. You cannot combine structures. T or F
False
A single alternative decision structure tests a condition and then takes one path if the condition is true, or another path if the condition is false. T or F
False
What is a single alternative decision structure?
Single alternative decision structure is a control structure that only provides one alternative path of execution. If the condition is true the alternative path is taken, if the condition is false the alternative path is skipped.
What is a control structure?
The logical design which is used to control or handle the execution order of statements.
A compound Boolean expression created with the "and" operator is true only when both subexpressions are true. T or F
True
A decision structure can be nested inside another decision structure. T or F
True
The following shows a combination of the values true and false connected by a logical operator. Indicate whether the result of such a combination is true or false. False or True
True
The following shows a combination of the values true and false connected by a logical operator. Indicate whether the result of such a combination is true or false. True and True
True
The following shows a combination of the values true and false connected by a logical operator. Indicate whether the result of such a combination is true or false. True or False
True
When you write an if-else statement, under what circumstances do the statements that appear after the else clause execute?
When the if condition is false the program follows the else clause.
Conditionally Executed
action that is performed only when a certain condition is true
Compare strings
allows you to create decision structures that test the value of a string
A compound Boolean expression created with the _____ operator is true only if both of its subexpressions are true. a. and b. or c. not d. both
and
When determining whether a number is inside a range, which logical operator is it best to use?
and
Explain how short-circuit evaluation works with the "and" and "or" operators.
and operator short circuit - If the left sub expression is false then it will not check the right sub expression because the overall expression has already been determined as false. or operator short circuit - If the left sub expression is true then it will not check the right sub expression because the overall expression has already been determined as true.
if clause
begins with the word if and is followed by the "condition"
How do we test more than one condition?
by nesting a decision structure inside another decision structure
Decision or Selection structures
can execute a set of statements only under certain circumstances
A _____ structure can execute a set of statements only under certain circumstances. a. sequence b. circumstantial c. decision d. Boolean
decision
Relational Operator
determines whether a specific relationship exists between two values
A(n) _____ structure tests a condition and then takes one path if the condition is true, or another path if the condition is false. a. if statement b. single alternative decision c. dual alternative decision d. sequence
dual alternative decision
condition
expression that will be evaluated as either true or false
Boolean expression
expressions tested by the if statement
A _____ is a Boolean variable that signals when some condition exists in the program. a. flag b. signal c. sentinel d. siren
flag
Example if-elif-else statement
if condition1: statement statement elif condition2: statement statement else: statement statement
Example if-else statement
if condition: statement statement else: statement statement
sample if statement
if condition: statement statement
You use a(n) _____ statement to write a dual alternative decision structure. a. test-jump b. if c. if-else d. if-call
if-else
What statement do you use in Python to write a dual alternative decision structure?
if..... else
and, or, and not are _____ operators. a. relational b. logical c. conditional d. ternary
logical
The _____ operator takes a Boolean expression as its operand and reverses its logical value. a. and b. or c. not d. either
not
A compound Boolean expression created with the ____ operator is true if either of its subexpressions is true. a. and b. or c. not d. either
or
Single Alternative Decision Structure
provides only one alternative path of execution
The symbols >, <, and == are all _____ operators. a. relational b. logical c. conditional d. ternary
relational
A _____ structure provides one alternative path of execution. a. sequence b. single alternative decision c. one path alternative d. single execution decision
single alternative decision
if statement
used to create a decision structure, which allows a program to have more than one path of execution Causes one or more statements to execute only when a boolean expression is true
nested block
when a block of code that is inside another block of code is indented
if-else statement
will execute one block of statements if its condition is true, or another block if its condition is false