I can't understand how these 'nested' loops work. Can anyone try explain to me in the most simplest form why they are needed and how they work?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I can't understand how these 'nested' loops work. Can anyone try explain to me in the most simplest form why they are needed and how they work?
Last edited by javapol; February 21st, 2013 at 07:01 AM. Reason: [code=java] moved
What is printed out when the code is executed?
If you don't understand my answer, don't ignore it, ask a question.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
How is the value of the variable: star changed to the digits that you posted as the program's output?
If you don't understand my answer, don't ignore it, ask a question.
Sorry wrong code, edited now.
Looks the same to me.
If you don't understand my answer, don't ignore it, ask a question.
Thats it now,
Have you tried playing computer with the code? Use a piece of paper to write down the values of the variables as each statement is executed.
If you don't understand my answer, don't ignore it, ask a question.
I just need to know what happens in nested loops, not that particular program, I mean if a loop loops something 10 times and a loop outside it loops 5 times does this mean that the inside is looped 50 times?
5*10 = 50inside is looped 50 times
If you don't understand my answer, don't ignore it, ask a question.
The compiler reads from top-down, so it will enter the outer for-loop and executes the code inside of it (i.e. inner for-loop). Once the inner for-loop has finished iterating, the outer for-loop iterates once more and executes the inner for-loop again. This continues until the outer for-loop has finished iterating.
Here's a very simple example I made on the spot:
import java.util.Scanner; public class something { public static void main(String[] args) { Scanner input = new Scanner(System.in); while(true) { System.out.println("Enter 'play' to play or 'no' to not play"); String data = input.nextLine(); if(data.equalsIgnoreCase("no")) { break; } while(data.equalsIgnoreCase("play")) { // more code } } } }
If the user enters "no", then the outer loop ends and the code within the inner loop is never executed. On the other hand, if the user enters, "play", then the code within the inner loop is executed. If the user enters anything else, then the inner loop is not executed and since there is no else statement with the if-statement, the outer loop re-iterates until the user enters valid input.
Not all programs will use nested loops, you only have nested loops in your program if it requires them. There generally are many ways of writing code to achieve the same functionality.