Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.

Slides:



Advertisements
Similar presentations
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Advertisements

June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Fundamentals of Software Development 1Slide 1 Today’s Summary Hello World concepts – –main, static, console Programming patterns involving assignment and.
Fundamentals of Software Development 1Slide 1 Programming Patterns: Motivation Many problems fit a “pattern” that experienced software developers recognizeMany.
Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Fundamentals of Software Development ISlide 1 Recap: Key ideas in WordGames ClassClass –versus instances –Defining –use of fields –Constructors E.g., to.
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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
Copyright 2008 by Pearson Education 1 Midterm announcements Next week on Friday May 8 Must bring an ID Open book, open notes, closed electronics Must attend.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© 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.
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.
Chapter 4: Control Structures II
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
Chapter 5 Loops.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
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.
By Chad Blankenbeker.  The for-loop is best used when you know how many times it is going to be looped  So if you know you want it to only loop 10 times,
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,
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
CS101 Computer Programming I Chapter 4 Extra Examples.
Chapter 4: Control Structures II
1 Ch. 6 Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit number of times.
Chapter 5: Control Structures II
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems.
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
Fundamentals of Software Development IProgramming patterns involving iteration1 Overview LoopsOverview Loops We’ll also see how loops are often combined.
Programming with Loops. When to Use a Loop  Whenever you have a repeated set of actions, you should consider using a loop.  For example, if you have.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
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.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Chapter 6 Controlling Program Flow with Looping Structures.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Loops A loop is: Java provides three forms for explicit loops:
Programming Patterns Many problems fit a “pattern” that experienced software developers recognize And hence can easily code, from experience Previously,
Chapter 5: Control Structures II
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
While Loops Chapter 3.
Chapter 5: Control Structures II
Building Java Programs
Building Java Programs
Looping and Repetition
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
PROGRAM FLOWCHART Iteration Statements.
Looping and Repetition
Presentation transcript:

Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java provides three forms for explicit loops:Java provides three forms for explicit loops: –while –for –do..while The conditions in loops are written as in if statementsThe conditions in loops are written as in if statements The break statement breaks out of the loop that it is withinThe break statement breaks out of the loop that it is within See examples on next slidesSee examples on next slides while (condition) { statement; … statement; } for (start; condition; step) { statement; … statement; } do { statement; … statement; } while (condition);

Fundamentals of Software Development 1Slide 2 for-loop example Problem: Display the sine of 0.01, sine of 0.02, … sine of 4.00.Problem: Display the sine of 0.01, sine of 0.02, … sine of Solution idea: Use a counting variableSolution idea: Use a counting variable –What should the variable start at? –The loop should continue while …? –Each time through the loop, increment the variable by …? for (double x = 0.01; x <= 4.00; x = x ) { System.out.println(Math.sin(x)); } for (int k = 1; k <= 400; ++k) { System.out.println(Math.sin(k/100.0)); } Both solutions are correct, although the first risks roundoff error in its stopping condition

Fundamentals of Software Development 1Slide 3 while-loop example Problem: Input numbers from the console, displaying the sine of each, stopping when the user inputs a negative numberProblem: Input numbers from the console, displaying the sine of each, stopping when the user inputs a negative number Three different approaches to solving this problem:Three different approaches to solving this problem: –Use a while loop that runs forever, but break when a negative number is input –First do an input, then use a while loop that runs while inputs are nonnegative –As above, but using a for loop Exercise: Work through the first solution approach yourselfExercise: Work through the first solution approach yourself –Don’t get hung up on the notation for getting input –Solutions are on the next slide

Fundamentals of Software Development 1Slide 4 Problem: Input numbers from the console, displaying the sine of each, stopping when the user inputs a negative numberProblem: Input numbers from the console, displaying the sine of each, stopping when the user inputs a negative number while (true) { input = inputStream.nextDouble(); if (input < 0) { break; } System.out.println(Math.sin(input)); } Both solutions are correct. The first emphasizes the stopping condition, at the expense of an awkward start (repeating the same statement before and within the loop). The first solution could be converted to a for-loop easily. Scanner inputStream = new Scanner(System.in); double input; input = inputStream.nextDouble(); while (input >= 0) { System.out.println(Math.sin(input); input = inputStream.nextDouble(); } This first, then either of the next two boxes.

Fundamentals of Software Development 1Slide 5 for loops versus while loops Typically we use:Typically we use: –for when we know in advance how many times the loop will execute –while when something that happens in the loop determines when the loop exits –do..while when we want a while loop that always does at least one iteration for (int i = 0; i < 7; i = i + 1) { System.out.println (i + " " + i*i); } int i = 0; while (i < 7) { System.out.println (i + " " + i*i); i = i + 1; } The two boxes are equivalent.