110 likes | 382 Views
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: **********
E N D
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
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 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(); } } }
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