1 The for loop. 2 Repetition with for loops So far, repeating a statement is redundant: System.out.println("Homer says:"); System.out.println("I am so.

Slides:



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

Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Copyright © Texas Education Agency, Computer Programming For Loops.
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
ADMIT TICKET WHAT DOES THIS OUTPUT? double y = 2.5; int x = 6 / (int) y; System.out.println(“x = “ + x);
CS 112 Introduction to Programming Variables; Type Casting; Using Variables in for Loops Yang (Richard) Yang Computer Science Department Yale University.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
For Loop Tips And Tricks
The for loop.
Chapter 9 Control Structures.
CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.
1 Building Java Programs Chapter 2: Primitive Data and Definite Loops These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Adapted from slides by Marty Stepp and Stuart Reges
Chapter 9 Repetition.
Building Java Programs Chapter 2
Lecture 4: Program Control Flow
Primitive Data, Variables, Loops (Maybe)
Topic 5 for Loops -Arthur Schopenhauer
CiS 260: App Dev I Chapter 4: Control Structures II.
CSc 110, Autumn 2017 Lecture 5: The for Loop and user input
Building Java Programs Chapter 2
Chapter 5 Repetition.
Building Java Programs Chapter 2
Chapter 9 Control Structures.
CSc 110, Spring 2018 Lecture 5: The for Loop and user input
Building Java Programs
Building Java Programs
Lab5 PROGRAMMING 1 Loop chapter4.
Building Java Programs
Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell.
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
Building Java Programs
Lecture 8:The For Loop AP Computer Science Principles.
Building Java Programs
Building Java Programs
Building Java Programs
CSE 190D, Winter 2013 Building Java Programs Chapter 2
The for loop suggested reading:
SSEA Computer Science: Track A
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Suggested self-checks:
Building Java Programs
Building Java Programs
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Presentation transcript:

1 The for loop

2 Repetition with for loops So far, repeating a statement is redundant: System.out.println("Homer says:"); System.out.println("I am so smart"); System.out.println("S-M-R-T... I mean S-M-A-R-T"); Java's for loop statement performs a task many times. System.out.println("Homer says:"); for (int i = 1; i <= 4; i++) { // repeat 4 times System.out.println("I am so smart"); } System.out.println("S-M-R-T... I mean S-M-A-R-T");

3 for loop syntax for ( initialization ; test ; update ) { statement ;... statement ; } –Perform initialization once. –Repeat the following: Check if the test is true. If not, stop. Execute the statements. Perform the update. body header

4 Initialization for (int i = 1; i <= 6; i++) { System.out.println("I am so smart"); } Tells Java what variable to use in the loop –Performed once as the loop begins –The variable is called a loop counter can use any name, not just i can start at any value, not just 1

5 Test for (int i = 1; i <= 6; i++) { System.out.println("I am so smart"); } Tests the loop counter variable against a limit –Uses comparison operators: < less than <= less than or equal to > greater than >= greater than or equal to

6 Increment and decrement shortcuts to increase or decrease a variable's value by 1 ShorthandEquivalent longer version variable ++; variable = variable + 1; variable --; variable = variable - 1; int x = 2; x++; double gpa = 2.5; gpa--;

7 Modify-and-assign shortcuts to modify a variable's value ShorthandEquivalent longer version variable += value ; variable = variable + value ; variable -= value ; variable = variable - value ; variable *= value ; variable = variable * value ; variable /= value ; variable = variable / value ; variable %= value ; variable = variable % value ; x += 3;// x = x + 3; gpa -= 0.5;// gpa = gpa - 0.5; number *= 2;// number = number * 2;

8 Repetition over a range System.out.println("1 squared = " + 1 * 1); System.out.println("2 squared = " + 2 * 2); System.out.println("3 squared = " + 3 * 3); System.out.println("4 squared = " + 4 * 4); System.out.println("5 squared = " + 5 * 5); System.out.println("6 squared = " + 6 * 6); –Intuition: "I want to print a line for each number from 1 to 6" Write a for loop that prints the above lines

9 Loop walkthrough for (int i = 1; i <= 4; i++) { System.out.println(i + " squared = " + (i * i)); } System.out.println("Whoo!"); Output: 1 squared = 1 2 squared = 4 3 squared = 9 4 squared = 16 Whoo!

10 Multi-line loop body System.out.println("+----+"); for (int i = 1; i <= 3; i++) { System.out.println("\\ /"); System.out.println("/ \\"); } System.out.println("+----+"); –Output: \ / / \ \ / / \ \ / / \

11 Expressions for counter int highTemp = 5; for (int i = -3; i <= highTemp / 2; i++) { System.out.println(i * ); } –Output:

12 System.out.print Prints without moving to a new line –allows you to print partial messages on the same line int highestTemp = 5; for (int i = -3; i <= highestTemp / 2; i++) { System.out.print((i * ) + " "); } Output: Concatenate " " to separate the numbers

13 Counting down The update can use -- to make the loop count down. –The test must say > instead of < System.out.print("T-minus "); for (int i = 10; i >= 1; i--) { System.out.print(i + ", "); } System.out.println("blastoff!"); System.out.println("The end."); –Output: T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff! The end.