sam_rox
sam_rox

Reputation: 747

If an outer for loop has no braces, will an inner for loop be considered as a single statement?

I know that if no curly braces are used for a for loop then there can be only a single statement following it. My question is in this code:

for(int i=0;i<=10;i++)
  for(int j=10;j>=1;j--){
    if(i!=j)
      break;
    n=n+1;
  }
System.out.println(n);

Does the first loop consider the entire second for loop

for(int j=10;j>=1;j--){
  if(i!=j)
    break;
  n=n+1;
}

as a single statement? I know it is good coding convention to use braces but I just want to know what happens here.

Upvotes: 0

Views: 75

Answers (4)

Duncan Jones
Duncan Jones

Reputation: 69339

A for statement can be followed immediately by another for statement without the need for braces. If you are ever in doubt about this sort of info, check the language specification:

14.14.1 The basic for Statement describes the basic for statement as:

BasicForStatement:
  for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement

If you click the link for Statement you see all the valid options for what can follow:

Statement:
  StatementWithoutTrailingSubstatement
  LabeledStatement
  IfThenStatement
  IfThenElseStatement
  WhileStatement
  ForStatement    

Notice the ForStatement in the line above.

Upvotes: 3

user3437460
user3437460

Reputation: 17454

Yes, the first (outer) loop will consider the entire 2nd (inner) loop.

Consider the following example:

for(int x=0; x<5; x++)     //Will only consider the next statement (enter y-loop)
    for(int y=0; y<5; x++) //Will only consider the next statement (enter z-loop)
        for(int z=0; z<5; x++) //Will only consider the next statement (enter if-statement)
            if(...)

The above statements work because even though each loop (without curly braces) will only loop the next statement, but in this case, the next statement is another loop statement.

The same applies to if-statements.

Consider this example 1:

if(a==0)
    if(b==0)
        System.out.println("This comes from b");
    else
        System.out.println("This else comes from b");

In the above code snippet, the else belong to the inner if-statement because the outer if only consider the next statement.

Consider this example 2:

if(a==0){
    if(b==0)
        System.out.println("This comes from b");
}    
else
    System.out.println("This else comes from b");

In the 2nd example, the else now belongs to the first if-statement.

Personally, I prefer to use braces to ensure clarity, however sometimes I still omit them when I am very sure what I am doing and when I know exactly what are the effects of omitting the braces.

Upvotes: 1

ManKeer
ManKeer

Reputation: 543

Definitely yes, The second for loop will be considered as a single statement for the first one, this loop represents the inner loop.

Upvotes: 0

Smutje
Smutje

Reputation: 18143

does the first loop consider the entire second for loop - Yes, and yes it is good convention because omitting the braces is more costly for a non-unified coding environment than two omitted characters.

Upvotes: 0

Related Questions