Nested For Loops It is also possible to place a for loop inside another for loop. int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1;

Slides:



Advertisements
Similar presentations
Nested for loops Cont’d. 2 Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? –Real graphics require a.
Advertisements

Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.
Mock test review Revision of Activity Diagrams for Loops,
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
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.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
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.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 5 Loops.
BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
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:
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
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 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
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Nested Loops, Break/Continue. Nested Loops Nested loops : loop inside a loop – Loop through rows and columns – Loop through every word in every file –
Lecture 18: Nested Loops and Two-Dimensional Arrays
Lecture 7: Repeating a Known Number of Times
Chapter 6 More Conditionals and Loops
Topic 5 for Loops -Arthur Schopenhauer
Decision statements. - They can use logic to arrive at desired results
The for-loop and Nested loops
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
The for loop suggested reading:
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Suggested self-checks:
Review of Previous Lesson
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
Control Statements:.
Presentation transcript:

Nested For Loops

It is also possible to place a for loop inside another for loop. int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=10; columns++) { System.out.print("*"); } System.out.println (); } Outer Loop Inner Loop Output:**************************************************

Nested For Loops, Example #2 //simple loop to print triangle using the character * //Produces a right triangle with hypoten increasing to left public class while_numbers { public static void main(String[] args) { int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=rows; columns++) { System.out.print ("*"); } System.out.print ("\n"); } }} Output:*************** Outer Loop Inner Loop

Nested For Loops using seconds and minutes Lets write a program in class to count number of seconds and minutes in one hour

Nested For Loops using seconds and minutes in one hour //simple loop to print seconds and minutes in one hour public class while_numbers { public static void main(String[] args) { int seconds,minutes; for (minutes=0; minutes <= 59; minutes++) { for (seconds = 0; seconds < = 59; seconds ++) { System.out.println(minutes + “ : “+ seconds); } }}

Nested For Loops using seconds and minutes Lets write a program in class to count number of seconds and minutes and hours in one day

Nested For Loops using seconds and minutes in one hour //simple loop to print seconds, minutes and hours in one day public class while_numbers { public static void main(String[] args) { int seconds,minutes, hours; for (hours=0; hours<= 23; hours++) { for (minutes=0; minutes <= 59; minutes++) { for (seconds = 0; seconds < = 59; seconds ++) { System.out.println(hours + “:“+ minutes + “:“+ seconds); } }}

Nested For Loops, Example #3 public class Prog1 { //Produces a right triangle with hypoten increasing to right public static void main(String[] asd) { final int MAX = 8; for(int j = 0; j < MAX; j++)// { for(int k = 0; k < MAX - j; k++)//draw MAX - j blanks { System.out.print(' '); } for(int k = 0; k <= j; k++)//draw remaining j columns as x's { System.out.print('x'); } System.out.println(); } System.out.println(); }

Nested For Loops, multiplication table public class Program2a { //Prelim to producing a multiplication table public static void main(String[] asd) { for(int j = 0; j < 8; j++)//label columns { for(int k = 0; k < 9; k++) { System.out.print(k); } System.out.println(); final int HORIZ = 6; for(int j = 1; j <= HORIZ; j++)//label horizontal { System.out.print("\t" + j);//tabs every 9 columns } System.out.println(); }

Nested For Loops, multiplication table example # 2 public class Prog2 { //Produces a multiplication table public static void main(String[] asd) { final int HORIZ = 6; final int VERT = 5; for(int j = 1; j <= HORIZ; j++)//label horizontal { System.out.print("\t" + j);//tabs every 9 columns } System.out.println(); for(int j = 1; j <= HORIZ + 1; j++)//draw line { System.out.print(" "); } System.out.println(); for(int k = 1; k <= VERT; k++) { System.out.print(k + " |"); for(int j = 1; j <= HORIZ; j++) { System.out.print("\t" + j*k); } System.out.println(); }

Good Programming Practices Do not change the value of the counter variable in your loop. –Do not attempt to manually adjust it to force an exit –Can cause subtle errors that are difficult to catch –Instead change your logic Do not put other expressions in the for control structure –Manipulations of other variables should appear before or within the body of the loop depending on whether or not you want to repeat them Put a blank line before and after each major control structure Try to limit nesting to three levels, if possible –More than three levels of indentation can get confusing Limit the for control header to one line if possible