+ 2
is there easy way to understand nested loop and not get confused?
33 Réponses
+ 6
Practise helps. I also put 2 print statements. One in each loop (although you could just use one).
In your first loop, say you're using int i,
Then print that, second loop, using int j, print that.
You should have something like
i = 0
j = 0
j = 1
j = 2
i = 1
j = 0
j = 1
j = 2
i = 2
j = 0
j = 1
j = 2
Does that help?
+ 6
eli The hands of a clock are a great visual representation of nested loops.
The hour hand is the outer loop.
The minute hand is the inner loop.
The hour hand "counter" loops around the clock face from 0 (12 o'clock position) back to 12.
For each increment of the hour hand "counter",
the minute hand "counter" loops around the clock face from 0 to 60.
The minute "counter" must finish its loop before the hour "counter" can increment again.
If you add seconds hand "counter", you can expand the analogy to 3-level nested loop.
The seconds "counter" must finish looping from 0 to 60,
before the minute "counter" can increment.
for (int hour = 0; hour <= 12; hour++) {
for (int minute = 0; minute <= 60; minute++) {
for (int seconds = 0; seconds <= 60; seconds++) {
System.out.println( hour + ":" + minute + ":" + seconds);
}
}
}
+ 6
eli Yet another way of thinking about nested loops is doing reps at a gym.
You want to do 5 sets of lifting a barbell 10 times.
The 5 sets is the outer loop.
The lifting the barbell 10 times is the inner loop.
You count in your head lifting the barbell 10 times,
and then pause counting that as 1st set.
You then start lifting the barbell 10 times again,
and again pause at the end counting that as 2nd set.
And so on until you have finished 5 sets of lifting the barbell.
The 2 key concepts to understand with nested loops are:
👉 The inner loop must completely finish before the outer loop can increment its counter.
👉 After the outer loop does increment, the inner loop starts over again from scratch.
+ 4
When I was learning Java, I really didn't understand nested loop untill I understand nested arrays.
The examples that made it clear to me is an array that goes like this
{
{0, 1, 2, 3},
{4, 5, 6, 7},
}
This example was given in a textbook: Java the complete reference (4th edition) but I can't remember the author's name
And I have to print each of the elements. Then later I created a 4x4 matrices library.
Arrays make it easier for me to understand nested loop.
I have had previous knowledge in python's list tho and I understand the array concept clearly. Also, I've used nested loop in some of my codes but didn't actually understand how it works untill then
+ 4
eli , so there's a task you're trying to do?
Can you share it and your code, as per this guide?
https://sololearn.com/compiler-playground/Wek0V1MyIR2r/?ref=app
What might also help is going the long way around first. Say you want a 3 star high triangle.
*
**
***
The long way:
System.out.println("*");
System.out.println("**");
System.out.println("***");
What do we see? First line, 1 star, second line, 2 stars, third line, 3 stars.
We now know (also thanks to Bob_Li 's tip) that we need an outer loop to loop 3 times.
for(int outer = 0; outer < 3; outer++)
We'd like the inner loop to print up to 3 stars, so we can also do 3 times.
for(int outer = 0; outer < 3; outer++){
for(int inner = 0; inner < 3; inner++){
//code
}
}
Using this, we can then repeat our prints. We know that println will put the star on a new line, it must go on the outer loop, that means print must go on the inner loop.
What do you think?
+ 3
eli
Well, the outer loop controls the row, the inner loop controls the colums in those rows.
The outer loop is simple. The inner loop is where you have to be creative, to make it do different patterns depending on the row number.
https://www.simplilearn.com/tutorials/java-tutorial/pattern-programs-in-java
+ 3
eli I tested your code and noticed it produced pyramid one height lower than number entered.
Don't know if that was intentional.
Also you don't need the z loop.
You just need if statement inside your y loop to decide between printing " " or "* "
https://sololearn.com/compiler-playground/ckSdFzQa1Hdo/?ref=app
+ 2
Great tip Shardis Wolfe
+ 2
Aarti Yadav please don't hijack a thread with an unrelated question.
You can search the forum, I'm sure if you type "br html", you'll find your answer.
Maybe reading this will help too:
https://sololearn.com/compiler-playground/W0uW3Wks8UBk/?ref=app
+ 2
maybe think of loops as people doing something repeatedly.
A simple loop might be:
Rick is counting from 1 to 10.
Nested loop will be two people counting:
Morty counts from 1 to 10 everytime Rick counts a number.
So Morty is actually counting 1 to 10 ten times...
+ 2
yes i know that basic one but when im going to create the triangle or different pattern i don't know what to do
+ 2
thank you to yall, i appreciate it a lot😭😭 I'll try harder to understand it and will update you. TYSM!!
+ 2
eli The stars example that Ausgrindtube posted gets into more complicated topic of nested loops where you can have current outer loop counter affect inner loop.
But sounds like you are having trouble with basic idea of nested loops.
For next couple days, look at what you do each day.
See if there is anything you do which involves doing some action multiple times and then repeating that again a little later.
+ 2
wait I'll paste it here
+ 2
Ausgrindtube
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan= new Scanner (System.in);
System.out.println("Enter a number from 1–10:");
int a= scan.nextInt();
if (a<=10) {
int x=0, y=0, z=0;
for (x=0; x<a; ++x){
for (y=a; y>x; --y){
System.out.print(" ");
}
for (z=0; z<x; ++z){
System.out.print("* ");
}
System.out.println();
}
} else {
System.out.println("error. I said 1–10 only");
}
scan.close();
}
}
+ 2
Shardis Wolfe sorry but can you explain how did that happen?😹 i tried to understand it but yeah
+ 2
eli with your original code, you needed to modify the exit condition on your x loop from "x<a" to "x<=a" to have pyramid height match the entered number.
With my version, you can either do that,
Or modify the exit condition of y loop from "y>0" to "y>=0"
Regarding the if-else inside the y loop,
outer loop x is counting up from 0 to a,
while inner loop y is counting down from a to 0.
This means x and y values will cross each other at some point during each inner loop.
So at start of inner loop y,
it will start out printing blank spaces.
Until y counts down enough to be equal to less than current x.
Once y passes current x, it will start printing star-space instead.
And keep doing that until it finishes counting down to 0.
At which point y loop ends, you print newline, increment x, and start on next row of pyramid.
Tracing the code by hand and tracking the x and y variables on some scrap paper as you do that can possibly help you see what is happening.
+ 1
Ye aap kaisa hi bhi
+ 1
What is the use of br tag in html