Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: 22-26 exercises: 10-14 videos: Ch. 2 #4.

Slides:



Advertisements
Similar presentations
Chapter 5 One- and Two-Sample Estimation Problems.
Advertisements

ax2 + bx + c 2x2 + 11x + 5 ac 2(5) = b =11 ( ) 2x2 + 10x + 1x
Mass Calculations for Reactions
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to XHTML Programming the World Wide Web Fourth edition.
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 The Basics of Javascript Programming the World Wide Web Fourth.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 11 Market-Clearing Models of the Business Cycle.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 Cascading Style Sheets Programming the World Wide Web Fourth edition.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 3.1 Chapter 3.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 14.
Acid Base Balance U47192.
CHAPTER 4 Fraction Notation: Addition, Subtraction, and Mixed Numerals Slide 2Copyright 2012, 2008, 2004, 2000 Pearson Education, Inc. 4.1Least Common.
Copyright 2012, 2008, 2004, 2000 Pearson Education, Inc.
Dependency Test in Loops By Amala Gandhi. Data Dependence Three types of data dependence: 1. Flow (True) dependence : read-after-write int a, b, c; a.
ABC Technology Project
Slide Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
15-1 Copyright © 2013 Pearson Education, Inc. publishing as Prentice Hall Chapter 15 Money and Banking.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1 self-check: #1-6 exercises: #1-3 videos: Ch.
For(int i = 1; i
Progam.-(6)* Write a program to Display series of Leaner, Even and odd using by LOOP command and Direct Offset address. Design by : sir Masood.
8.4 Percent Concentration
Programming In C++ Spring Semester 2013 Practice 2 Programming In C++, Practice By Umer Rana.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 15 2 k Factorial Experiments and Fractions.
Copyright ©2013 Pearson Education, Inc. publishing as Prentice Hall
Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 13 One-Factor Experiments: General.
Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 14 Factorial Experiments (Two or More Factors)
Building Java Programs
Chapter 2 Application Layer. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 2-2.
Exercise Exercise3.1 8 Exercise3.1 9 Exercise
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:
Exercise Exercise Exercise Exercise
Exercise Exercise Exercise Exercise
Exercise Exercise6.1 7 Exercise6.1 8 Exercise6.1 9.
Chapter 6 Human Capital. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2.
Chapter 16 Resources and the Environment at the Global Level.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
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.
Practice Slides Unlabeled. Copyright © 2010 Pearson Education, Inc. Plate 1.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
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:
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
©2004 by Pearson Education. ©2004 by Pearson Education.
©2004 by Pearson Education. ©2004 by Pearson Education.
Copyrights apply.
توكيد الذات.
Copyright © 2008 Pearson Prentice Hall Inc.
Copyright © 2008 Pearson Prentice Hall Inc.
Chapter 7 MATH 1325 Business Calculus Ch.7 Copyright © 2005 Pearson Education, Inc.
Copyright © 2008 Pearson Prentice Hall Inc.
Copyright © 2008 Pearson Prentice Hall Inc.
Chapter 3 MATH 1325 Business Calculus Ch.3 Copyright © 2005 Pearson Education, Inc.
Chapter 5 MATH 1325 Business Calculus Ch.5 Copyright © 2005 Pearson Education, Inc.
Building Java Programs
Chemistry Ch. 10 Review and worksheets
Introduction: Some Representative Problems
Section 10.5 The Dot Product
Unit 4 Review Answers.
Copyright © 2008 Pearson Prentice Hall Inc.
Copyright © 2008 Pearson Prentice Hall Inc.
Chapter 6 Dynamic Programming.
Chapter 4 Greedy Algorithms.
Presentation transcript:

Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4

Copyright 2008 by Pearson Education 2 Redundancy between loops for (int j = 1; j <= 5; j++) { System.out.print(j + "\t"); } System.out.println(); for (int j = 1; j <= 5; j++) { System.out.print(2 * j + "\t"); } System.out.println(); for (int j = 1; j <= 5; j++) { System.out.print(3 * j + "\t"); } System.out.println(); for (int j = 1; j <= 5; j++) { System.out.print(4 * j + "\t"){ } System.out.println(); Output:

Copyright 2008 by Pearson Education 3 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.

Copyright 2008 by Pearson Education 4 Nested for loop exercise 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(); } Output: **********

Copyright 2008 by Pearson Education 5 Nested for loop exercise 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(); } Output: * ** *** **** ***** ******

Copyright 2008 by Pearson Education 6 Nested for loop exercise 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(i); } System.out.println(); } Output:

Copyright 2008 by Pearson Education 7 Common errors 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(); }

Copyright 2008 by Pearson Education 8 Complex lines What nested for loops produce the following output? Can 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)

Copyright 2008 by Pearson Education 9 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) a number more dots (0 dots on the first line)

Copyright 2008 by Pearson Education 10 Nested for loop exercise A table for the dots we start with Could do a second table for the other dots 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

Copyright 2008 by Pearson Education 11 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.print(line); for (int j = 1; j <= (line - 1); j++) { System.out.print("."); } System.out.println(); } Output:

Copyright 2008 by Pearson Education 12 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises: videos: Ch. 2 #5

Copyright 2008 by Pearson Education 13 Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? Real graphics require a lot of finesse ASCII art has complex patterns Can focus on the algorithms #================# | <><> | | <>....<> | | <> <> | |<> <>| | <> <> | | <>....<> | | <><> | #================#

Copyright 2008 by Pearson Education 14 Development strategy Recommendations for managing complexity: 1. Write an English description of steps required (pseudo-code) use pseudo-code to decide methods 2. Create a table of patterns of characters use table to write loops in each method #================# | <><> | | <>....<> | | <> <> | |<> <>| | <> <> | | <>....<> | | <><> | #================#

Copyright 2008 by Pearson Education Pseudo-code pseudo-code: An English description of an algorithm. Example: Drawing a 12 wide by 7 tall box of stars print 12 stars. for (each of 5 lines) { print a star. print 10 spaces. print a star. } print 12 stars. ************ * ************

Copyright 2008 by Pearson Education 16 Pseudo-code algorithm 1. Line #, 16 =, # 2. Top half | spaces (decreasing) <> dots (increasing) <> spaces (same as above) | 3. Bottom half (top half upside-down) 4. Line #, 16 =, # #================# | <><> | | <>....<> | | <> <> | |<> <>| | <> <> | | <>....<> | | <><> | #================#

Copyright 2008 by Pearson Education 17 Methods from pseudocode public class Mirror { public static void main(String[] args) { line(); topHalf(); bottomHalf(); line(); } public static void topHalf() { for (int line = 1; line <= 4; line++) { // contents of each line } public static void bottomHalf() { for (int line = 1; line <= 4; line++) { // contents of each line } public static void line() { //... }

Copyright 2008 by Pearson Education Tables A table for the top half: Compute spaces and dots expressions from line number linespacesdots linespaces line * dots4 * line #================# | <><> | | <>....<> | | <> <> | |<> <>| | <> <> | | <>....<> | | <><> | #================#

Copyright 2008 by Pearson Education Writing the code Useful questions about the top half: What methods? (think structure and redundancy) Number of (nested) loops per line? #================# | <><> | | <>....<> | | <> <> | |<> <>| | <> <> | | <>....<> | | <><> | #================#

Copyright 2008 by Pearson Education 20 Partial solution // Prints the expanding pattern of <> for the top half of the figure. public static void topHalf() { for (int line = 1; line <= 4; line++) { System.out.print("|"); for (int space = 1; space <= (line * ); space++) { System.out.print(" "); } System.out.print("<>"); for (int dot = 1; dot <= (line * 4 - 4); dot++) { System.out.print("."); } System.out.print("<>"); for (int space = 1; space <= (line * ); space++) { System.out.print(" "); } System.out.println("|"); }

Copyright 2008 by Pearson Education 21 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5

Copyright 2008 by Pearson Education 22 Scaling the mirror Let's modify our Mirror program so that it can scale. The current mirror (left) is at size 4; the right is at size 3. We'd like to structure the code so we can scale the figure by changing the code in just one place. #================# | <><> | | <>....<> | | <> <> | |<> <>| | <> <> | | <>....<> | | <><> | #================# #============# | <><> | | <>....<> | |<> <>| | <>....<> | | <><> | #============#

Copyright 2008 by Pearson Education 23 Limitations of variables Idea: Make a variable to represent the size. Use the variable's value in the methods. Problem: A variable in one method can't be seen in others. public static void main(String[] args) { int size = 4; topHalf(); printBottom(); } public static void topHalf() { for (int i = 1; i <= size; i++) { // ERROR: size not found... } public static void bottomHalf() { for (int i = size; i >= 1; i--) { // ERROR: size not found... }

Copyright 2008 by Pearson Education 24 Variable scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists only in that loop. A variable declared in a method exists only in that method. public static void example() { int x = 3; for (int i = 1; i <= 10; i++) { System.out.println(x); } // i no longer exists here } // x ceases to exist here x's scope i's scope

Copyright 2008 by Pearson Education 25 Scope implications Variables without overlapping scope can have same name. for (int i = 1; i <= 100; i++) { System.out.print("/"); } for (int i = 1; i <= 100; i++) { // OK System.out.print("\\"); } int i = 5; // OK: outside of loop's scope A variable can't be declared twice or used out of its scope. for (int i = 1; i <= 100 * line; i++) { int i = 2; // ERROR: overlapping scope System.out.print("/"); } i = 4; // ERROR: outside scope

Copyright 2008 by Pearson Education 26 Class constants class constant: A value visible to the whole class. value can only be set at declaration value can't be changed while the program is running Syntax: public static final type name = value ; name is usually in ALL_UPPER_CASE Examples: public static final int DAYS_IN_WEEK = 7; public static final double INTEREST_RATE = 3.5; public static final int SSN = ;

Copyright 2008 by Pearson Education 27 Constants and figures Consider the task of drawing the following scalable figure: +/\/\/\/\/\/\/\/\/\/\+ | | | Multiples of 5 occur many times | +/\/\/\/\/\/\/\/\/\/\+ +/\/\/\/\+ | | | The same figure at size 2 +/\/\/\/\+

Copyright 2008 by Pearson Education 28 Repetitive figure code public class Sign { public static void main(String[] args) { drawLine(); drawBody(); drawLine(); } public static void drawLine() { System.out.print("+"); for (int i = 1; i <= 10; i++) { System.out.print("/\\"); } System.out.println("+"); } public static void drawBody() { for (int line = 1; line <= 5; line++) { System.out.print("|"); for (int spaces = 1; spaces <= 20; spaces++) { System.out.print(" "); } System.out.println("|"); }

Copyright 2008 by Pearson Education 29 Adding a constant public class Sign { public static final int HEIGHT = 5; public static void main(String[] args) { drawLine(); drawBody(); drawLine(); } public static void drawLine() { System.out.print("+"); for (int i = 1; i <= HEIGHT * 2; i++) { System.out.print("/\\"); } System.out.println("+"); } public static void drawBody() { for (int line = 1; line <= HEIGHT; line++) { System.out.print("|"); for (int spaces = 1; spaces <= HEIGHT * 4; spaces++) { System.out.print(" "); } System.out.println("|"); }

Copyright 2008 by Pearson Education 30 Complex figure w/ constant Modify the Mirror code to be resizable using a constant. A mirror of size 4: #================# | <><> | | <>....<> | | <> <> | |<> <>| | <> <> | | <>....<> | | <><> | #================# A mirror of size 3: #============# | <><> | | <>....<> | |<> <>| | <>....<> | | <><> | #============#

Copyright 2008 by Pearson Education 31 Using a constant Constant allows many methods to refer to same value: public static final int SIZE = 4; public static void main(String[] args) { topHalf(); printBottom(); } public static void topHalf() { for (int i = 1; i <= SIZE; i++) { // OK... } public static void bottomHalf() { for (int i = SIZE; i >= 1; i--) { // OK... }

Copyright 2008 by Pearson Education 32 Loop tables and constant Let's modify our loop table to use SIZE #================# | <><> |#============# | <>....<> | | <><> | | <> <> | | <>....<> | |<> <>| |<> <>| | <> <> | | <>....<> | | <>....<> | | <><> | | <><> | #============# #================# SIZElinespaces -2*line + (2*SIZE) dots 4*line ,2,3,46,4,2,0-2*line + 80,4,8,124*line ,2,34,2,0-2*line + 60,4,84*line - 4 SIZElinespacesdots 41,2,3,46,4,2,00,4,8,12 31,2,34,2,00,4,8 SIZElinespacesdots 41,2,3,46,4,2,0-2*line + 80,4,8,124*line ,2,34,2,0-2*line + 60,4,84*line - 4

Copyright 2008 by Pearson Education 33 Partial solution public static final int SIZE = 4; // Prints the expanding pattern of <> for the top half of the figure. public static void topHalf() { for (int line = 1; line <= SIZE; line++) { System.out.print("|"); for (int space = 1; space <= (line * -2 + (2*SIZE)); space++) { System.out.print(" "); } System.out.print("<>"); for (int dot = 1; dot <= (line * 4 - 4); dot++) { System.out.print("."); } System.out.print("<>"); for (int space = 1; space <= (line * -2 + (2*SIZE)); space++) { System.out.print(" "); } System.out.println("|"); }

Copyright 2008 by Pearson Education 34 Observation about constant It doesn't replace every occurrence of the original value. “Different fours” for different reasons A good reason to use variables to keep things straight for (int dot = 1; dot <= (line * 4 - 4); dot++) { System.out.print("."); }