Presentation is loading. Please wait.

Presentation is loading. Please wait.

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;

Similar presentations


Presentation on theme: "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;"— Presentation transcript:

1

2 Nested For Loops

3 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:**************************************************

4 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

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

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

7 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

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

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

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

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

12 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


Download ppt "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;"

Similar presentations


Ads by Google