Results 1 to 14 of 14
Thread: Can smb explain this question?
- 01-29-2015, 07:13 AM #1
Member
- Join Date
- Jan 2015
- Posts
- 48
- Rep Power
- 0
- 01-29-2015, 07:26 AM #2
Member
- Join Date
- Jan 2015
- Posts
- 25
- Rep Power
- 0
Re: Can smb explain this question?
In the loop u have set the variable i to 0 and also put a limit upto 10.
- 01-29-2015, 08:26 AM #3
Re: Can smb explain this question?
Try to figure it out on paper. When you enter the loop for the first time, the value of i is set to 0, throwing away the earlier value of 5.
That first time through the loop, i get a value of 1 after line 6.
What happens to the value of i in the next time around the loop (the next iteration)? And the next, and the next, until the value of i does not satisfy i<=10 and the loop terminates?
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 01-29-2015, 08:26 AM #4
- 01-29-2015, 11:02 AM #5
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
- 01-29-2015, 01:41 PM #6
- 01-29-2015, 05:09 PM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Can smb explain this question?
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 01-29-2015, 05:20 PM #8
Member
- Join Date
- Jan 2015
- Posts
- 48
- Rep Power
- 0
- 01-29-2015, 05:20 PM #9
Member
- Join Date
- Jan 2015
- Posts
- 48
- Rep Power
- 0
Re: Can smb explain this question?
How does one get a value of 1 after line 6??
- 01-29-2015, 05:48 PM #10
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Can smb explain this question?
No, its irrelevant.
Java Code:public class Test { public static void main(String[] args) { int i = 5; for (i = 0; i <= 10; System.out.println("i about to increment, i = " + i), i++, System.out.println("i increments to " + i)) { System.out.println("i = " + i); i = i + 1; System.out.println("now i = " + i); } System.out.println(i); } } // now rewrite it like this; public class Test { public static void main(String[] args) { int i = 5; // corrected thanks to Gimbal for (i = 0; i <= 10;) { System.out.println("i = " + i); i = i + 1; System.out.println("now i = " + i); System.out.println("i about to increment, i = " + i); i++; System.out.println("i increments to " + i); } System.out.println(i);
You can read about it here -> Chapter*14.*Blocks and Statements
Regards,
JimLast edited by jim829; 01-29-2015 at 06:48 PM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 01-29-2015, 06:40 PM #11
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Can smb explain this question?
Just a quick note: that second snippet doesn't compile since you were a little too eager when removing 'int' keywords ;)
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 01-29-2015, 06:47 PM #12
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Can smb explain this question?
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 01-29-2015, 06:56 PM #13
Member
- Join Date
- Jan 2015
- Posts
- 48
- Rep Power
- 0
Re: Can smb explain this question?
Thank you, Jim! Really helpful! One question: how many years of experience do you have with Java?
- 01-29-2015, 07:03 PM #14
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Can smb explain this question?
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Don't know how to explain this
By Jhaz in forum New To JavaReplies: 0Last Post: 08-16-2014, 01:45 PM -
Question dealing with a simple object (hard to explain)
By Lanuk in forum New To JavaReplies: 8Last Post: 01-10-2012, 05:40 AM -
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 2Last Post: 12-13-2010, 02:34 PM -
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 3Last Post: 12-13-2010, 08:22 AM -
Can somebody explain me this plz
By ccie007 in forum New To JavaReplies: 4Last Post: 05-20-2010, 08:47 PM
Bookmarks