0% found this document useful (0 votes)
15 views

Lecture Module 4 - Repeating Parts

The document discusses repeating structures in Python programming such as loops. It covers while loops, which repeat as long as a condition is true, and for loops, which repeat a specific number of times. It also discusses exit conditions like pass, break, and continue that control loop execution. Examples are provided to demonstrate while loops, for loops, and using break, continue, and pass in loops. The document aims to help students understand and implement repeating structures in their Python programs.

Uploaded by

Marko Refianto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lecture Module 4 - Repeating Parts

The document discusses repeating structures in Python programming such as loops. It covers while loops, which repeat as long as a condition is true, and for loops, which repeat a specific number of times. It also discusses exit conditions like pass, break, and continue that control loop execution. Examples are provided to demonstrate while loops, for loops, and using break, continue, and pass in loops. The document aims to help students understand and implement repeating structures in their Python programs.

Uploaded by

Marko Refianto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

MEN3221221

Basic Programming in Python

Lecture Module 4: Repeating Parts

Prof. Dr. Ir. Supriyono


Prof. Dr. Ir. Waluyo Adi Siswanto
Learning Outcomes

➢ Understand the need of repeating structure


➢ Implement while, for, and exit conditions in the program
➢ Implement nested loop in the program

Lecture Module 4 Repeating Parts


Topics

➢ Repeating structure
➢ While Loop
➢ For Loop
➢ Exit condition within loops (pass, break,
continue)
➢ Nested loop

Lecture Module 4 Repeating Parts


Repeating Structure

Why need repeating program?

For example:
You need to convert 50 students’ results data to
grade. The calculation method for each student is the
same.

If we can repeat the calculation for each student,


there should be very efficient way.

Lecture Module 4 Repeating Parts


There are two possibilities of repeating structure

process process

The process looping The process looping


continues until continues according
certain condition to the predefined
then it will stop. counts then it will
stop.

Lecture Module 4 Repeating Parts


Loops are a fundamental concept in Python
programming (and in programming in general) that
allow you to repeat a block of code multiple times.

In Python, there are two types of loops:


a) while loop (A Condition-Controlled Loop)
b) for loop (A Count-Controlled Loop)

Lecture Module 4 Repeating Parts


While loop

true
Condition process

false

process

while condition:
# Code to be repeated as long as the condition is true

The process looping Try this simple program and learn from the result
continues until
certain condition for i in range(1, 6):
then it will stop. print(i)

print('end program')

Lecture Module 4 Repeating Parts


For loop

Predefined items/counts

false
Last item/count reached process

true

process

for item in iterable :


# Code to be repeated for each item

The process looping Try this simple program and learn from the result
continues according
to the predefined for i in range(1, 6):
counts then it will print(i)
stop. print('end program')

Lecture Module 4 Repeating Parts


Example 4-1

Try these simple programs and learn from the results

count = 0
while (count < 9): i=1
print ("The count is: ", count) while i <= 5:
count = count + 1 print(i)
I += 1 # this is identical with i = i +1
print ("Good bye!")
print ("Good bye!")

# program to calculate the sum of numbers


# until the user enters zero

total = 0

number = int(input('Enter a number: '))

# add numbers until number is zero


while number != 0:
total += number # total = total + number

# take integer input again


number = int(input('Enter a number: '))

print('total =', total)

Lecture Module 4 Repeating Parts


Example 4-2

Try these simple programs and learn from the results

for i in range(1, 6):


print(i)

print('end program')

#Contoh pengulangan for sederhana


angka = [1,2,3,4,5]
for x in angka:
print(x)

print('end program')

#Contoh pengulangan for


buah = ["nanas", "apel", "jeruk"]
for makanan in buah:
print ("Saya suka makan", makanan)

print('end program')

Lecture Module 4 Repeating Parts


Exit Condition

The exit control statements are essential for


controlling the flow of your loops and making your
code more flexible and efficient.

pass is mainly used for code structure and
readability, while

break and continue are used to control the
execution of loops based on specific conditions.

Lecture Module 4 Repeating Parts


Exit Condition
pass
The pass statement is a placeholder in Python. It
is used when a statement is syntactically required
but you don't want it to do anything. It is often
used as a temporary placeholder when defining
functions, loops, or conditional statements that
you plan to implement later.
Try this simple program and learn from the result

for i in range(5):
if i == 2:
pass # Placeholder, does nothing
else:
print(i)

Lecture Module 4 Repeating Parts


break
The break statement is used to exit a loop
prematurely when a certain condition is met. It is
commonly used within loops to terminate the
loop's execution.

Try this simple program and learn from the result


for i in range(10):
if i == 7:
break # Exit the loop when i is 5
print(i)

print('outside loop')

Lecture Module 4 Repeating Parts


continue
The continue statement is used to skip the current
iteration of a loop and move to the next iteration. It
is often used when you want to skip certain
iterations based on a condition without terminating
the entire loop.

Try this simple program and learn from the result


for i in range(5):
if i == 2:
continue # Skip the iteration
else:
print(i)

Lecture Module 4 Repeating Parts


Example 4-3

You have a program to classify a triangle and to calculate the area of the triangle
based on Heron’s formula in Example 3-7.

Now you want to improve the program so you can repeat for different data, until
you want to stop (no more data), by using While structure.

Lecture Module 4 Repeating Parts


Initial value of
number_control

The loop will keep on


running unless the
value of
number _control = 0

If you enter
number_control = 0
then the program will
not request another
data

Lecture Module 4 Repeating Parts


Now run directly (click or double click) from the folder.
The program run and continues the next entry at the
bottom,

Lecture Module 4 Repeating Parts


Add these two
commands.

Then run it again


directly from the
folder, try several
times.

What is the
difference with the
previous one?

Lecture Module 4 Repeating Parts


Example 4-4

Write a program to calculate the average of examination results of maximum 50


students.

- The entry is name and results (0-100)


- You can stop the entry if the data already complete, although not reaching 50

Lecture Module 4 Repeating Parts


Lecture Module 4 Repeating Parts
Please add the feature of the program to make
sure the data_nilai is in correct range
from 0 to 100
if out the range you need to re-enter the data

Lecture Module 4 Repeating Parts


Lecture Module 4 Repeating Parts
Example 4-5

Now add the feature in example 4-4 to classify the triangle by the angle.
The calculation of the angle by using cosine rules

Lecture Module 4 Repeating Parts


Lecture Module 4 Repeating Parts
Lecture Module 4 Repeating Parts
Nested Loop

A loop that is inside another loop is called a nested loop.


loop

Python programming language allows the use of one loop inside another loop. The
following sections show some examples to illustrate the concept.

A clock is a good example of some-thing that works like a nested loop.


The second hand,
hand minute hand,
hand and hour hand all spin around the face of the
clock. The hour hand, however, only makes 1 revolution for every 12 of the minute
hand’s revolutions. And it takes 60 revolutions of the second hand for the
minute hand to make 1 revolution. This means that for every complete revolution of
the hour hand, the second hand has revolved 720 times.

Let’s see the program at works

Lecture Module 4 Repeating Parts


Example 4-6
Create a simulation program of a clock following the flowchart

Lecture Module 4 Repeating Parts


Example 4-7

Write a code to help a teacher to calculate the average of each student tests in the
class.

The number of the students and the number of the test should be defined in the
program by entering the number than used in program as range of for nested loop.

The average score for each student should be calculated

The overall average should be calculated

Lecture Module 4 Repeating Parts


Try to improve the program by adding the capability to make sure the entry score for
each students is in the range of 0-100, if out of range should repeat the entry

Lecture Module 4 Repeating Parts


Assignment 1

Write a program to find the roots of quadratic


equations by using abc formulas. Yor code should
accommodate many features, for example

Check the discriminant should not be negative

The program can be repeated if user wants to
calculate another data

You have to think the input and output be nice and
tidy

Be creative to write so that the program becomes
user friendly.

Lecture Module 4 Repeating Parts


Lecture Module 4 Repeating Parts

You might also like