1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

Repeating Actions While and For Loops
Branching Loops exit(n) method Boolean data type and expressions
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
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.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
CS303ELoops1 CS 303E Lecture 8: Loops Self-referentially, short for GNU's not UNIX, a Unix-compatible software system developed by the Free Software Foundation.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
 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.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
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.
Flow of Control Chapter 3 Flow of control Branching Loops
Introduction To Scientific Programming Chapter 3.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Lecturer: Dr. AJ Bieszczad Chapter 3 COMP 150: Introduction to Object-Oriented Programming 3-1 l Branching l Loops l exit(n) method l Boolean data type.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
While ( number
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
ITM © Port, Kazman1 ITM 352 Flow-Control: Loops Lecture #8.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
Topic 4: Looping Statements
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Loop Structures.
Control Structures II (Repetition)
ITM 352 Flow-Control: Loops
Branching Loops exit(n) method Boolean data type and expressions
Outline Altering flow of control Boolean expressions
3.5- The while Statement The while statement has the following syntax:
Chapter 6: Repetition Statements
Repetition Statements (Loops) - 2
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Announcements/Reminders
Chapter 3 Flow of Control Loops in Java.
Presentation transcript:

1 Week 9 Loops

2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations »counting loops »sentinel-controlled loops »infinite loops »minimum of zero or minimum of one iteration l Several programming statement variations »while »do-while »for

3 while Loop l Syntax: while(Boolean_Expression) { //body of loop First_Statement;... Last_Statement; } l Initialization statements usually precede the loop. Boolean_Expression is the loop termination condition. The loop will continue executing as long as Boolean_Expression is true. l May be either counting or sentinel loop »Good choice for sentinel loop Something in body of loop should eventually cause Boolean_Expression to be false.

4 Semantics of the while Statement while (Boolean_Expression) Body Start Evaluate Boolean_Expression End loop false Execute Body true

5 while : A Counting Loop Example l A loop to sum 10 numbers entered by user int next; //Loop initialization int count = 1; int total =0; while (count <= 10) //Termination condition { //Body of loop next = Integer.parseInt(field.getText().trim()); total = total + next; count++; //Loop termination counter }

6 while : A Sentinel Controlled Loop Example l A loop to sum positive integers entered by the user next is the sentinel l The loop terminates when the user enters a negative number //Initialization int next = 0; int total = 0; while(next >= 0) //Termination condition { //Body total += next; next = Integer.parseInt(field.getText().trim()); }

7 while : A Minimum of Zero Iterations Because the first input value read and the test precedes the loop, the body of the while loop body may not execute at all //Initialization int next; int total = 0; next = Integer.parseInt(field.getText().trim()); while(next >= 0) //Termination condition { total = total + next; next = Integer.parseInt(field.getText().trim()); } l If the first number the user enters is negative, the loop body never executes

8 do-while Loop Syntax do { //body of loop First_Statement;... Last_Statement; } while(Boolean_Expression); l Initialization code may precede loop body l Loop test is after loop body so the body must execute at least once (minimum of at least one iteration) l May be either counting or sentinel loop »Good choice for sentinel loop Something in body of loop should eventually cause Boolean_Expression to be false.

9 Semantics of the do-while Statement do Body while (Boolean_Expression); Start Evaluate Boolean_Expression End loop false Execute Body true Execute Body

10 do-while Example int count = 1; int number = 5; do //Display integers 1 to 5 on one line { field.appendText(count + " "); count++; } while(count <= number); Note that field.appendText() is used and not field.setText() so the numbers will all be in the TextArea together. Output:

11 for Loop l Good choice for counting loop l Initialization, loop test, and loop counter change are part of the syntax l Syntax: for(Initialization; Boolean_Expression; Update_Action) loop body;

12 Semantics of the for Statement for(Initialization; Boolean_Expression; Update_Action) loop body; Start Evaluate Boolean_Expression End loop false Execute Body true Execute Initialization Execute Update_Action

13 for Example l Count down from 3 to 1 for(int count = 3; count >= 1; count--) { field.appendText("T = " + count); field.appendText(" and counting\n"); } field.appendText("Blast off!"); Output: T = 3 and counting T = 2 and counting T = 1 and counting Blast off!

14 Nested Loops l The body of a loop can have any kind of statements, including another loop. l Each time the outer loop body is executed, the inner loop body will execute 5 times, making a total of 20 times. for (line = 0; line < 4; line++) { for (star = 0; star < 5; star++) field.appendText('*'); field.appendText(‘\n'); } body of inner loop body of outer loop Output: *****

15 Some Practical Considerations When Using Loops l The most common loop errors are unintended infinite loops and off-by-one errors in counting loops. l Sooner or later everyone writes an unintentional infinite loop l Loops should be tested thoroughly, especially at the boundaries of the loop test, to check for off-by-one and other possible errors.

16 Tracing a Variable in a Loop l Tracing a variable: display the variable each time through the loop l A common technique is to test loop counters and troubleshoot off-by-one and other loop errors. l Some systems provide a built-in tracing system that allows you to trace a variable without having to change your program. l If no built-in utility is available, insert temporary output statements to display values.