For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

TE Sessions Supported by: Basic Concepts of Programming November 3, 2012.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
While Loops. Challenge: ● Ask the user a simple math questions ● Continue asking the question until the user gets it right.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Repeating Actions While and For Loops
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
 Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control.
Copyright © Texas Education Agency, Computer Programming For Loops.
1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
27/05/ Iteration Loops Nested Loops & The Step Parameter.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Counting Loops.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Basic Conditions. Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
CSD 340 (Blum)1 For Loops See Beginning JavaScript (Paul Wilton) p. 87.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Loops. Review Control Structure – making decisions if-statement Program Design Understand what you want to do Design your solution Write and test your.
Repetition In today’s lesson we will look at: why you would want to repeat things in a program different ways of repeating things creating loops in Just.
 See pages 65 – 67 in your book  While loops are all around us ◦ Shampoo  Rinse, Lather, Repeat  While (Condition Is True) : ◦ Execute a block of.
PYTHON WHILE LOOPS. What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Slides by Evan Gallagher
Slides by Evan Gallagher
Understanding the Geometric Shape Code
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Java's for Statement.
While Loops Chapter 3.
ITM 352 Flow-Control: Loops
Introducing Do While & Do Until Loops & Repetition Statements
Problem Solving and Programming CS140: Introduction to Computing 1 8/21/13.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Lecture Notes – Week 3 Lecture-2
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Nested Loops & The Step Parameter
Case & Repetitive Statements
Instructor: Craig Duckett
Conditional Loops Counted Loops
Python While Loops.
The Step Parameter & Nested For … To … Next loops
SEQUENCE Start Initial situation Step 1 Step 2 Step 3 End
Presentation transcript:

For Loops

Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output statement!

Loops ● A loop is used for repetitive behaviour ● A set of commands is placed in a code block ● A condition determines when the block stops repeating

The For Loop ● Used when you know how many times something will happen ● Great for counting ● Also often used with lists

Parts of a For Loop ● Sentry Variable ● Initial Value ● Looping Condition ● Increment Statement

Sentry Variable ● A special variable designed to control the loop ● In for loops, usually an integer ● Use a meaningful name when practical ● Traditionally use i if no other name makes sense

Initial Value ● Give sentry some meaningful initial value ● Usually 0 or 1

Looping Condition ● A condition ● Always involves sentry variable ● Indicates when loop should continue ● If condition is true, loop will repeat ● When condition becomes false, loop will end

Increment Statement ● A line of code ● Always involves sentry variable ● Changes value of sentry ● Usually i++, i--, I += something ● Must make it possible for condition to become false eventually

Racer Algorithm New Program Racer by me New integer variable lap starts at 1 For loop with lap going from 1 to 10 by 1 Output “Now on lap: “ + lap End For loop End racer

Challenge: Backwards Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● This time go backwards! ● Make X vary, so it says 10, then 9, then 8… ● Use only one output statement!

Counting Backwards ● Use same for loop elements ● Use larger value for initial sentry value ● Decrement variable each time through ● Check for smaller result

Backwards Racer Algorithm New Program backRacer by me New integer variable lap starts at 10 For loop with lap going from 10 to 1 by -1 Output “Now on lap: “ + lap End For loop End racer

Counting by 5 Algorithm New Program byFive by me New integer variable lap starts at 0 For loop with lap going from 0 to 50 by 5 Output “Now on lap: “ + lap End For loop End racer

Code Tracing ● Make a chart ● Make each variable a column head ● Make a column for each condition ● Make a column for output

Code Tracing Cont’d ● Walk through code one line at a time ● Each time a variable changes, change it on the chart ● Each time you get to a condition statement, evaluate the condition

Code Tracing III ● For conditions, write TRUE or FALSE ● Write any output

Code Tracing Example

Ordinary Slide ● Level 1 – Level 2 ● Level 3

Code My code More Code