Java's for Statement.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
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.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Chapter 6 Repetition Asserting Java © Rick Mercer.
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
CS150 Introduction to Computer Science 1
Chapter 5: Control Structures II (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.
09 Non-deterministic iteration1June Non-deterministic iteration CE : Fundamental Programming Techniques.
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.
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.
©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.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Chapter 6 Repetition Asserting Java © Rick Mercer.
Java Programming: From the Ground Up
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
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.
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 6A While Loops Asserting Java © Rick Mercer.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Chapter 6 Repetition Asserting Java © Rick Mercer.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Repetition Statements
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
while Repetition Structure
Chapter 6, Part 1 While Loops Asserting Java © Rick Mercer 1.
Determinate Loop Pattern, Java's for Statement, and Scanner objects
Loop Structures.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
C# and the .NET Framework
Repetition-Counter control Loop
Determinate Loop Pattern, Java's for Statement, and Scanner objects
Looping and Repetition
Building Java Programs
Outline Altering flow of control Boolean expressions
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 8 Repetition Computing Fundamentals with C++ 3rd Edition
Chapter 6 Repetition Asserting Java © Rick Mercer 1.
Control structures to direct program flow
Chapter 6: Repetition Statements
Alternate Version of STARTING OUT WITH C++ 4th Edition
Repetition Statements (Loops) - 2
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Repetition Statements
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Chapter 6, Part 1 While Loops Asserting Java © Rick Mercer 1.
Looping Structures.
Looping and Repetition
Presentation transcript:

Java's for Statement

Algorithmic Pattern: The Determinate loop We often need to perform some action a specific number of times: Generate 89 paychecks Count down to 0 (take 1 second of the clock) Simulate playing a game of "Let's Make a Deal" 10,000 times The determinate loop pattern repeats some action a specific number of times

Determinate Loops This template repeats a process n times replace comments with appropriate statements int n = /* how often we must repeat the process */ for( int j = 1; j <= n; j = j + 1 ) { // the process to be repeated } determinate loops must know the number of repetitions before they begin know exactly how many employees, or students, or whatever that must be processed, for example

General Form The Java for loop for ( initial statement ; loop-test ; update-step) { repeated-part } When a for loop is encountered, the initial-statement is executed (used here quite often as int j = 1). The loop-test evaluates. If loop-test is false, the for loop terminates. If loop-test is true, the repeated-part executes followed by the update-step.

Flow chart view of a for loop Initial statement True False Loop test Iterative part update-step

Example for loop that produces an average Scanner keyboard = new Scanner(System.in); double sum = 0.0; System.out.print("How many do you want to average? "); int n = keyboard.nextInt(); // Do something n times for (int j = 1; j <= n; j = j + 1) { System.out.print("Enter number: "); // <- Repeat 3 int number = keyboard.nextInt(); // <- statements sum = sum + number; // <- n times } double average = sum / n; System.out.print("Average = " + average);

Code Demo: Use the debugger to trace this code int n = 5; for (int j = 1; j <= n; j = j + 1) { System.out.println(j); } for (int k = 10; k >= 0; k = k - 2) { System.out.println(k);

Other Incrementing Operators It is common to see determinate loops of this form where n is the number of repetitions for( int j = 1; j <= n; j++ ) } // ... } The unary ++ and -- operators add 1 and subtract 1 from their operands, respectively. int n = 0; n++; // n is now 1 equivalent to n=n+1; or n+=1; n++; // n is now 2 n--; // n is now 1 again The expression count++ is equivalent to the more verbose count = count + 1;

Other Assignment Operators Java has several assignment operators in addition to = (-= and +=) j -= 2; is the equivalent of j = j - 2; sum += x; is the equivalent of sum = sum + x; What is sum when a user enters 7 and 8? int sum = 0; int x = 0; System.out.print("Enter a number: "); x = keyboard.nextInt(); // user enters 7 sum += x; x = keyboard.nextInt(); // user enters 8