Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | amice-owens |
View: | 213 times |
Download: | 0 times |
CSC 110 -Intro. to Computing
Lecture 13:
PALGO
Announcements
Midterm is in one weekTime to start reviewingWe will do more review in class Tuesday
Loops
Computers are fast, but VERY stupidGood for simple computations: computing what
happens when atomic bombs detonatesBad at complex ideas: designing better atomic
bombs; not needing to explode bombs Use computers to take advantage of this
Problems requiring lots of little calculationsChoosing best option from MANY possibilities
Loops
Do not want to write same instructions over and over and over again90% of time spent running 10% of code
Instead, loops tell computer to execute certain instructions repeatedly
Loops in Palgo
Palgo contains three different loops:repeat
Execute commands a fixed number of times
for Execute commands a fixed number of times and
record how many times execution has occurred
while Execute commands while some condition applies
Repeat loops
Repeat Loops
99x
Variables in repeat loops
More than Movie References
What would this code do?
x = 5y = 3total = 0repeat y times total = total + xendprint total
Tracing a Loop
1 x = 52 y = 33 total = 04 repeat y times5 total = total + x6 end7 print total
Line
Cond-ition
x ytotal
output
1 5
2 3
3 0
4 1 of 3
5 5
4 2 of 3
5 10
4 3 of 3
5 15
7 15
Line
Cond-ition
x ytotal
outputLine
Cond-ition
x ytotal
output
1 5
Line
Cond-ition
x ytotal
output
2 3
Line
Cond-ition
x ytotal
output
3 0
Line
Cond-ition
x ytotal
output
4 1 of 3
Line
Cond-ition
x ytotal
output
5 5
Line
Cond-ition
x ytotal
output
4 2 of 3
Line
Cond-ition
x ytotal
output
5 10
Line
Cond-ition
x ytotal
output
4 3 of 3
Line
Cond-ition
x ytotal
outputCond-ition
5 15
For loops
For loops are similar to repeat loops, but they use a variable to count the loopsCan loop between any two values
Tracing a for loop
What would this code do?
num = input_fact(“What number?”)fact = 1for i = 1 to num fact = fact * iendprint fact
Tracing n! 1 num = input_num(“What number?”)
2 fact = 13 for i = 1 to num4 fact = fact * i5 end6 print fact
LineCond-ition
num
fact i OutputCond-ition
1 4
Cond-ition
2 1
Cond-ition
3 1 < 4 1
Cond-ition
4 1
Cond-ition
3 2 < 4 2
Cond-ition
4 2
Cond-ition
3 3 < 4 3
Cond-ition
4 6
Cond-ition
3 4 < 4 4
Cond-ition
3
4 24
LineCond-ition
num
fact i Output
2 1
3 1 < 4 1
4 1
3 2 < 4 2
4 2
3 3 < 4 3
4 6
3 4 < 4 4
4 24
3 5 < 4 5
LineCond-ition
num
fact i Output
3 1 < 4 1
4 1
3 2 < 4 2
4 2
3 3 < 4 3
4 6
3 4 < 4 4
4 24
3 5 < 4 5
6 24
While loops
While loops iterate while a condition is metCan be as many, or as few, times as neededValues can change within the loop
Can convert for, repeat, & while loops Not always easy to change one to anotherUsually choose the loop that makes the most
sense
More code examples
What will this print out? n = 10while n > 0 if n != 1 then print n + “ bottles of beer on
the wall” else print n + “ bottle of beer”
endend
Oops!
More code examples
n = 10while n > 0 if n != 1 then print n + “ bottles of beer on the wall”
else print n + “ bottle of beer”
end n = n - 1end
Success!
Coding Loops
Any code is allowed within a loopCan even write loops or if-then-else-end
statements within loops But what if wanted to remember something
from every pass of a loopNeed new variable for each piece of dataExcel: 16,384 rows x 256 columns
Over 4,000,000 different variables!
Lists
Lists are variables which can hold multiple pieces of data at onceLists are similar to columns in ExcelEach list/column includes numbered locationsWhile each location can hold only 1 value, the
list can include many locations
Lists
To be used, a list must be assigned to a variableThese variable follow the normal Palgo rules of
variables, however Assign to students a new list by:
students = list() Initially this list is empty (does not have any “cells”) It grows as we assign new data into it
Access data in lists
Add to a list by assigning its locations:students[0] = “Ashley”students[1] = “Sarah”students[2] = “David”students[3] = “Lisa”
Use these locations like a normal variable So, print students[2] would make
Palgo print out Lisa
Using Lists
Have to create locations in a list in orderscores[0] = 100scores[4] = 99
Important: Lists are numbered from 0First assignment must be to location “0”
Using Lists
Get number of locations in a list using length functionlen = length(scores)assigns to num
the number of elements in the list scores
Since lists are numbered from 0, last defined location in scores would be scores[length(scores)-1]
Looping over lists
Lists are really made for loops Could compute sum and average by:data = new list()data[0] = input_num(“1st score?”)data[1] = input_num(“2nd score?”)data[2] = input_num(“3rd score?”)sum = 0for i = 0 to length(data)-1 sum = sum + data[i]endavg = sum / length(data)print “Total ” + sum + “Average “ + avg
Palgo Loop Selection
Each of Palgo’s loops serves different purpose repeat
Executing the commands a specific number of times for
Remembering the number of times that we are executing the instructions in a loop
Very useful for lists! while
Executing a loop for as long as needed to meet a specific condition
Your Turn: Tracing Practice
Show program trace if input was (in order):94, 34, 67, 90, 97, -1 i = 0data = new list()num = input_number(“Next score?”)stupidName = numwhile (num != -1) data[i] = num if stupidName > num then stupidName = num end i = i + 1 num = input_number(“Next score?”)end
Your Turn
Write a program which has the user input the number of hits for each player on a baseball team (only 9 players on a team) Store the values in a list
Once the user has entered all the hits, use a loop to find the highest number of hits any player hit and print this value out
For Next Lecture
Continue getting ready for the midtermGiven in class on Mar. 9th
Continue playing with PalgoRun the demo programsTry seeing what happens if you enter some of
the code from the handoutTry writing a program of your own