Surender Thakran
Surender Thakran

Reputation: 684

Scope of a 'for' loop

From what I know, the scope of a 'for' loop, without a set of parentheses after it, is just one statement. Right?

So how come this code:

    for(int x = 0; x < 3; x++)
        if(x < 2)
            System.out.println("hello");
            System.out.println("world");

gives the output:

hello
hello
world

Is the statement in the if is also considered part of the for loop? Of course it is, but my question is: Why?

Does what actually is that that the scope is a block right after for statement because the above code when modified like this:

    for(int x = 0; x < 3; x++)
        if(x < 2) {
            System.out.println("hello");
            System.out.println("world");
        }

gives the output:

hello
world
hello
world

Most of the answers are about explaining the flow control in the above code. I already know that. My question was about the rule of the for loop scope.

Is the rule actually that: the scope of an braceless for loop is the next block of statements immediately after it?

Upvotes: 0

Views: 1184

Answers (6)

obataku
obataku

Reputation: 29646

You should read Braceless if considered harmful. This post was specifically made because of examples just like this; the confusion that brace-less control flow statements can leave you scratching your head for quite a while, especially with misleading indentation (such as in your example).

The code you pasted is equivalent to the following,

for (int x = 0; x < 3; x++) {
  if (x < 2) {
    System.out.println("hello");
  }
}
/* outside of the for */
System.out.println("world");

As you can see, the loop iterates three times; the first two, it will print "hello". After the loop completes, it will print "world".


The reason this works is clear in reading Chapter 14 of the Java Language Specification. In fact, it makes sense to think of blocks as statements, as per §14.5.

for (int x = 0; x < 3; x++)
   if (x < 2) 
      System.out.println("hello");
      System.out.println("world");

Looking at the descriptions of if (§14.9) and basic for (§14.14.1), we see both merely take a statement; in this case, we can see our for statement contains if statement, which itself encapsulates your println("hello") statement. Following the for statement, you then have your println("world") statement.

for (int x = 0; x < 3; x++)
    if (x < 2) { 
      System.out.println("hello");
      System.out.println("world");
    }

Here, we see the for statement body is the if statement, which encapsulates a block that contains 2 statements, namely both your println statements. Note that this is indeed not the same as the former.

Hopefully this clears things up for you.

Upvotes: 3

NullPoiиteя
NullPoiиteя

Reputation: 57322

The one line after the loop ,condition is considered in the body of loop ,condition you did not use the {} so the only if is consider in the for body

   for(int x = 0; x < 3; x++)
        if(x < 2) 
            System.out.println("hello");
            System.out.println("world");

gives the output hello hello world because

the only 1 line after the if statement is consider in the for loop when the loop end the world print

its like

for(int x = 0; x < 3; x++)
      {  if(x < 2) 
            System.out.println("hello");
       }
     System.out.println("world");

and in the

 for(int x = 0; x < 3; x++)
        if(x < 2) { 
            System.out.println("hello");
            System.out.println("world");
        }

the both System.out.println("hello"); System.out.println("world"); consider in the for loop

Upvotes: 1

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

It's doing the right thing. It prints "hello" for i = 0 and 1 and then for loop ends and "world" is printed.

I think you are confused by the alignment, following is how the first one should look -

for(int x = 0; x < 3; x++) {
    if(x < 2) {
        System.out.println("hello");
    }
}
System.out.println("world");

And the second one -

for(int x = 0; x < 3; x++) {
    if(x < 2) { 
        System.out.println("hello");
        System.out.println("world");
    }
}

More readable and easy to understand the logic.

Upvotes: 0

kosa
kosa

Reputation: 66637

First x=0
 Then if (x < 2) condition satisfies (again no braces, so only one statement executes)
   Prints "hello"
for loop continues
x=1
 Then if (x < 2) condition satisfies (again no braces, so only one statement executes)
   Prints "hello"
  for loop continues
x=2
 Then if (x < 2) condition NOT satisfies, so if statement won' execute, moves to print "world"
   Prints "world"

First snippet will be treated like:

for(int x = 0; x < 3; x++){
        if(x < 2) {
            System.out.println("hello");
        }
}
            System.out.println("world");

Upvotes: 3

jaychapani
jaychapani

Reputation: 1509

When you don't put {} only next line after the loop or conditional statement is considered as a part of its scope.

Upvotes: 0

Bharat Sinha
Bharat Sinha

Reputation: 14363

See it like this...

for and if have control over next statement or next block enclosed in {...}.

If {} are missing only next statement is considered as the body.

In first case, i have corrected indentation so that body part is clearly visible.

for(int x = 0; x < 3; x++)  
    if(x < 2)   
        System.out.println("hello");
System.out.println("world"); 

Upvotes: -1

Related Questions