Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Repetition Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Loop Statements After reading and studying this Section,
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 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
© 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Java Programming: From the Ground Up
CONTROLLING PROGRAM FLOW
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 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
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.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Loops and Files. 5.1 The Increment and Decrement Operators.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 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.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
While ( number
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Unit 3 Lesson 9 Repetition Statements (Loops)
Loop Structures.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 6 The while Statement
Looping and Repetition
Outline Altering flow of control Boolean expressions
Introduction to Object-Oriented Programming with Java--Wu
Chapter 6: Repetition Statements
Repetition Statements (Loops) - 2
Chapter 3 Flow of Control Loops in Java.
Looping and Repetition
Presentation transcript:

Repetition

Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION

Repetition - Used to repeat one or more programming statements. while Statement do-while Statement for Statement

The while Statement In cooking, a recipe may tell us that, as there are lumps in a sauce we should stir it. In Java: While (there are lumps) give sauce a stir;

The while Statement  Loop while loops continue to loop while some condition is true, and stops when the condition becomes false  The condition is a Boolean expression  Will never execute if the condition is initially false

while ( number <= 100 ) { sum = sum + number; number = number + 1; } Syntax for the while Statement while ( ) { } Statement (loop body) Boolean Expression

The while Statement int sum = 0, number = 1; while ( number <= 100 ) { sum = sum + number; number = number + 1; } These statements are executed as long as number is less than or equal to 100.

Control Flow of while int sum = 0, number = 1 number <= 100 ? false sum = sum + number; number = number + 1; sum = sum + number; number = number + 1; true

Infinite Loops  A loop that continues executing forever  Can be caused by syntax or logic errors. For example: while (num < 0) //error--no braces System.out.print("Enter a value: "); num = input.nextInt(); while (num < 0); //error-- semicolon System.out.print("Enter a value: "); num = input.nextInt(); Causes an infinite loop.

while Loop Pitfall - 1 Infinite Loops Both loops will not terminate because the boolean expressions will never become false. Infinite Loops Both loops will not terminate because the boolean expressions will never become false. int count = 1; while ( count != 10 ) { count = count + 2; } 2 2 int product = 0; while ( product < ) { product = product * 5; } 1 1

while Loop Pitfall - 2 Goal: Execute the loop body 10 times. count = 1; while (count < 10) {... count++; } 1 1 count = 0; while (count <= 10) {... count++; } 3 3 count = 1; while (count <= 10) {... count++; } 2 2 count = 0; while (count < 10) {... count++; } andexhibit off-by-one error.

The do-while Statement  Alternative form of the while statement  Executes at least once  In do while loops the test condition comes at the end of the loop. So, the loop always iterates at least once. In Cooking Example: give sauce a stir; while (there are lumps)

do { sum += number; number++; } while (sum <= ); Syntax for the do-while Statement do { } while ( ); Statement (loop body) Boolean Expression

The do-while Statement int sum = 0, number = 1; do { sum += number; number++; } while ( sum <= ); These statements are executed as long as sum is less than or equal to 1,000,000.

Control Flow of do-while int sum = 0, number = 1 sum += number; number++; sum += number; number++; sum <= ? true false

Pre-test vs. Post-test loops Use a pre-test loop for something that may be done zero times Use a post-test for something that is always done at least once

Checklist for Repetition Control 1. Watch out for the off-by-one error (OBOE). 2. Make sure the loop body contains a statement that will eventually cause the loop to terminate. 3. Make sure the loop repeats exactly the correct number of times.

The for Statement Returning one last time to our cooking analogy, we might have instructions to eliminate lumpiness phrased in the form”stir mixture for 100 strokes” In Java: int count; for (count = 0; count < 100; count++) give sauce a stir;

The for Statement  Loop structure that executes a set of statements a fixed number of times  Uses a loop control variable (lcv)  The increment ( ++ ) or decrement ( -- ) operators are used to change the value of the loop control variable

for ( i = 0 ; i < 20 ; i++ ) { number = inputBox.getInteger(); sum += number; } Syntax for the for Statement for ( ; ; ) Initialization Boolean Expression Update Statement (loop body)

The for Statement int i, sum = 0, number; for (i = 0; i < 20; i++) { number = In.getInt(); sum += number; } These statements are executed for 20 times ( i = 0, 1, 2, …, 19).

Control Flow of for i = 0; false number = inputBox.getInteger( ); sum += number; true i ++; i < 20 ?

The Nested-for Statement If a first loop contains a second loop, we say the second is nested within the first. The first loop is often referred to as the outer loop and the second as the inner loop. Read page

Which Loop, When? for loop – when you know how many times the loop will be executed while loop – when you don’t know how many times the loop will execute, including when the user is in control while loop – when you may not want the loop to execute even once do while – when you want the loop to execute even once

Debugging Techniques  The debugger included with many compilers  Variable trace, which is a manual technique of list values of variables at the points of assignment  Additional println() statements for displaying variable values at points of assignment  "Commenting out" code to detect bugs through a process of elimination

Variable Trace int num1 = 0; int num2 = 0; while (num1 < 10) { if (num1 % 3 == 0) { num2 += num1; System.out.print(num2 + " "); } num1 += 1; }

Using println() to Debug int num1 = 0; int num2 = 0; System.out.println("num1 before while: " + num1);//debug while (num1 < 10) { System.out.println("num1 in while: " + num1);//debug if (num1 % 3 == 0) { num2 += num1; System.out.println("num2: " + num2);//debug System.out.print(num2 + " "); } num1 += 1; }

Using Comments to Debug int num1 = 0; int num2 = 0; while (num1 < 10) { //if (num1 % 3 == 0) { //num2 += num1; //System.out.print(num2 + " "); //} num1 += 1; }

Homework Page 138 # 1, 2, 3, 5 Page 140 # 1 Page 141 # 3, 4 Page 143 # 1