Question

There's one thing bugging me about this code

    for (int i = 0; i<10; i++)
    {
          for (int c = 0; c < 10; c++)                  
              if(i == c)
                  System.out.print("*");
              else
                  System.out.print("-");

        System.out.println(".");
    }

I know the inner for-loop has to complete before the outer-loop can loop again. Why is it that the System.out.println(".") only occurs once in the inner loop even though it should loop multiple times?

Was it helpful?

Solution

The line

System.out.println(".");

is outside the nested for. If you want it inside, you should use brackets {}:

for (int i = 0; i < 10; i++) {
    for (int c = 0; c < 10; c++) {
        if (i == c) {
            System.out.print("*");
        } else {
            System.out.print("-");
        }
        System.out.println(".");
    }
}

Remember that if you don't put brackets {}, the body of the for loop will only be one statement (the one which is next to the for declaration).

OTHER TIPS

because it is out side of inner for loop, use parentheses around for loop body for better readability and to avoid such confusion

Not sure what you were expecting, but your code is equivalent to:

for (int i = 0; i<10; i++)
{
      for (int c = 0; c < 10; c++) {
          if(i == c) {
              System.out.print("*");
          } else {
              System.out.print("-");
          }
      }

      System.out.println("."); // This is OUTSIDE the inner loop.
}

Moral of the story: When in doubt, use parenthesis.

Inner loop must complete all of it's passes/times and not just once before running parent loop again.

for (int i = 0; i<10; i++)
{
      for (int c = 0; c < 10; c++)  
          {            
          if(i == c)
              System.out.print("*");
          else
              System.out.print("-");
          }
    System.out.println(".");//The . would get printed 10 times as it is inside first for loop.
}

This confusion is because of the absence of Curly Braces. Your code has nothing wrong in it. The last System.out.println("."); which is confusing you is part of the outer loop. So your code is equivalent to this:

for (int i = 0; i<10; i++)
{
      for (int c = 0; c < 10; c++) {                 
          if(i == c){
              System.out.print("*");
          }
           else{
              System.out.print("-");
          }
    System.out.println(".");
    }
}

Your final statement is part of the outer loop.

System.out.println(".");

Since you have a single if-else statement in the inner loop, braces were not needed. Always use braces to define the scope of the loop and for the sake of readability.

for (int i = 0; i<10; i++)
{
      for (int c = 0; c < 10; c++) {                 
          if(i == c) {
              System.out.print("*");
          } 
          else {
              System.out.print("-");
          } 
      }

    System.out.println(".");
}

the Sysout in your code belongs to the outer loop and not inner loop.

If you don't put curly braces after your for loop it just takes only 1 statement after it.

This should work in your case.

for (int i = 0; i<10; i++)
{
      for (int c = 0; c < 10; c++)                  
       {   if(i == c)
              System.out.print("*");
          else
              System.out.print("-");

    System.out.println(".");}
     }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top