1 / 11

Nested For Loops

Nested For Loops. 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. Output: **********

florrie
Download Presentation

Nested For Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Nested For Loops

  2. 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 Output: ********** ********** ********** ********** ********** Inner Loop

  3. 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

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

  5. 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); } } }}

  6. 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

  7. 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); } } } }}

  8. 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(); } }

  9. 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(); } }

  10. Nested For Loops, multiplication tableexample # 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(); } } }

  11. 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

More Related