Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 110 - Intro. to Computing Lecture 13: PALGO. Announcements Midterm is in one week  Time to start reviewing  We will do more review in class Tuesday.

Similar presentations


Presentation on theme: "CSC 110 - Intro. to Computing Lecture 13: PALGO. Announcements Midterm is in one week  Time to start reviewing  We will do more review in class Tuesday."— Presentation transcript:

1 CSC 110 - Intro. to Computing Lecture 13: PALGO

2 Announcements Midterm is in one week  Time to start reviewing  We will do more review in class Tuesday

3 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

4 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

5 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

6 Repeat loops

7 Repeat Loops 99x

8 Variables in repeat loops

9 More than Movie References What would this code do? x = 5 y = 3 total = 0 repeat y times total = total + x end print total

10 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 15 23 30 41 of 3 55 42 of 3 510 43 of 3 515 7 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

11 For loops For loops are similar to repeat loops, but they use a variable to count the loops  Can loop between any two values

12 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

13 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 14 21 31 < 41 Cond- ition 41 32 < 42 Cond- ition 42 33 < 43 Cond- ition 46 34 < 44 Cond- ition 3 424 Line Cond- ition nu m factiOutput 21 31 < 41 41 32 < 42 42 33 < 43 46 34 < 44 424 35 < 45 Line Cond- ition nu m factiOutput 31 < 41 41 32 < 42 42 33 < 43 46 34 < 44 424 35 < 45 624

14 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

15 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

16 Oops!

17 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

18 Success!

19 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!

20 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

21 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

22 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

23 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”

24 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]

25 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

26 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

27 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

28 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

29 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


Download ppt "CSC 110 - Intro. to Computing Lecture 13: PALGO. Announcements Midterm is in one week  Time to start reviewing  We will do more review in class Tuesday."

Similar presentations


Ads by Google