Professional Documents
Culture Documents
Wk02 Selection and Loops
Wk02 Selection and Loops
SELECTION
Example: Solving a Quadratic Eqt.
Example: Solving a Quadratic Eqt.
Example: Solving a Quadratic Eqt.
• Remember what we learned in high school…
• Why?
delta = 25-24 = 1 > 0
• If delta < 0
• The equation has no real solution
CONTROL
STRUCTURES
The basic building blocks of programming
CS1010E Programming Methodology
Control Structures
Sequence • Default
True False
• Also called ?
Selection branching
True False
?
True False
?
• For example:
If (I have $1000000000000000)
Buy a car
Eat a lot of buffets
Go travel
Quit NUS!
Else
Be good and study
CS1010E Programming Methodology
True False
?
True False
?
• For example:
If (I have $1000000000000000)
Buy a car
Eat a lot of buffets
Go travel
Quit NUS!
Else
Be good and study
True False
?
• For example:
If (I have $1000000000000000)
Buy a car
Eat a lot of buffets
Go travel
Quit NUS!
Else
Be good and study
Condition
condition
If the condition is True
If the condition is False
Conditional
Syntax Example
indentation
Conditional
Syntax Example
indentation 'Good'
'Good'
'Good'
Conditional
Syntax Example
if <expr>: a = 4
if <expr>: if a < 10:
if a < 1:
statement(s)
print('Here')
Print nothing
Conditional
Syntax Example
v rich
Conditional
Syntax Example
if <expr>: >>> a = -3
statement(s) >>> if a > 0:
elif <expr>: print('yes')
statements(s) elif a == 0:
else: print('no')
statement(s) else:
print('huh')
'huh'
Conditional
Syntax Example
if <expr>: >>> a = 4
statement(s) >>> if a > 0:
print('yes')
elif <expr>:
elif a == 0:
statements(s) print('no')
elif <expr>: elif a == 4:
statements(s) print('ahh')
else: else:
print('huh')
statement(s)
'yes'
Can be many
T F
a < 0
e.g. T F
>>> a = 4 print(‘yes’) a==0
>>> if a < 0:
print('yes')
print(‘no’)
elif a == 0: T F
print('no') a==4
elif a == 4:
print('ahh')
print(‘ahh’) print(‘huh’)
else:
print('huh')
'ahh'
Homework: Figure out ALL conditions
condition
Repetition
CS1010E Programming Methodology
False
Control Structure: Repetition ?
True
• While (a condition)
• Do something
• For example
While (I am hungry)
Eat a bun
0
1
2
3 Exclusive
4
Repetition Flow Control: “For”
Example Interpreted as
Produc Counte
1t
120
24
6
2 × 2r
5
4
3
1
Iterative Factorial
• 𝑛𝑛! = 1 × 2 × 3 ⋯ × 𝑛𝑛
• Computationally
• Starting:
• product = 1
• counter = 1
• Iterative (repeating) step:
• product product × counter
• counter counter + 1
• End:
• product contains the result
Computing Factorial
• 𝑛𝑛! = 1 × 2 × 3 ⋯ × 𝑛𝑛
• Factorial rule: product counter
• product product × counter
• counter counter + 1 1 2
non-inclusive. 1x2 = 2 3
def factorial(n):
Up to n. 1x2x3=6 4
product = 1
1x2x3x4=24 5
for counter in range(2, n+1):
120 6
product = product * counter
720 7
return product
factorial(6)
for loop
• for <var> in <sequence>:
• <body>
• sequence
• a sequence of values
• var
• variable that take each value in the sequence
• body
• statement(s) that will be evaluated for each value in the sequence
range function
• range([start,] stop[, step])
May omit
• creates a sequence of integers
• from start (inclusive) to stop (non-inclusive)
• incremented by step
Examples
for i in range(10):
print(i)
Randomly
generate
either 0 or 1
while loop
• while <expression>:
• <body>
• expression
• Predicate (condition) to stay within the loop
• body
• Statement(s) that will be evaluated if predicate is True
Repetition (Infinite)
while True: a = 0
print(‘Ah...’) while a > 0:
a = a + 1
print(a)
Repetition
indentation
Syntax Example
Syntax Example
• When
• num == ‘bye’? Or
• num!= ‘bye’ ?
Three Types of Loops
• Must run exactly N times
• Run any number of times
• Run at most N times
• Check all True (or check all False)
• Find any True (or False)
Check if a String is all Alphabets
• Given a string, example, ‘abc123’
• Check if all the characters are alphabets
• Return True or False
for j in range(10): 1
if j % 2 == 0: 3 Continue with
continue Jump 5 next value
print(j) 7
print("done") 9
done
Let’s play a game
>>> guessANum()
I have a number in mind between 0 and 99
Guess a number: 50
Too big
Guess a number: 25
Too big
Guess a number: 12
Too big
Guess a number: 6
Too small
Guess a number: 9
Too big
Guess a number: 7
Bingo!!!
>>>
guessANum.py
import random
def guessANum():
secret = random.randint(0,99) # 0 <= secret <= 99
guess = -1
print('I have a number in mind between 0 and 99')
while guess != secret:
guess = int(input('Guess a number: '))
if guess == secret:
print('Bingo!!! You got it! ') Repeat
elif guess < secret: until the
print('Your number is too small') condition
else: is False
print('Your number is too big')
guessANum()
guessANum.py
import random
def guessANum():
secret = random.randint(0,99) # 0 <= secret <= 99
guess = -1
print('I have a number in mind between 0 and 99')
while guess != secret:
guess = int(input('Guess a number: '))
if guess == secret:
print('Bingo!!! The answer is ' + str(secret))) Repeat
elif guess < secret: until the
print('Your number is too small') condition
else: is False
print('Your number is too big')
guessANum()
Monty Hall Problem (Again?!!)
How to write a love letter in Python
def show_my_love():
everything = True
you = everything
my_mind = you
while(my_mind == True):
print('I love you')
How to write a love letter in Python
def show_my_love():
everything = True #Everything I say is true
you = everything #You are everything to me
my_mind = you #All my mind is filled with you