Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes What is the scope of an int in a for loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the scope of an int in a for loop?" Watch "What is the scope of an int in a for loop?" New topic
Author

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

Adrian Mears
Greenhorn

Joined: Aug 30, 2003
Posts: 5
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

Joined: Aug 11, 2005
Posts: 23
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


"I wrote an all-Java OS in 1998 but can't be sure if or how it works... it's still booting"
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
So what your code really does is this:



The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Many programmers consider it good practice


Count me as one of many.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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

Joined: Jul 29, 2005
Posts: 60
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

Joined: Oct 14, 2002
Posts: 8764
    
    5
If you ever decide to take the SCJP certification exam, this is a common gotcha to look out for.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ashok George
Ranch Hand

Joined: Mar 25, 2005
Posts: 87
hi..
thats a real gud idea...
we have to keep track of usch things while prep for SCJP!!!
thanx pals!!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the scope of an int in a for loop?
 
Similar Threads
conversion Doubt
code has an error please help
operators
a question about super command