Intro to CS Lec05
Intro to CS Lec05
Variables: Scratch
Review
def print_twice(param):
print(param)
print(param)
Review
>>> print_twice(1)
>>> print_twice(‘test’)
>>> print_twice(math.pi)
>>> print_twice(‘test ’ * 4)
Flow of Execution
When a flowchart will not fit onto a single page we use this
shape to show how the sections of the flowchart connect
together.
Branching programs
Review: Boolean Expressions
A Boolean expression is an
expression that evaluates to produce
a result which is a Boolean value.
Review: Boolean Expressions
Test
True False
Block Block
Code
Simple conditional statement
if(<conditional statements>):
<block of code>
Tea brewing algorithm
Wait 5min
if ( tea boils ):
turn off the stove
Indentation
blocks of code
Most other programming languages use {}
Start
example
Input number
Divisible by two Pseudocode
Accept integer input from user
Is
Divisible?
End
Example
>>> print(dividend/divisor)
if(1): if(0):
print(‘one’) print(‘zero’)
Alternative Executions
if(<conditional statements>):
<if block>
else:
<else block>
Odd–Even program
Input: x
if (x is divisible by 2):
print(‘x is even’)
else:
print(‘x is odd’)
Chained conditionals
if(<conditional statements>):
<if block>
elif(<conditional statements>):
<elif block>
else:
<else block>
Nested Conditionals
if(<conditional statements>):
if(<conditional statements>):
<if block>
else:
<else block>
else:
<else block>
Nested Conditionals
if 0 < x:
if x % 2 == 0:
print(‘x is positive even number’)
Common Errors
Common Errors
if((Value > 0) or (Value <= 10)):
print(Value)