Iteration: FOR, DO, LOOP Loop Damian Gordon. FOR Loop The FOR loop does the same thing as a WHILE loop but is easier if you are using the loop to do a.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Neal Stublen Loop Structure  Sometimes it’s useful to repeat yourself  The same actions need to occur more than once  Ensures.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Flow Charting, Structured English and PseudoCode
ITC 240: Web Application Programming
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Iteration: WHILE Loop Damian Gordon. WHILE Loop Consider the problem of searching for an entry in a phone book with only SELECTION:
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Lesson Year 1 CS112/0401/V1 LESSON 6 DESIGN TOOL PSEUDOCODE  Program Design Language (PDL)  Represent logic in English-like manner  Easier to.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
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.
ITEC113 Algorithms and Programming Techniques
Data Structures: Arrays Damian Gordon. Arrays Imagine we had to record the age of everyone in the class, we could do it declaring a variable for each.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Structured Programming Constructs March, 2011Copyright Yvette Francis.
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Review while loops Control variables Example Infinite loop
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Repetition Looping. Types for while do..while for The for loop allows you to iterate through a variable for a specific range of values. You must supply.
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
UCT Department of Computer Science Computer Science 1015F Iteration
Selection: CASE Statement Damian Gordon. Selection As well as the IF Statement, another form of SELECTION is the CASE statement.
Introduction to Python
ECE Application Programming
Python Loops and Iteration
The switch Statement, and Introduction to Looping
Chapter Topics 11.1 Introduction to Menu-Driven Programs
The Linux Command Line Chapter 29
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
The ‘grep’ Command Colin Masterson.
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
Understanding the Three Basic Structures
LOOPS BY: LAUREN & ROMEO.
Lab5 PROGRAMMING 1 Loop chapter4.
Some Common Issues: Iteration
3.1 Iteration Loops For … To … Next 18/01/2019.
© 2016 Pearson Education, Ltd. All rights reserved.
REPETITION STATEMENTS
Looping III (do … while statement)
Final Code Generation and Code Optimization
CHAPTER 4 Iterative Structure.
A LESSON IN LOOPING What is a loop?
LC-3 Control Structures
Debugging October 4, 2006 ComS 207: Programming I (in Java)
CHAPTER 21 LOOPS 1.
Some Common Issues: Print, Maths, Variables
High-level language structures
Decisions, decisions, decisions
CSCI 1100/1202 February 6, 2002.
Debugging.
Lecture 1 Review of 1301/1321 CSE /26/2018.
Hossain Shahriar CISC 101: Fall 2011 Hossain Shahriar
Presentation transcript:

Iteration: FOR, DO, LOOP Loop Damian Gordon

FOR Loop The FOR loop does the same thing as a WHILE loop but is easier if you are using the loop to do a countdown (or countup).

FOR Loop For example:

WHILE Loop PROGRAM Print1to5: A <- 1; WHILE (A != 6) DO Print A; A <- A + 1; ENDWHILE; END.

FOR Loop Can be expressed as:

FOR Loop PROGRAM Print1to5: FOR A IN 1 TO 5 DO Print A; ENDFOR; END.

FOR Loop Or, in general: FOR Variable IN Range DO ; ENDFOR;

DO Loop The WHILE loop can execute any number of times, including zero times. If we are writing a program, and we know that the loop we are using will be executed at least once, we could consider using a DO loop instead.

DO Loop PROGRAM MenuOptions: DO Print “****** MENU OPTIONS ******”; Print “1) Input Data”; Print “2) Delete Data”; Print “3) Print Report”; Print “9) Exit”; Get Value; WHILE (Value != 9) END.

DO Loop Or, in general: DO ; WHILE ( )

LOOP Loop The LOOP loop is one that has no condition, so it is an infinite loop. But it does include an EXIT command to break out of the loop if needed.

LOOP Loop PROGRAM Print1to5: A <- 1; LOOP Print A; IF (A = 6) THEN EXIT; ENDIF; A <- A + 1; ENDLOOP; END.

LOOP Loop Or, in general: LOOP ; IF ( ) THEN EXIT; ENDIF; ; ENDLOOP;

etc.