Hi guys just wanted some clarification on two issues.

Nested Loops, I just want to clarify how i should read these for example:

public void nestedForLoop()
   {
       int i = 0;
       int j = 0;
       
     for (i = 0; i < 10; i++)
       {
         for (j = 0; j < 3; j++)
         {
             System.out.println("Inside both loops: " + i +"\t"+ j);
         }
       System.out.println("Inside outer loop: " + i +"\t"+ j);
      }
      System.out.println("Outside Loop: " + i +"\t"+ j);
   }

How should i read a nested for loop? Like would this run essentially 30 times? Does it basically say run the inside loop 10 times? So then does it basically run once, go into the nested loop, run 3 times, then go back into the outer loop, then back into the nested loop 3 times?

And I'v read a few definitions and my lecturer has failed what a 'literal' is effectivley. So can i ask, what are they?

Thanks in advance, just want to clear the ideas in my head, because they're surrounded by a lot of confusion.

Dani AI

Generated

Nested loops run the inner loop to completion once for every single iteration of the outer loop. As pointed out, the number of times the inner-body executes equals outerIterations × innerIterations. The statement(s) that follow the inner loop but still sit inside the outer loop execute once per outer iteration. Because the original code declared i and j outside the for headers, those variables remain in scope after the loops and therefore hold the values they had when the loops terminated.

Step-by-step flow to keep in mind: outer counter is initialized, outer condition is tested, then the inner loop is entered and its counter is initialized and run until its condition fails. Control returns to the outer body, any outer-only statements execute, then the outer counter advances. Common pitfalls are off-by-one conditions (using < vs <=), assuming an inner counter "remembers" a value between outer iterations when it is actually reinitialized, and relying on variables declared in the header when they are out of scope.

For variable-scope clarity, this pattern keeps counters local:

for (int a = 0; a < 2; a++) {
    for (int b = 0; b < 3; b++) {
        // inner work
    }
}
System.out.println(a); // compile error: a not visible here

Declaring counters inside the for header confines them to the loop; declaring them outside makes them visible after the loop and yields the final loop values there.

A literal is simply a fixed value written directly in source code; was correct to call out literal values. Java literal forms (integer, floating, boolean, character, string, null, plus suffixes and alternate bases) are defined in the language spec — see the Java Language Specification on literals for the exact grammar and rules: Literals (JLS 3.10). For a concise explanation of for loops and examples, the Oracle tutorial is helpful: The for Statement.

Recommended Answers

All 4 Replies

How should i read a nested for loop? Like would this run essentially 30 times? Does it basically say run the inside loop 10 times? So then does it basically run once, go into the nested loop, run 3 times, then go back into the outer loop, then back into the nested loop 3 times?

Yes, the inner code will be run 30 times. Basically, the outer loop is run 10 times, and each time the outer loop is run the inner loop is run 3 times, (plus the output line is run once each time the outer loop is run).

And I'v read a few definitions and my lecturer has failed what a 'literal' is effectivley. So can i ask, what are they?

A literal is not a terminology I am immediately familiar with, what is the context?

Well i haven't really got an example but maybe you can explain this definition i found to me because i'm not sure what it means:

A literal represents a value of a certain type where the type describes how that value behaves.

There are different types of literals namely number literals, character literals, boolean literals, string literals,etc.

A literal is a fixed value declared in the source code such as:
123
true
'A'
"A String"
{1,2,3}

see:

This link has a good explanation of primitives and literals. Let us know if you still have questions after reading this page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.