+ 2
Hi can anybody explain this loop
4 Answers
+ 1
Hi! The outer loop sets the bounderies, then the inner loop prints out all numbers up to the boundery:
1: 0
2: 0 1
3: 0 1 2
4: 0 1 2 3
+ 1
Yes, the while loop increases its output in every iteration of the for loop, and it depends on the value of the i variable in the for loop.
+ 1
Thanks for your answers i got it
0
let split the code to two section . outer . inner. outer is for loop . inner i the while loop. the inner loop does not effects the value of i .
for (int i = 1; i < 5; i++) {...
i get values 1 2 3 4 when i get 5 , it violate the condition i<5 therefor ending the outer loop
for each i the inner loop is done .
int j = 0;
while (j < i) {
System.out.print(j + " ");
j++;
}// while
for every i , j start from 0 .
so when i = 1 j get 0 and printed 0 then j=1 which ends inner loop
when i=2 j get again 0 and also 1 then printed 0 1 then j=2 which ends inner loop
when i=3 j get again 0 and also 1 2 then printed 0 1 2 then j=3 which ends inner loop
when i=4 j get again 0 and also 1 2 3 then printed 0 1 2 3 then j=4 which ends inner loop
when i=5 outer loop ends