90 likes | 193 Views
FOR LOOP WALK THROUGH. public class NestedFor { public static void main(String [] args ) { for ( int i = 1; i <= 7; i ++) { System.out.println (); for ( int j = 1; j <= i ; j++) { System.out.print ( "*" ); } } } }.
E N D
FOR LOOP WALK THROUGH publicclassNestedFor{publicstaticvoid main(String [] args) { for (inti = 1; i <= 7; i++) {System.out.println();for (intj = 1; j <= i; j++) {System.out.print("*"); } } }}
publicclassNestedFor// FIRST TIME THROUGH THE LOOP{publicstaticvoid main(String [] args) { for (inti = 1; i<= 7; i++) {System.out.println();for (intj = 1; j <= i; j++) {System.out.print("*"); } } }} i = 1 1 is less than or equal to 7 New line j = 1 1 is less than or equal to 1 Print * , increment, and loop 2 is NOT equal to 1 so fall out and return to outer for 01 02 * 03 04 05 06 07 08 09 10
publicclassNestedFor// SECOND TIME THROUGH THE LOOP{publicstaticvoid main(String [] args) { for (inti = 1; i<= 7; i++) {System.out.println();for (intj = 1; j <= i; j++) {System.out.print("*"); } } }} 2 i = 2 2 is less than or equal to 7 New line j = 1 1 is less than or equal to 2 Print * , increment, and loop 2 is less than or equal to 2 Print * , increment, and loop 3 is NOT equal to 2, so fall out and return to outer for 01 02 * 03 ** 04 05 06 07 08 09 10
publicclassNestedFor// THIRD TIME THROUGH THE LOOP{publicstaticvoid main(String [] args) { for (inti = 1; i<= 7; i++) {System.out.println();for (intj = 1; j <= i; j++) {System.out.print("*"); } } }} 3 i = 3 3 is less than or equal to 7 New line j = 1 1 is less than or equal to 3 Print * , increment, and loop 2 is less than or equal to 3 Print * , increment, and loop 3 is less than or equal to 3 Print * , increment, and loop 4 is NOT equal to 3, so fall out and return to outer for 01 02 * 03 ** 04 *** 05 06 07 08 09 10
publicclassNestedFor// FOURTH TIME THROUGH THE LOOP{publicstaticvoid main(String [] args) { for (inti = 1; i<= 7; i++) {System.out.println();for (intj = 1; j <= i; j++) {System.out.print("*"); } } }} 4 i = 4 4 is less than or equal to 7 New line j = 1 1 is less than or equal to 4 Print * , increment, and loop 2 is less than or equal to 4 Print * , increment, and loop 3 is less than or equal to 4 Print * , increment, and loop 4 is less than or equal to 4 Print * , increment, and loop 5 is NOT equal to 4, so fall out and return to outer for 01 02 * 03 ** 04 *** 05 **** 06 07 08 09 10
publicclassNestedFor// FIFTH TIME THROUGH THE LOOP{publicstaticvoid main(String [] args) { for (inti = 1; i<= 7; i++) {System.out.println();for (intj = 1; j <= i; j++) {System.out.print("*"); } } }} 5 i = 5 5 is less than or equal to 7 New line j = 1 1 is less than or equal to 5 Print * , increment, and loop 2 is less than or equal to 5 Print * , increment, and loop 3 is less than or equal to 5 Print * , increment, and loop 4 is less than or equal to 5 Print * , increment, and loop 5 is less than or equal to 5 Print * , increment, and loop 6 is NOT equal to 5, so fall out and return to outer for 01 02 * 03 ** 04 *** 05 **** 06 ***** 07 08 09 10
publicclassNestedFor// SIXTH TIME THROUGH THE LOOP{publicstaticvoid main(String [] args) { for (inti = 1; i<= 7; i++) {System.out.println();for (intj = 1; j <= i; j++) {System.out.print("*"); } } }} 6 i = 6 6 is less than or equal to 7 New line j = 1 1 is less than or equal to 6 Print * , increment, and loop 2 is less than or equal to 6 Print * , increment, and loop 3 is less than or equal to 6 Print * , increment, and loop 4 is less than or equal to 6 Print * , increment, and loop 5 is less than or equal to 6 Print * , increment, and loop 6 is less than or equal to 6 Print * , increment, and loop 7 is NOT equal to 6, so fall out and return to outer for 01 02 * 03 ** 04 *** 05 **** 06 ***** 07 ****** 08 09 10
publicclassNestedFor// SEVENTH TIME THROUGH LOOP{publicstaticvoid main(String [] args) { for (inti = 1; i<= 7; i++) {System.out.println();for (intj = 1; j <= i; j++) {System.out.print("*"); } } }} 7 i = 7 7 is less than or equal to 7 New line j = 1 1 is less than or equal to 7 Print * , increment, and loop 2 is less than or equal to 7 Print * , increment, and loop 3 is less than or equal to 7 Print * , increment, and loop 4 is less than or equal to 7 Print * , increment, and loop 5 is less than or equal to 7 Print * , increment, and loop 6 is less than or equal to 7 Print * , increment, and loop 7 is less than or equal to 7 Print * , increment, and loop 8 is NOT equal to 7, so fall out and return to outer for 01 02 * 03 ** 04 *** 05 **** 06 ***** 07 ****** 08 ******* 09 10
publicclassNestedFor// EIGHTH TIME THROUGH LOOP{publicstaticvoid main(String [] args) { for (inti = 1; i<= 7; i++) {System.out.println();for (intj = 1; j <= i; j++) {System.out.print("*"); } } }} 8 i = 8 8 is NOT less than 7 Break from for loop 01 02 * 03 ** 04 *** 05 **** 06 ***** 07 ****** 08 *******