Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the scope of an int in a for loop?

 
Adrian Mears
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try to compile the following code I get a compilation error on the second print statement. Why?

public class LoopTest {
public static void main (String args []) {
for ( int i = 0 ; i < 10 ; i++ )
System.out.println ( i ) ;
System.out.println ( i ) ;
}
}

Adrian
 
Henrique Boreg
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because the second print statement is outside the scope of the loop, so the "i" is not defined...add curly braces { } to encapsulate what you want to be inside the loop
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what your code really does is this:

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to the subject of this thread is "the entire body of the loop".

Now, as the other two people have said, your second print statement is outside the body, hence the error.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many programmers consider it good practice to always use braces with loops, even if these include a single statement. Not only is it easier to read, but it's also easier to add additional code in the future without introducing these types of errors.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many programmers consider it good practice


Count me as one of many.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Often one of the first things i do when working on old code is put in braces around 1-line blocks.
 
Jean-Sebastien Abella
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the code is indented I don't see the problem of omiting the bracket. Personaly I do it for small if condition that I can put on a single line.

But for those that do not use an IDE capable of formatting text it can be difficult I admit.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you ever decide to take the SCJP certification exam, this is a common gotcha to look out for.
 
Ashok George
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi..
thats a real gud idea...
we have to keep track of usch things while prep for SCJP!!!
thanx pals!!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jean-Sebastien Abella:
If the code is indented I don't see the problem of omiting the bracket.


Well, I wouldn't call it a *problem*. I just find that code is easier to parse when the if statements look more similarly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic