Nested for loop with examples | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Nested for loop with examples

I can't understand how the flow of nested loops are when I saw several * print programs

3rd Sep 2018, 3:06 PM
shivam singh
shivam singh - avatar
3 Answers
0
for (int i = 1; i <= 4; i++) { for (int k = 1; k <= 3; k++) { // some code } } i, k values on following iterations: i = 1, k = 1 i = 1, k = 2 i = 1, k = 3 i = 2, k = 1 i = 2, k = 2 i = 2, k = 3 i = 3, k = 1 i = 3, k = 2 i = 3, k = 3 i = 4, k = 1 i = 4, k = 2 i = 4, k = 3 In other words, whole inner loop will be executed for each iteration of outer loop. If outer loop runs n times, inner loop runs m times, then code in the inner loop will be executed n*m times.
3rd Sep 2018, 3:12 PM
Steppenwolf
Steppenwolf - avatar
0
no bro it didn't meet my expectations I learned nothing from your comment
3rd Sep 2018, 3:16 PM
shivam singh
shivam singh - avatar
0
Perhaps, due to lack of the basic programming knowledge: syntax, terms, keywords. I personally can not explain it in other words in a simple way. My advice for you is to finish your Java course on SoloLearn at first. Try to run this code, maybe it helps: for (int i = 1; i <= 4; i++) { for (int k = 1; k <= 3; k++) { System.out.print("i = " + i); // prints out i value System.out.println(", k = " + k); //prints out k value } }
3rd Sep 2018, 3:27 PM
Steppenwolf
Steppenwolf - avatar