Programming
Official (Open)
Test Lab PC with Netsupport
Official (Open)
Test the following
• Can Login to nyplms
• Can Open IDE and run a print()
• Can download Zip notes and extract zip file
• Can open PDF using Acrobat reader
2-2
Demo ( range() statement )
Official (Open)
Loop body
range( 3 )
• This generates a sequence that starts from 0, ends before 3 0, 1, 2
• for loop runs through the sequence 0, 1, 2 assigning the value to i
• While running through the sequence, it also execute the statement inside
it’s Loop body
Output
• so it run the statement 3 times
• i = 0,
• i = 1,
• i = 2,
2-3
Demo ( for loop statement )
Official (Open)
Output
for loop
• Used when we need to repeat running some codes
• Known as counter-controlled loop because
• there is a counter variable , i start from 0
• After each loop, i value updates increment by 1
• Keep looping (repeating) as long end is not reached Have not reach 3
• Usually used when we know how many times we want to repeat
2-4
Demo ( for loop statement )
Official (Open)
Let’s look at the following program that get user to enter 3 days pay and
display the total at the end.
Sample Output
We can see that the code repeat 3 times
How do we use a for loop to do it?
3 times
2-5
Demo ( for loop statement )
Official (Open)
We can also use the loop variable if necessary
Sample Output
Can change a bit to start from 1 instead of 0 Sample Output
Starts from 1
Ends before 4
2-6
Demo ( range(start, stop, step) statement )
Official (Open)
range() with 1 argument Output
start = 0, stop before 5, step = +1
2 arguments Output
start = 1, stop before 5, step = +1
3 arguments Output
start = 1, stop before 5, step = +2
Output
start = 5, stop before 1, step = -1
2-7
Demo ( while loop statement )
Official (Open)
There is another type of loop known as while loop.
We can use a while loop to run like a for loop using a counter
counter var
test condition
Vs
update var
• for loop does everything automatically !
• Create a counter variable, i
• Set up test condition to loop , i < 3
• Update variable after each loop
2-8
Demo ( while loop statement )
Official (Open)
There is another type of loop known as while loop.
We can use a while loop to run like a for loop using a counter
while loop works like “repeated” if
counter var
test condition
update var
• Create a counter variable, i
• Set up test condition to loop , i < 3
• Update variable after each loop
2-9
Demo ( while loop statement )
Official (Open)
We usually use for loop for counter controlled loop.
When do we use while loop?
• when decision to repeat is determine during runtime (usually by the user)
• Cannot use counter control, so cannot use a for loop
• See example below
Sample Output
2-10
Exercise
Official (Open)
Write a program that prompts the user to enter a number and then
calculates the sum of all odd numbers from 1 up to that number. For
example, if the user enters 7, the output should be 16 (1+3+5+7).
Use a for loop for this.
Sample Output 1 Sample Output 2
2-11
Exercise
Official (Open)
Write a program using a while loop to repeatedly get user to enter
numbers and add them together. The loop should stop when the user
enters zero or a negative number, at which point the program will display
the total sum.
Sample Output 1 Sample Output 2
2-12
Exercise
Official (Open)
Write a program that repeatedly asks the user to enter the cost price of
an item, then calculates and displays the selling price, which is 25%
higher than the cost price. The program should handle invalid inputs with
appropriate messages (as shown in the Sample Output) and stop when
the user enters zero for the cost price.
Sample Output
Find the eLearning Topic Loop Control
Structures
Read through the following section
• break, continue statements and Else
clause
• break and continue may be useful
2-13
CoLab Exercise
Official (Open)
Use the link below to access the CoLab Exercise
[Link]
3 Exercises
• With guiding instruction
• Solution provided as well
• Can complete outside Lab
2-14
Programming Essentials
Official (Open)
Exercise
Official (Open)
In a semester, each learning unit (LU) carries some LU credits (CR). The score for each LU
determines the Grade Point (GP) for that LU. Each LU earns credit points (CP) = CR * GP.
The grade point average (GPA) is computed by total CP / total CR.
GP Score Table Sample Scores
Score GP (Grade Point)
LU Score GP (Grade Point) CR (Credit) CP (Credit Points)
80 - 100 4.0
LU 1 79 3.5 4 3.5 * 4
75 – 79 3.5
LU 2 75 3.5 4 3.5 * 4
70 – 74 3
LU 3 71 3 4 3*4
65 – 69 2.5
LU 4 63 2 8 2*8
60 – 64 2
LU 5 87 4 2 4*2
55 – 59 1.5
50 – 54 1
Total CR = ( 4 + 4 + 4 + 8 + 2 )
0 - 49 0
Total CP = ( 3.5*4 + 3.5*4 + 3*4 + 2*8 + 4*2 )
GPA = Total CP / Total CR = 2.91
2-16
Exercise
Official (Open)
Write a program to prompt the user for the LU score and credits (CR) for 5 LU. It then
computes and displays the GPA.
Sample Output
2-17
Exercise ( nested loop statement )
Official (Open)
Let’s look at the following program. Do you know what is the output?
Type it out and see if you can understand how it works.
Check with your tutor if there is any doubts.
2-18
Exercise
Official (Open)
Write a program to generated the pattern below with the height
specified by the user.
Sample Output 1 Sample Output 2
2-19
Exercise
Official (Open)
Write a program that calculates and display the average test score for
each student. The program should prompt the user to enter the
number of students enrolled in a learning unit and the number of tests
each student has taken.
Sample Output
2-20
Exercise
Official (Open)
Write a program that asks the user to enter a number and then
generates all the prime numbers before this number.
Sample Output 1 Sample Output 2
2-21
Exercise
Official (Open)
Sample Output 1
Write a fast food program that allows user
to select food to buy, quantity and price. It
also allows user to decide whether to add
more orders. The program should display
total price when it ends.
2-22
Exercise
Official (Open)
Sample Output 2
Write a fast food program that allows user
to select food to buy, quantity and price. It
also allows user to decide whether to add
more orders. The program should display
total price when it ends.
2-23
Official (Open)