0% found this document useful (0 votes)
20 views17 pages

Algoritma LOOPING

1. Python provides while and for loops to repeatedly execute a block of code. The while loop continuously executes as long as a condition is true, while the for loop iterates over items in a sequence. 2. Loops can be controlled using break, continue, and else statements. break exits the entire loop, continue skips to the next iteration, and else executes after loop completion without a break. 3. The range() function generates a sequence of numbers for use in for loops, optionally specifying the start, stop, and increment values.

Uploaded by

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

Algoritma LOOPING

1. Python provides while and for loops to repeatedly execute a block of code. The while loop continuously executes as long as a condition is true, while the for loop iterates over items in a sequence. 2. Loops can be controlled using break, continue, and else statements. break exits the entire loop, continue skips to the next iteration, and else executes after loop completion without a break. 3. The range() function generates a sequence of numbers for use in for loops, optionally specifying the start, stop, and increment values.

Uploaded by

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

Algoritma LOOPING

Pertemuan 6
Python While Loops
• Dengan menggunakan perintah looping while kita dapat menjalankan
beberapa baris perintah, selama kondisi yang disyaratkan bernilai True
• Contoh Syntax :

i = 1
while i < 6:
  print(i)
  i += 1
• Catatan: remember to increment i, or else the loop will continue
forever.
Flowchart While Start

• Pada looping while, variable i=1


counter wajib diisikan diawal
sebelum pengecekan kondisi.
F
• print(i) , dilakukan hanya jika i < 6 ?
kondisi bernilai True atau benar.
T
• I += 1 , berfungsi sebagai
incremental print(i)

Stop
i += 1
Start

The break Statement


i=1

• With the break statement we can stop the


loop even if the while condition is true: i < 6 ?
F

• Contoh : (Exit the loop when i is 3)


T

i = 1 print(i)
while i < 6:
  print(i) Stop
  if i == 3:
    break i T
  i += 1 == 3 ?

i += 1
Start

The continue Statement


i=0

• With the continue statement we can stop


the current iteration, and continue with i < 6 ?
F
the next:
T
• Contoh : (Continue to the next iteration if i
is 3) i += 1

i = 0
while i < 6: T Stop
  i += 1 i
  if i == 3: == 3 ?
    continue
  print(i) F

print(i)
The else Statement Start

• With the else statement we can run a i=1


block of code once when the condition
no longer is true F
• Contoh : (Print a message once the i < 6 ?

condition is false) T print(“i is


no longer
print(i) less than
i = 1 6”)
while i < 6:
  print(i) i += 1
  i += 1 Stop
else:
  print("i is no longer less than 6")
Python For Loops Start

fruits =
["apple", "banana",
• A for loop is used for iterating over a  "cherry"]
sequence (that is either a list, a tuple, a
dictionary, a set, or a string) For x F
• Contoh : (Print each fruit in a fruit list) in
fruits
T
Stop
fruits = ["apple", "banana", "cherry"] print(x)
for x in fruits:
  print(x)
Looping Through a String
• Even strings are iterable objects, they contain a sequence of
characters
• Contoh : (Loop through the letters in the word "banana")

for x in "banana":
  print(x)
The break Statement Start

fruits =
• With the break statement we can stop ["apple", "banana",
the loop before it has looped through  "cherry"]
all the items
• Contoh : (Exit the loop when x is For x
in
F
"banana") fruits
T
Stop
fruits = ["apple", "banana", "cherry"]
for x in fruits: print(x)
  print(x)
  if x == "banana":
    break F x ==  T
”banana”
?
The continue Statement Start

fruits =
• With the continue statement we can stop ["apple", "banana",
the current iteration of the loop, and  "cherry"]
continue with the next
• Contoh : (Do not print banana) For x
in
F

fruits
fruits = ["apple", "banana", "cherry"] T
for x in fruits: Stop
  if x == "banana": T
x == 
    continue ”banana”
  print(x) ?
F

print(x)
Increment

The range() Function - 1 by 1

0≤x<6
• To loop through a set of code a specified
number of times, we can use the range() Start
function
• The range() function returns a sequence of For x = 0 F
numbers, starting from 0 by default, and to 5
increments by 1 (by default), and ends at a T Stop
specified number.
print(x)
• Contoh : (Using the range() function)

for x in range(6):
  print(x) Note that range(6) is not the values
of 0 to 6, but the values 0 to 5
The range() Function - 2
2≤x<6
• The range() function defaults to 0 as a
starting value, however it is possible to Start
specify the starting value by adding a
parameter: range(2, 6), which means values F
For x = 2
from 2 to 6 (but not including 6) to 5

T Stop
for x in range(2, 6): print(x)
  print(x)

Increment
by 1
The range() Function - 3
2 ≤ x < 30

• The range() function defaults to increment


Start
the sequence by 1, however it is possible
to specify the increment value by adding a
third parameter: range(2, 30, 3) For x = 2 F
to 29

T Stop

for x in range(2, 30, 3): print(x)
  print(x)

Increment
by 3
Else in For Loop - 1
0≤x<6
• The else keyword in a for loop specifies a
block of code to be executed when the Start
loop is finished
• Contoh : (Print all numbers from 0 to 5, and For x = 0 F
print a message when the loop has ended) to 5
print(“Fin
T
ally
for x in range(6): finished!”)
  print(x) print(x)
else:
  print("Finally finished!")
Stop
Else in For Loop - 2 0≤x<6

Start
• The else block will NOT be executed if the
loop is stopped by a break statement
• Contoh : (Break the loop when x is 3, and For x = 0 F
to 5
see what happens with the else block)
print(“Fin
T
for x in range(6): ally
  if x == 3: break finished!”)
  print(x)
else: If x == 3
T
  print("Finally finished!")
Stop
F

print(x)
Start

Nested Loops adj =


[“red", “big", “tasty"]

• A nested loop is a loop inside a loop. fruits =


["apple", "banana",
• The "inner loop" will be executed one  "cherry"]
time for each iteration of the "outer
loop“ F
For x
• Contoh : (Print each adjective for every in adj
fruit) T Stop
adj = ["red", "big", "tasty"] F For y
fruits = ["apple", "banana", "cherry"] in
fruits
for x in adj:
T
  for y in fruits:
    print(x, y) print(x, y)
Referensi
• https://www.w3schools.com/python/python_while_loops.asp
• https://www.w3schools.com/python/python_for_loops.asp
• https://problemsolvingwithpython.com/09-Loops/09.04-Flowcharts-D
escribing-Loops/

You might also like