Loops A loop is: Java provides three forms for explicit loops:

Slides:



Advertisements
Similar presentations
08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
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.
Fundamentals of Software Development ISlide 1 Recap: Key ideas in WordGames ClassClass –versus instances –Defining –use of fields –Constructors E.g., to.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other 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.
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”);
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
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.
Chapter 5 Loops.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CS101 Computer Programming I Chapter 4 Extra Examples.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems.
Fundamentals of Software Development IProgramming patterns involving iteration1 Overview LoopsOverview Loops We’ll also see how loops are often combined.
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.
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.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
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.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Slides by Evan Gallagher
Java Fundamentals 4.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
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
Chapter 5: Control Structures II
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
While Loops Chapter 3.
Chapter 5: Control Structures II
Control Structure Senior Lecturer
While Statement.
Looping and Repetition
Repetition Statements
MSIS 655 Advanced Business Applications Programming
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
Lecture Notes – Week 3 Lecture-2
Java Language Basics.
Introduction to Object-Oriented Programming with Java--Wu
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Building Java Programs
Control Statements Loops.
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Seating “chart” Front - Screen rows Back DOOR.
Loops.
Control Statements Loops.
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Looping and Repetition
Presentation transcript:

Loops A loop is: Java provides three forms for explicit loops: a block of code that executes repeatedly while some condition holds true. Java provides three forms for explicit loops: while for do..while The conditions in loops are written as in if statements See examples on next slides Fundamentals of Software Development 1

Loops while loop while (condition) { statement; … statement; } for (start; condition; step) { statement; … statement; } for loop do { statement; … statement; } while (condition); do-while loop Fundamentals of Software Development 1

for-loop example Problem: Display the square of all numbers from 1 to 99 Solution 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 (int k = 1; k < 100; ++k) { System.out.println(k*k); } Alternative solution: for (int k = 1; k <= 99; ++k) { System.out.println(k*k); } Both solutions are correct; the first risks round-off error in its stopping condition Fundamentals of Software Development 1

while-loop example Problem: Input numbers from the console, displaying the square of each, stopping when the user inputs a negative number Solution: First do an input, then use a while loop that runs while inputs are nonnegative Fundamentals of Software Development 1

Problem: Input numbers from the console, displaying the square of each, stopping when the user inputs a negative number Scanner inputStream = new Scanner(System.in); double input; input = inputStream.nextDouble(); while (input >= 0) { System.out.println(input*input); } Fundamentals of Software Development 1

for loops versus while loops 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. 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 Fundamentals of Software Development 1