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.

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Introduction to Computing Science and Programming I
Repeating Structures Do While Loops. Do While Loop While loops have a test condition before the loop –This means that java will test the condition before.
Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the.
ITC 240: Web Application Programming
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Tell the robot exactly how to draw a square on the board.
An Introduction to Textual Programming
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
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.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
By the end of this session you should be able to...
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
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.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
 There are times when you will want blocks to repeat. Instead of duplicating blocks and ending up with a long script that might be confusing, there.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
BO65: PROGRAMMING ITERATION 1. Starter  Write down what you think the code below outputs on screen: Dim intCounter as integer intCounter = 0 Do while.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Getting Started With Python Brendan Routledge
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
FOP: While Loops.
Trace Tables In today’s lesson we will look at:
CS161 Introduction to Computer Science
Functions and Procedures
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 for Beginners.
CS1371 Introduction to Computing for Engineers
Chapter 5: Repetition Structures
Topics Introduction to Repetition Structures
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Algorithms Today we will look at: what the word algorithm means
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Introduction to pseudocode
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Looping and Random Numbers
Loops CIS 40 – Introduction to Programming in Python
Repetition Structures
Module 4 Loops.
Topics Introduction to Repetition Structures
Repetition In today’s lesson we will look at:
Loop Strategies Repetition Playbook.
Computer Science Core Concepts
Flowcharts and Pseudo Code
A LESSON IN LOOPING What is a loop?
GCSE Computing:: While Loops
While Loops in Python.
Presentation transcript:

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 BASIC

Repetition Imagine you wanted to print the word “hello” ten times... You could do it like this: print “hello” print “hello” print “hello” print “hello” print “hello” Is that a sensible way to do it? What about if you change your mind about what you want it to say? What about if you want to change the number of times you say it?

Sometimes you might want to repeat a section of your program… a fixed number of times until something happens while a particular condition exists forever! In programming, these are called loops Repetition

How many times do you need to repeat to: choose lottery numbers for a “lucky dip”? print a list of all of the square numbers less than 100? ask the user for a number and make sure that it’s between 1 and 10? Examples

In Just BASIC there are three types of loop: FOR... NEXT WHILE... WEND DO... UNTIL/WHILE (most languages have similar loops, but often the bottom one is called REPEAT...) BASIC

A FOR... NEXT loop is probably the most common and most useful, e.g. FOR x = 0 to 100 STEP 10 PRINT x NEXT You can use any number variable in place of x You only need the STEP part if you are counting down, or counting up in steps other than 1 You can use the value of the variable inside the loop It is traditional to indent lines of code inside the loop FOR Example

A WHILE loop looks like this: x = 1 WHILE x*x < 100 print x; " squared is "; x*x x = x + 1 WEND Rather than repeating a fixed number of times, it repeats while a condition – in this case that the square is less than 100 – is true. The check is done at the start so the bit inside the loop may never run at all WHILE Example

A DO WHILE loop looks like this: DO input "Give me a number from 1 to 10: "; x LOOP WHILE x 10 print "Thank you" With a DO loop the test condition comes at the end, so it always loops at least once Could you do this same example with a WHILE loop? What would happen? DO WHILE Example

A DO UNTIL loop looks like this: DO UNTIL x >= 1 AND x <= 10 input "Give me a number from 1 to 10: "; x LOOP print "Thank you" This program does the same as the last one, but the condition is the opposite way round Because they are interchangeable, not all programming languages have both WHILE and REPEAT UNTIL loops DO UNTIL Example

An algorithm is a plan for a program Think about what the task requires in everyday terms before you rush to program it, e.g. –Ten green bottles –Times tables –The Twelve Days of Christmas Algorithm First!