1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.

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:
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
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.
Building Java Programs
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.
BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past.
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:
CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
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.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
For Loop Tips And Tricks
The for loop.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
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:
Adapted from slides by Marty Stepp and Stuart Reges
Chapter 4 Repetition Statements (loops)
Building Java Programs Chapter 2
Building Java Programs Chapter 2
Building Java Programs Chapter 2
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
Building Java Programs
CSc 110, Spring 2018 Lecture 7: input and Constants
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
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
Building Java Programs
Building Java Programs
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
Building Java Programs
Suggested self-checks:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Building Java Programs
Presentation transcript:

1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS

2 NESTED FOR LOOPS

3 NESTED LOOPS nested loop: A loop placed inside another loop. for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 10; j++) { System.out.print("*"); } System.out.println(); // to end the line } Output: ********** The outer loop repeats 5 times; the inner one 10 times.

4 NESTED FOR LOOP EXERCISE What is the output of the following nested for loops? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } Output: * ** *** **** *****

5 NESTED FOR LOOP EXERCISE What is the output of the following nested for loops? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(i); } System.out.println(); } Output:

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

7 COMPLEX LINES What nested for loops produce the following output? 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 loop (loops 5 times because there are 5 lines) inner loop (repeated characters on each line)

8 OUTER AND INNER LOOP 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 (0 dots on the last line), then a number Observation: the number of dots is related to the line number.

9 MAPPING LOOPS TO NUMBERS for (int count = 1; count <= 5; count++) { System.out.print(... ); } What statement in the body would cause the loop to print: for (int count = 1; count <= 5; count++) { System.out.print(3 * count " "); }

10 LOOP TABLES What statement in the body would cause the loop to print: To see patterns, make a table of count and the numbers. Each time count goes up by 1, the number should go up by 5. But count * 5 is too great by 3, so we subtract 3. count# to print5 * count * count

11 LOOP TABLES QUESTION What statement in the body would cause the loop to print: Let's create the loop table together. Each time count goes up 1, the number printed should... But this multiple is off by a margin of... count number to print * count-4 * count * count

12 NESTED FOR LOOP EXERCISE Make a table to represent any patterns on each line To print a character multiple times, use a for loop. for (int j = 1; j <= 4; j++) { System.out.print("."); // 4 dots } line # of dots * line * line

13 NESTED FOR LOOP SOLUTION Answer: for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (-1 * line + 5); j++) { System.out.print("."); } System.out.println(line); } Output:

14 NESTED FOR LOOP EXERCISE What is the output of the following nested for loops? for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (-1 * line + 5); j++) { System.out.print("."); } for (int k = 1; k <= line; k++) { System.out.print(line); } System.out.println(); } Answer:

15 NESTED FOR LOOP EXERCISE Modify the previous code to produce this output: Answer: for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (-1 * line + 5); j++) { System.out.print("."); } System.out.print(line); for (int j = 1; j <= (line - 1); j++) { System.out.print("."); } System.out.println(); }