REPETITION (loops or iteration) Schneider: Sec. 6.1 & 6.3.

Slides:



Advertisements
Similar presentations
Looping Structures: Do Loops
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Repeating Actions While and For Loops
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Loop Statements (Iteration). Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of.
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.
Copyright © Texas Education Agency, Computer Programming For Loops.
COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
© 1999, by Que Education and Training, Chapter 7, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CPS120 Introduction to Computer Science Iteration (Looping)
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Visual Basic Programming
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Repetition Statements while and do while loops
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
CS 100 Introduction to Computing Seminar October 7, 2015.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
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?
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
COMP Loop Statements Yi Hong May 21, 2015.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Repetition Structures The long awaited …. Repetition Structures I repeat …
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
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.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
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.
Comp1004: Loops and Arrays I Whiles, For and Arrays[]
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
CS161 Introduction to Computer Science
Programming Logic and Design Fourth Edition, Comprehensive
Scratch: iteration / repetition / loops
Iteration with While You can say that again.
Loops A portion of a program that repeats a statement or a group of statements is called a loop. The statement or group of statements to be repeated is.
Introduction to Repetition Structures
A LESSON IN LOOPING What is a loop?
Topics Introduction to Repetition Structures
CS2011 Introduction to Programming I Loop Statements (I)
Looping Structures.
Looping and Repetition
Presentation transcript:

REPETITION (loops or iteration) Schneider: Sec. 6.1 & 6.3

We use loops because computers are really good at doing the same thing over & over & over again...

There are three types of loops in Visual Basic While –entrance controlled Until –exit controlled For … Next –counting

How they are alike They all have the format: loop control loop body loop control They all cause the statements in the loop body to be executed repeatedly loop control contains the condition for loop termination

But… each type of loop has its own special features Sometimes it’s appropriate - even correct - to use one type over the others Often, the choice is yours Let’s see how they compare with each other...

While loops (Sec. 6.1) Do While condition loop body Loop The condition is checked before the loop body is executed. –It must be initialized outside the loop. The loop body must contain an instruction that affects the condition. –Otherwise, the loop will be infinite!

Until loops (Sec. 6.1) Do loop body Loop Until condition The condition is checked after the loop body is executed. –It does not have to initialized outside the loop The loop body must contain an instruction that affects the condition. –Otherwise, the loop will be infinite!

For … Next loops (Sec. 6.3) For i = m To n Step s loop body Next i Before the first iteration, i is initialized to m If m <= n (typical): –After each iteration, i = i + s –The loop continues to iterate as long as i is n, the loop terminates

When to use one or the other? If you don’t want the loop body to be executed even once before the condition is checked… If you want the loop body to be executed at least once before the condition is ever checked… If you know how many times the loop body should be executed...

Examples p Ex 1 - while & for..next p Ex 2 - while p Ex 3 - until p Ex 4 - while p Problem 24 - until & while p Ex 1 - for…next p Ex 2, Ex 3 - for…next

Assignment Lab today –pg 259 # 25, 28 Homework due Tuesday: –Schneider – pg 260 # 32, 34 pg 285 # 20, 23