Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views24 pages

Lecture3 Ed

The document covers programming concepts including looping structures (while and for loops), flow-charting, and the use of break statements. It also introduces turtle graphics, a method for teaching programming through visual representation. Examples demonstrate how to implement these concepts in Python, including finding the square of an integer and drawing shapes with turtle graphics.
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)
4 views24 pages

Lecture3 Ed

The document covers programming concepts including looping structures (while and for loops), flow-charting, and the use of break statements. It also introduces turtle graphics, a method for teaching programming through visual representation. Examples demonstrate how to implement these concepts in Python, including finding the square of an integer and drawing shapes with turtle graphics.
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/ 24

Logika Pemrograman

Pertemuan 3
Pertemuan 3
1. Looping 3. Introduction to turtle
graphics
• Looping: introduction
• while loop
• break statement
• for loop

2. Flow-chart
Looping: introduction
• When we want a program to do the same thing many
times, we can use iteration. Code

• A generic iteration (also called looping) mechanism is Loop


shown in the box. False
Test
• Like a conditional statement, it begins with a test. True

• If the test evaluates to True, the program executes the Loop


loop body once, and then goes back to reevaluate the body
test.

• This process is repeated until the test evaluates to


False, after which control passes to the code following
the iteration statement.
Code
while loop Start

Input (x)
• We can write the kind of loop depicted in the
flowchart using a while statement. y=0
• Example: write a program to find the square of an k=0
integer (the square of x = y) Loop
False
# Program to find the square of an integer k<x
# y = x^2, k: number of iteration True
x = int(input(“Enter x: ")) y=y+x
y = 0 k=k+1
k = 0 𝑛
while (k < x): ෍ 𝑛 = 𝑛 + 𝑛 + ⋯ = 𝑛2
y = y + x 𝑘=1
k = k + 1 Output (y)

print(f"x**2 = {y}") End


while loop
• There are three cases to consider: x == 0, x > 0, and x < 0.

• Suppose x == 0. The initial value of k will also be 0, and the loop body will
never be executed.

• Suppose x > 0. The initial value of k will be less than x, and the loop body
will be executed at least once.

• Suppose x < 0, the result is wrong


Enter x: -3
x**2 = 0
while loop
# Program to find the square of an integer
# y = x^2, k: number of iteration

x = int(input(“Enter x: "))
y = 0
k = 0
while (k < abs(x)):
y = y + x
k = k + 1

print(f"x**2 = {y}")

Enter x: -3
x**2 = -9 the result is still wrong
while loop
# Program to find the square of an integer
# y = x^2, k: number of iteration

x = int(input("Masukkan x: "))
y = 0
k = 0
while (k < abs(x)):
y = y + abs(x)
k = k + 1

print(f"x**2 = {y}")

Masukkan x: -3
x**2 = 9 the result is correct
break statement Code
Loop
• It is sometimes convenient to exit a loop
without testing the loop condition. False
Test
• Executing a break statement terminates the True
loop in which it is contained and transfers
True
control to the code immediately following the Break?
loop.
False
Loop
body

Code
break statement
Without break statement:
#Program to find a positive integer that is
#divisible by both 11 and 12
x = 1
while (x < 300):
if x%11 == 0 and x%12 == 0:
print(x, 'is divisible by 11 and 12')
x = x + 1

132 is divisible by 11 and 12


264 is divisible by 11 and 12
break statement
With break statement:
#Program to find a positive integer that is
#divisible by both 11 and 12
x = 1
while (x < 300):
if x%11 == 0 and x%12 == 0:
print(x, 'is divisible by 11 and 12')
break
x = x + 1
132 is divisible by 11 and 12
break statement
With break statement:
#Program to find a positive integer that is
#divisible by both 11 and 12
x = 1
while True:
if x%11 == 0 and x%12 == 0:
print(x, 'is divisible by 11 and 12')
break
x = x + 1
132 is divisible by 11 and 12

while True: → If we write it then the loop will run forever (except with
break statement).
for loop
• The general form of a for statement is (recall that the words in italics are descriptions
of what can appear, not actual code):
total = 0
for variable in sequence: for num in (77, 11, 3):
code block total = total + num
print(total)

• The variable following for is bound to the first value in the sequence, and the code
block is executed.
• The variable is then assigned the second value in the sequence, and the code block is
executed again.
• The process continues until the sequence is exhausted or a break statement is
executed within the code block.
for loop
• The sequence of values bound to a variable is most commonly generated using the
built-in function range, which returns a series of integers.

range (start, stop, step)

range (5, 40, 10) → yields the sequence 5, 15, 25, 35

• If the first argument to range is omitted, it defaults to 0, and if the last argument
(the step size) is omitted, it defaults to 1

range (3) → yields the sequence 0, 1, 2

range (0, 3) → yields the sequence 0, 1, 2


for loop
x = 4
for i in range(x):
print(i)

It prints:
0
1
2
3
for loop
# Program to find the square of an integer
# y = x**2, k : number of iteration

x = int(input("Enter x: "))
y = 0

for k in range (abs(x)):


y = y + abs(x)

print(f"x**2 = {y}")

Enter x: -3
Notice that unlike the while loop implementation, the number
x**2 = 9 of iterations is not controlled by an explicit test, and the index
variable k is not explicitly incremented.
for loop
requested_toppings = ['mushrooms', 'pepperoni', 'olives', 'tuna']

for requested_topping in requested_toppings:


print(f"Adding {requested_topping}.")

print("Finish making your pizza!")

It prints:
Adding mushrooms.
Adding pepperoni.
Adding olives.
Adding tuna.
Finish making your pizza!
for loop
requested_toppings = ['mushrooms', 'pepperoni', 'olives', 'tuna']

for requested_topping in requested_toppings:


if requested_topping =='mushrooms':
print("Sorry, we are out of mushrooms right now.")
else:
print(f"Adding {requested_topping}.")
print("Finish making your pizza!")

It prints:
Sorry, we are out of mushrooms right now.
Adding pepperoni.
Adding olives.
Adding tuna.
Finish making your pizza!
for loop
available_toppings = ['mushrooms', 'olives', 'green peppers',
'pepperoni', 'pineapple', 'extra cheese']

requested_toppings = ['mushrooms', 'french fries', 'extra


cheese']

for requested_topping in requested_toppings:


if requested_topping in available_toppings:
print(f"Adding {requested_topping}.")
else:
print(f"Sorry, we don't have {requested_topping}.")

print("Finished making your pizza!")

Each item in requested_toppings is checked against the list of


available_toppings before it’s added to the pizza:
Introduction to turtle graphics
• Turtle graphics is a popular way for introducing programming to kids. It
was part of the original Logo programming language developed by Wally
Feurzig and Seymour Papert in 1966.

• Imagine a robotic turtle starting at (0, 0) in the x-y plane.

• After an import turtle, give it the command turtle.forward(15), and it


moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as
it moves. Give it the command turtle.right(25), and it rotates in-place 25
degrees clockwise.

• By combining together these and similar commands, intricate shapes and


pictures can easily be drawn.
Forward and Turn
• Write the following code to call turtle in python.
from turtle import *
Shape ('turtle')

• then write the following code to see turtle function working

forward(50)
left(90)
backward(100)
right(45)
Turtle Shortcuts
fd = forward lt = left

bk = backward rt = right
• Try!
fd(50)
lt(90)
bk(100)
rt(45)
The Square
• Try to draw a square of side 80
Additional functions & features
• Use the online Python docs to look up some additional
functions and features you can use with turtle graphics

(http://docs.python.org/2/library/turtle.html )
Quiz
• Draw this house using turtle:

You might also like