Results 1 to 9 of 9
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Please help clear up confusion

    The below program won't compile and the reason given by the professor is that the variable is not available outside of the loop. I am confused about this. Does this mean that i is not available outside of the parenthesis because it was not declared prior to the loop? I thought everything within the brackets was part of the loop. In other words, I thought that sum = sum + i and System.out.println(i) were part of the loop body. Is the loop body not part of the loop?

    I rewrote the program and it works fine if i is declare prior to the loop. I just don't understand what is considered inside and outside of the loop.

    Java Code:
    public class practice {
    
        public static void main(String[] args){
    
    		//declare variable sum
    		int sum = 0;
    
    		//create for loop
    		for(i = 1; i <= 10; i++){
    			sum = sum + i;
    			System.out.println(i);
    		}
    	}
    }

  2. #2
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Not sure why your professor told you that was the problem, the actual problem is that i is not declared when it is written as you posted. You can declare i inside the for statement by preceding i in the first part of the for statement with the data type of i like you normally would outside a for statement.

    Like so:
    Java Code:
    for(int i = 1; i <= 10; i++)
    The scope of i when written like this would be within the for loop, so any code after the closing brace of the for loop would not be able to use it but code using it inside like you have should be fine.
    Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default

    As your code is now, you don't define your variable i anywhere. Do this:

    Java Code:
    		for(int i = 1; i <= 10; i++){
    			sum = sum + i;
    			System.out.println(i);
    		}
    Now your variable i is defined for just the loop body (and the header part of your loop).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Skiller View Post
    Not sure why your professor told you that was the problem, the actual problem is that i is not declared when it is written as you posted. You can declare i inside the for statement by preceding i in the first part of the for statement with the data type of i like you normally would outside a for statement.

    Like so:
    Java Code:
    for(int i = 1; i <= 10; i++)
    The scope of i when written like this would be within the for loop, so any code after the closing brace of the for loop would not be able to use it but code using it inside like you have should be fine.
    It is not the professors fault it is my own. I miscopied the program. It should have been:
    Java Code:
    public class practice {
    
        public static void main(String[] args){
    
             int sum = 0;
    
             for(int i = 1 ; i <= 10 ; i++)
                  sum = sum+i;
                  System.out.println(i);
        }
    }
    The program doesn't compile. I get a "cannot find the variable i" compile error for the System.out.println(i); line. It appears to me to be within the scope so I do not understand why it cannot find the variable i.

  5. #5
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    As your code is now, you don't define your variable i anywhere. Do this:

    Java Code:
    		for(int i = 1; i <= 10; i++){
    			sum = sum + i;
    			System.out.println(i);
    		}
    Now your variable i is defined for just the loop body (and the header part of your loop).

    kind regards,

    Jos
    Thanks. It was my mistake as I miscopied the program. Sorry. The way you wrote the code is actually the way it should have been originally. It still doesn't compile though due to the reason mentioned by the professor.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jim01 View Post
    Thanks. It was my mistake as I miscopied the program. Sorry. The way you wrote the code is actually the way it should have been originally. It still doesn't compile though due to the reason mentioned by the professor.
    Show your code as it is now; I noticed that you forgot a couple of curly brackets in your latest reply (causing the error mesage).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Show your code as it is now; I noticed that you forgot a couple of curly brackets in your latest reply (causing the error mesage).

    kind regards,

    Jos
    Java Code:
    public class practice {
    
        public static void main(String[] args){
    
             int sum = 0;
    
             for(int i = 1 ; i <= 10 ; i++)
                  sum = sum+i;
                  System.out.println(i);
    That's the way the professor originally wrote the program.

    You are right though, once I put the brackets in, the program worked correctly. The professor didn't add the brackets in his program. This brings up another question though. He has stated that you do not need brackets in a loop or a branch statement if you only have one. That is why I didn't pay any attention to that. it would appear though that this isn't correct. Or maybe it is because there is more than one statement in the loop body?

  8. #8
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by jim01 View Post
    He has stated that you do not need brackets in a loop or a branch statement if you only have one. That is why I didn't pay any attention to that. it would appear though that this isn't correct. Or maybe it is because there is more than one statement in the loop body?
    Yer if there is only one statement in the body of the loop or branch then you don't need the braces, but IMO it's a lot better to add them in anyway as it's a lot clearer what is intended to happen.
    Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.

  9. #9
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Skiller View Post
    Yer if there is only one statement in the body of the loop or branch then you don't need the braces, but IMO it's a lot better to add them in anyway as it's a lot clearer what is intended to happen.
    OK. That explains it then. Thanks to everyone for their help in explaining this to me. I am finding loops to be very confusing, especially nested loops. I get lost in them.

Similar Threads

  1. Confusion here @@' Help!
    By pleasurelyours in forum New To Java
    Replies: 7
    Last Post: 06-09-2010, 03:42 PM
  2. confusion in paragraph
    By JavaJunkie in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-19-2009, 03:02 PM
  3. Tic Tac Toe confusion
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 04-12-2009, 01:47 AM
  4. Confusion over arrays
    By dbashby in forum New To Java
    Replies: 5
    Last Post: 04-04-2009, 09:38 PM
  5. bracket confusion...?
    By gallimaufry in forum New To Java
    Replies: 4
    Last Post: 10-28-2008, 11:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •