Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8407819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:28:20+00:00 2026-06-09T23:28:20+00:00

From what I know, the scope of a ‘for’ loop, without a set of

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:28:21+00:00Added an answer on June 9, 2026 at 11:28 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have come to know from book that for declaring a structure variable it
I know how to set an initial bundle level, for just to be deployed
I want to call a method from one class to another, don't know if
I know from documentation we can find distance between two CLLocation points using the
I know from wikipedia for example that exception handling is used in an application
I know from here that I can pass an ignore option in the jQuery.validate
I know from reading the assoc. Google group that there is not currently an
I know from Algebra class that with ABC and 123 we can make 216
So I know from various online sources that it is generally a no-no to
In an ASP.Net WAP, the home page /default.aspx is receiving periodic requests from I-know-not-where.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.