Module 3 - Conditionals Flashcards
What are control structures? What are examples of control structures?
A logical design that controls the order in which a set of statements execute A structure (block of code) that CONTROLS the flow of the program For example, sequential (sequence), decision (selection) = if/then, iteration (loop)
How do statements execute in a sequence control structure?
Line-by-line in the order they appear
How do statements execute in a decision control structure?
Specified action occurs only if a condition exists
What does a diamond represent in a flowchart?
Decision point
Represents true/false condition that must be tested
What is a single alternative decision structure?
If condition is not true, exit structure
A decision structure that provides a single alternative path of execution. If the condition that is being tested is true, the program takes the alternative path.
if statement in Python
if “condition”:
statement 1
statement 2
- 4 spaces before statements
- NOT If (case sensitive!)
What is a boolean expression? Give an example.
An expression that evalutes to true or false
Tested by if statement
Example: a>b
How do you test if a and b are equal?
a==b
What is a relational operator?
Determines relationship b/w two values/ operands
>,=,<=,>=,==,!=
What happens if you enter “if a=b:” into Python?
= is the assignment operator
writing “if a=b:” will produce a syntax error
to test if they are equal, must use ==
What’s the relational operator for “not equal”?
!=
What is a dual alternative decision structure?
Two possible paths of execution: one if decision is true, one if decision is false
Write an if statement that assigns 0 to x if y is equal to 20.
if y==20:
x=0
How do you align/indent if-then statements?
if condition:
statements
else:
statements
if and else must be aligned
statements in each block must be indented
(4 spaces before the statements)
What is whitespace? How many spaces per indentation?
Whitespace is just characters which are used for spacing, and have an “empty” representation
4 spaces per indentation
When indenting in python, can you mix tab and space?
No NEVER mix them
If you compare a and b in Python, which is bigger?
“b”>”a”
“c”>”b”
Is “a”>”A” in Python?
Yes! ‘A’
How would Python compare the strings hellO and Hello? i.e., “hellO” < “Hello”
It goes letter by letter (in the corresponding location)
The FIRST comparison that fails stops the whole comparison and returns the false
h < H –> FALSE. it should stop there and return false.