Download presentation
Presentation is loading. Please wait.
Published byGilbert Moody Modified over 9 years ago
1
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("*"); } } } }
2
public class NestedFor // FIRST TIME THROUGH THE LOOP { 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("*"); } } } } i = 1 1 is less than or equal to 7 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 New line 01 02 * 03 04 05 06 07 08 09 10
3
public class NestedFor // SECOND TIME THROUGH THE LOOP { 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("*"); } } } } i = 2 2 is less than or equal to 7 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 New line 01 02 * 03 ** 04 05 06 07 08 09 10 2
4
public class NestedFor // THIRD TIME THROUGH THE LOOP { 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("*"); } } } } i = 3 3 is less than or equal to 7 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 New line 01 02 * 03 ** 04 *** 05 06 07 08 09 10 3
5
public class NestedFor // FOURTH TIME THROUGH THE LOOP { 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("*"); } } } } i = 4 4 is less than or equal to 7 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 New line 01 02 * 03 ** 04 *** 05 **** 06 07 08 09 10 4
6
public class NestedFor // FIFTH TIME THROUGH THE LOOP { 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("*"); } } } } i = 5 5 is less than or equal to 7 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 New line 01 02 * 03 ** 04 *** 05 **** 06 ***** 07 08 09 10 5
7
public class NestedFor // SIXTH TIME THROUGH THE LOOP { 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("*"); } } } } i = 6 6 is less than or equal to 7 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 New line 01 02 * 03 ** 04 *** 05 **** 06 ***** 07 ****** 08 09 10 6
8
public class NestedFor // SEVENTH TIME THROUGH LOOP { 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("*"); } } } } i = 7 7 is less than or equal to 7 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 New line 01 02 * 03 ** 04 *** 05 **** 06 ***** 07 ****** 08 ******* 09 10 7
9
public class NestedFor // EIGHTH TIME THROUGH LOOP { 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("*"); } } } } i = 8 8 is NOT less than 7 Break from for loop 01 02 * 03 ** 04 *** 05 **** 06 ***** 07 ****** 08 ******* 8
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.