Results 1 to 9 of 9
Thread: Please help clear up confusion
- 04-17-2011, 06:48 PM #1
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
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); } } }
- 04-17-2011, 07:02 PM #2
Member
- Join Date
- Jan 2011
- Posts
- 67
- Rep Power
- 0
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:
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.Java Code:for(int i = 1; i <= 10; i++)
Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.
- 04-17-2011, 07:04 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
As your code is now, you don't define your variable i anywhere. Do this:
Now your variable i is defined for just the loop body (and the header part of your loop).Java Code:for(int i = 1; i <= 10; i++){ sum = sum + i; System.out.println(i); }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-17-2011, 07:13 PM #4
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
It is not the professors fault it is my own. I miscopied the program. It should have been:
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.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); } }
- 04-17-2011, 07:17 PM #5
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
- 04-17-2011, 07:20 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 04-17-2011, 07:33 PM #7
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
That's the way the professor originally wrote the program.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);
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?
- 04-17-2011, 08:34 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 67
- Rep Power
- 0
- 04-17-2011, 08:55 PM #9
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Similar Threads
-
Confusion here @@' Help!
By pleasurelyours in forum New To JavaReplies: 7Last Post: 06-09-2010, 03:42 PM -
confusion in paragraph
By JavaJunkie in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-19-2009, 03:02 PM -
Tic Tac Toe confusion
By jigglywiggly in forum New To JavaReplies: 15Last Post: 04-12-2009, 01:47 AM -
Confusion over arrays
By dbashby in forum New To JavaReplies: 5Last Post: 04-04-2009, 09:38 PM -
bracket confusion...?
By gallimaufry in forum New To JavaReplies: 4Last Post: 10-28-2008, 11:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks