Loops/Iteration Used to repeat an action Must have a STOP condition

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
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.
08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
©2004 Brooks/Cole Chapter 5 Repetition. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Repetition So far, our programs processed a single set.
Datalogi A 5: 6/10. while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
Java Programming: From the Ground Up
For Loops 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while Which loop to use? task with a specific.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
 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 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Chapter 5: Control Structures II
Java Prepared by Gary Langner Types of Loops u for loop (entrance controlled) –do an action for a definite number of times u while loop (entrance controlled.
Overview Go over parts of quiz? Another iteration structure for loop.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Three kinds of looping structures The while loop The for loop The do (also called the do-while) loop All have equivalent power, e.g., if you can write.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161.
Loops A loop is: Java provides three forms for explicit loops:
Chapter 4 Repetition Statements (loops)
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Chapter 5: Control Structures II
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Programming Fundamentals
CSC240 Computer Science III
Conditinoal Constructs Review
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
For & do/while Loops.
Building Java Programs
Outline Altering flow of control Boolean expressions
Alternate Version of STARTING OUT WITH C++ 4th Edition
Conditinoal Constructs Review
Repetition Control Structure
Chapter 6: Repetition Statements
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Repetition Statements
Java LESSON 3 Loops.
Indefinite loop variations
Building Java Programs
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
Building Java Programs
Control Statements:.
Presentation transcript:

Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while

Anatomy of a while loop 1 check the test 2 if the test is true exec the body when the body has finished go to step 1 if the test is false exit the loop int n = ?; // try n as 6 while (n >= 0) { n -= 2; System.out.println( n ); } System.out.println( “final n is “ + n ); test ? loop body stmt following loop stmt before loop The test is always a “keep going” condition. To determine the termination condition, negate the test. I.e. the loop will keep going as long as n >= 0 the loop will terminate when n becomes negative (n < 0)

while Loops The test is checked at the very beginning and then again each time after the after the entire loop body has been executed The test is NOT checked in the middle of the loop body This is true for all the loops (for, while, and do/while), not just the while loop A while is just an if statement that keeps going back to the test and quits looping at first failure of test x = ?; // try x as 45 while (x < 50) { x++; System.out.println( x ); }

Practice What’s the output? int d = 90; while (d < 80) { ++d; } System.out.println( “d is “ + d ); int x = 90; while (x < 100) x += 5; if (x>95) x-=25; System.out.println(“final value for x is “ + x ); int z = 85; while (z < 90) z -= 5; System.out.println(“final value for z is “ + z );

Summing (even) numbers with a while loop Example of an indeterminate loop - the user’s input will determine how many times the loop executes // assume kbd declared (save space) int sum = 0, evensum = 0, number; System.out.print(“ First number please “); number = kbd.nextInt() ); while (number > 0) { sum += number; if (number % 2 == 0) evensum += number; System.out.print(“number please “); } System.out.println(“sum is “ + sum ); System.out.println(“sum of even #’s is “ + evensum );

Error checking with a do loop do loop is a variant of the while that waits till the bottom to make the test. There are some very good uses for the do form of the while final int MAX = 10, MIN = 5; int number; do { System.out.print(“Enter # between “ + “5 and 10 inclusive: “); number = kbd.nextInt(); if (number < MIN || number > MAX) System.out.println(“:=( Try again”); } while (number < MIN || number > MAX); System.out.print(“The user entered “ + number ); test ? loop body stmt following loop stmt before loop