Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j <= 4; j++) { System.out.print("."); // 4 dots } int i =...

Slides:



Advertisements
Similar presentations
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Advertisements

COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Nested For Loops It is also possible to place a for loop inside another for loop. int rows, columns; for (rows=1; rows
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:
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
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.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Nested for loops.
For Loop Tips And Tricks
The for loop.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Nested For Loops. First, the rules to these loops do not change. We are sticking one loop inside another. while(condition) { // loop body } do { // loop.
CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Adapted from slides by Marty Stepp and Stuart Reges
Chapter 9 Repetition.
Chapter 4 Repetition Statements (loops)
Building Java Programs Chapter 2
Topic 5 for Loops -Arthur Schopenhauer
The nested repetition control structures & Continue Statements
Building Java Programs Chapter 2
Chapter 5 Repetition.
Fencepost loops reading: 4.1.
Building Java Programs Chapter 2
Chapter 9 Control Structures.
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
Building Java Programs
Building Java Programs
Building Java Programs
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.
Building Java Programs
Building Java Programs
Building Java Programs
CSE 190D, Winter 2013 Building Java Programs Chapter 2
Building Java Programs
The for loop suggested reading:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Suggested self-checks:
Drawing complex figures
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
CIS 110: Introduction to Computer Programming
Presentation transcript:

Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j <= 4; j++) { System.out.print("."); // 4 dots } int i =... ; // i is some positive int for (int j = 1; j <= i; j++) System.out.print(“*”); //output? based on slides at buildingjavaprograms.com

Nested Loops nested loop: A loop placed inside another loop. for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 5; j++) { System.out.print((i * j) + "\t"); } System.out.println(); // to end the line } Output: Statements in the outer loop's body are executed 4 times. The inner loop prints 5 numbers each time it is run.

Nested for Loops Example What is the output of the following nested for loops? for (int i = 1; i <= 6; i++) { for (int j = 1; j <= 10; j++) { System.out.print("*"); } System.out.println(); }

Nested Loops Example What is the output of the following nested for loops? for (int i = 1; i <= 6; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } Write nested loops that display:

Another Nested Loops Example What nested for loops produce the following output? inner loop - prints one line outer loop - loops 5 times since there.4 are 5 lines 5 We must build multiple complex lines of output using: an outer "vertical" loop for each of the lines inner "horizontal" loop(s) for the patterns within each line

Outer and Inner Loops First write the outer loop, from 1 to the number of lines. for (int line = 1; line <= 5; line++) {... } Now look at the line contents. Each line has a pattern: some dots - how many? How does number of dots relate to the line number? a number - how does it depend on line number?

Nested for Loops Exercise What is the output of the following nested for loops? for (int line = 1; line <= 5; line++) { for (int j = 1; j <= 5 - line; j++) { System.out.print("."); } for (int k = 1; k <= line; k++) { System.out.print(line); } System.out.println(); // new line }

Solution Answer:

Exercise Modify the previous code to produce this output:

Solution for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (5 - line); j++){ System.out.print("."); } System.out.print(line); for (int j = 1; j <= (line - 1); j++) { System.out.print("."); } System.out.println(); }

Common Mistakes Both of the following sets of code produce infinite loops: for (int i = 1; i <= 10; i++) { for (int j = 1; i <= 5; j++) { System.out.print(j); } System.out.println(); } for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 5; i++) { System.out.print(j); } System.out.println(); }