CSC Intro. to Computing Lecture 13: PALGO
Announcements Midterm is in one week Time to start reviewing We will do more review in class Tuesday
Loops Computers are fast, but VERY stupid Good for simple computations: computing what happens when atomic bombs detonates Bad at complex ideas: designing better atomic bombs; not needing to explode bombs Use computers to take advantage of this Problems requiring lots of little calculations Choosing best option from MANY possibilities
Loops Do not want to write same instructions over and over and over again 90% 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 = 5 y = 3 total = 0 repeat y times total = total + x end print total
Tracing a Loop 1 x = 5 2 y = 3 3 total = 0 4 repeat y times 5 total = total + x 6 end 7 print total Lin e Cond- ition xy tota l output of of of Lin e Cond- ition xy tota l output Lin e Cond- ition xy tota l output 15 Lin e Cond- ition xy tota l output 23 Lin e Cond- ition xy tota l output 30 Lin e Cond- ition xy tota l output 41 of 3 Lin e Cond- ition xy tota l output 55 Lin e Cond- ition xy tota l output 42 of 3 Lin e Cond- ition xy tota l output 510 Lin e Cond- ition xy tota l output 43 of 3 Lin e Cond- ition xy tota l output Cond- ition 515
For loops For loops are similar to repeat loops, but they use a variable to count the loops Can loop between any two values
Tracing a for loop What would this code do? num = input_fact(“What number?”) fact = 1 for i = 1 to num fact = fact * i end print fact
Tracing n! 1 num = input_num(“What number?”) 2 fact = 1 3 for i = 1 to num 4 fact = fact * i 5 end 6 print fact Line Cond- ition nu m factiOutput Cond- ition < 41 Cond- ition < 42 Cond- ition < 43 Cond- ition < 44 Cond- ition Line Cond- ition nu m factiOutput < < < < < 45 Line Cond- ition nu m factiOutput 31 < < < < <
While loops While loops iterate while a condition is met Can be as many, or as few, times as needed Values can change within the loop Can convert for, repeat, & while loops Not always easy to change one to another Usually choose the loop that makes the most sense
More code examples What will this print out? n = 10 while n > 0 if n != 1 then print n + “ bottles of beer on the wall” else print n + “ bottle of beer” end end
Oops!
More code examples n = 10 while n > 0 if n != 1 then print n + “ bottles of beer on the wall” else print n + “ bottle of beer” end n = n - 1 end
Success!
Coding Loops Any code is allowed within a loop Can even write loops or if-then-else-end statements within loops But what if wanted to remember something from every pass of a loop Need new variable for each piece of data Excel: 16,384 rows x 256 columns Over 4,000,000 different variables!
Lists Lists are variables which can hold multiple pieces of data at once Lists are similar to columns in Excel Each list/column includes numbered locations While each location can hold only 1 value, the list can include many locations
Lists To be used, a list must be assigned to a variable These 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 order scores[0] = 100 scores[4] = 99 Important: Lists are numbered from 0 First assignment must be to location “0”
Using Lists Get number of locations in a list using length function len = 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(“1 st score?”) data[1] = input_num(“2 nd score?”) data[2] = input_num(“3 rd score?”) sum = 0 for i = 0 to length(data)-1 sum = sum + data[i] end avg = 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 = 0 data = new list() num = input_number(“Next score?”) stupidName = num while (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 midterm Given in class on Mar. 9 th Continue playing with Palgo Run the demo programs Try seeing what happens if you enter some of the code from the handout Try writing a program of your own