Nested For Loops in Java

Hello

I am taking a shot in the dark here.

Could anyone explain in simple terms how a nested for Loop works please?

I have just got used to working with single looping structures and I have been introduced to nested loops and I just cannot seem to get my head around them.

Thanks
Kevin_CoorsAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
krakatoaConnect With a Mentor Commented:
class Loop{

public static void loop(){
	for (int a=0;a<10;a++){
       	    System.out.println("Out loop value is --->"+a);
       		for (int b=0;b<20;b++){
             			System.out.println("Inner loop value is ---> "+b );
       		 }
	}
}

public static void main(String[] args){loop();}

}

Open in new window

0
 
krakatoaCommented:
Kevin - we've covered this one before I am pretty sure. What difficulties have you run into now?
0
 
krakatoaCommented:
In 'short' - a loop completes whatever task you set it, up to its iteration limit. If it is enclosed by another loop, - (nesting) - that outer loop takes another iteration, and the inner loop is repeated.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Kevin_CoorsAuthor Commented:
Hi Krakatoa

We covered nested if statements the last time

Thanks
0
 
krakatoaCommented:
We covered nested if statements the last time

*Did* you mean nested, then? If so, there's a short and v simple one above. Let us know if this is not what you had in mind. There is plenty of help here if it isn't.
0
 
krakatoaCommented:
And you didn't knock a single point off because I names the outer loop 'out' instead of 'outer' ! ;)
0
 
Kevin_CoorsAuthor Commented:
Thanks again :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.