GR 7-5.print - Input - Flow of Control - Trace Table
GR 7-5.print - Input - Flow of Control - Trace Table
Syntax:
2. SELECTION
3. ITERATION OR LOOPING
SEQUENC SELECTIO ITERATIO
E N N
Statement 1
Statement 2
Statement 3
SEQUENC
E
1. SEQUENCE - PROGRAM
1. SEQUENCE
FLOW CHART
False
Condition ? Statement 1 Statement 2
Main True
Body
Statement 1
else
Statement 2 body
CONDITIONAL CONSTRUCT – if else STATEMENT
if (condition):
#statements to be executed when condition is True.
else:
#statements to be executed when condition is False.
: Colon Must
Use
indent
else is
used
OUT
PUT
Try Example
name = "Luigi"
if name == "Mario":
print("It’s same, Mario!")
else:
print("I'm not Mario ;_;")
Keywords – are reserved words and always written in Upper case.
Example: - INPUT, OUTPUT, IF, ELSE etc.
Example: To find the greater number between two numbers.
This program represented in pseudocode would look like this:
START
OUTPUT “Enter two numbers”
INPUT a, b
IF a > b THEN
OUTPUT “a is greater”
ELSE
OUTPUT “b is greater”
ENDIF
END
Flow Chart
HW-Basic Programming
Worksheet 6
Trace Table
a b Output
Largest
5 3 a is largest
8 9 b is largest
HW: Basic Programming Worksheet 7
Nested if statement:
temp=float(input("Enter temperature:"))
if (temp >= 100):
if (temp == 100):
print("Temperature is equal to boiling point of water")
else:
print("Temperature is greater than boiling point of water")
else:
print("Temperature is less than boiling point of water")
Note: 100 C
Elif Statement:
if first condition:
first body
elif second condition:
second body
elif third condition:
third body
else:
fourth body
EXAMPLES – if elif STATEMENT
READ AS
18 is less
than age
and
18 is less
than 60
OUTPU
T
Python program
age=int(input("Enter age"))
if (age >= 60):
print("Senior Discount")
elif (18<= age and age<60):
print("No Discount")
else:
print("Junior Discount")
HW :
Basic Programming worksheet
8
3. ITERATION OR LOOPING
ITERATIO
N
ITERATION OR LOOPING
while loop
for loop
for LOOP
for LOOP
for n in range(3,6):
print(n)
for LOOP - range KEYWORD
: Colon Must
Use
indent
OUTPU
T
range(start, stop, step)
OUTPU
T
Input range
Q5,Q6 and Q7
Cake Sale
Once your code is done, complete the following tests to check that your code is working as it
should:
#Cake Sale Challenge
cupcakePrice = 0.40
macaronPrice = 0.50
cheesecakePrice = 0.70
#Step 1:
Inputcupcakes = int(input("How many cupcakes do you plan to sell?"))
macarons = int(input("How many Macarons do you plan to sell?"))
cheesecake = int(input("How many cheesecake do you plan to sell?"))
#Step 2: Process
total=cupcakes*cupcakePrice+macarons*macaronPrice+cheesecake*cheesec
akePrice
#Step 3: Output
print("In this cake sale you will raise:")
print(total)
Iteration
▪ Iteration is the repeated execution of a
set of statements. There are two ways
to create iteration in Python:
• By counting how many times the
statements are to be executed
• By repeatedly executing the
statements while a condition is true
Types of iteration
▪ Definite iteration: A for
loop is used to iterate a
set of statements for a
specific number of
times.
▪ Indefinite iteration: A
while loop is used to
iterate a set of
statement while a
While loop
condition is true.
Basics Programming -Worksheet 12
in class
For loop- 63
Recap
total = 0