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.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
R for Macroecology Aarhus University, Spring 2011.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Programming Types of Testing.
CS0004: Introduction to Programming Repetition – Do Loops.
The Little man computer
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Computer Science 1620 Loops.
CSC Intro. to Computing Lecture 17: Even More Robotran!
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Adding Automated Functionality to Office Applications.
General Programming Introduction to Computing Science and Programming I.
MOM! Phineas and Ferb are … Aims:
PHP meets MySQL.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
C++ crash course Class 8 statements, sort, flight times program.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
BEGINNING MULTIPLICATION BASIC FACTS Multiplication is REPEATED ADDITION. It is a shortcut to skip counting. The first number in the problem tells.
CSC Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
Programming, an introduction to Pascal
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Intro to Programming Web Design ½ Shade Adetoro. Programming Slangs IDE - Integrated Development Environment – the software in which you develop an application.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Using Lists Games Programming in Scratch. Games Programming in Scratch Extension – Using Lists Learning Objectives Create a temporary data store (list)
CSC Intro. to Computing Lecture 26: XML & Course Review.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Application development with Java Lecture 6 Rina Zviel-Girshin.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
COMP Loop Statements Yi Hong May 21, 2015.
Algorithms and Pseudocode
GCSE Computing: Programming GCSE Programming Remembering Python.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
CS 101 – Oct. 7 Solving simple problems: create algorithm Structure of solution –Sequence of steps (1,2,3….) –Sometimes we need to make a choice –Sometimes.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
Learning Javascript From Mr Saem
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.
The Little man computer
REPETITION CONTROL STRUCTURE
Loops BIS1523 – Lecture 10.
Counted Loops.
Siti Nurbaya Ismail Senior Lecturer
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Python I/O.
Introduction to pseudocode
In Class Program: Today in History
Algorithms & Pseudocode
CS190/295 Programming in Python for Life Sciences: Lecture 6
Iteration: Beyond the Basic PERFORM
Loops CIS 40 – Introduction to Programming in Python
Text Analyzer BIS1523 – Lecture 14.
Loop Strategies Repetition Playbook.
Computing Fundamentals
Dry Run Fix it Write a program
Presentation transcript:

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