Results 1 to 6 of 6
- 04-30-2010, 01:07 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 4
- Rep Power
- 0
- 04-30-2010, 01:17 PM #2
- 04-30-2010, 01:21 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 4
- Rep Power
- 0
can you explain (if possible) how you converted it into the for loop am really confused and new to this
thanks
- 04-30-2010, 01:34 PM #4
easy, take the syntax of the for-loop
Java Code:for (initialization; termination; increment) { statement(s) }
and then replace the initialization, termination and increment according to your needs. your count was initialized with 0 so then initialization is 0, your while loop contain count < 5 so the termination is also i < 5 and the increment is 1 because your increment was count++
- 04-30-2010, 02:13 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 4
- Rep Power
- 0
Ohh i see thank you very much, this java is so complicated :)
- 04-30-2010, 05:05 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
That's close, and your explanation is correct, but I think this is a more accurate translation:
Actually, that is probably what the instructor wants, but it's not really an accurate translation, because the count and sum variables will not survive the loop, as they do in the while case. This code works the same as the while loop, but is ugly:Java Code:for (int count = 0, sum = 0; count < 5; count++) { sum += count; System.out.println(sum); }
-Gary-Java Code:int count, sum = 0; for (count = 0; count < 5; count++) { sum += count; System.out.println(sum); }Last edited by gcalvin; 04-30-2010 at 05:09 PM.
Similar Threads
-
for loop help
By soc86 in forum New To JavaReplies: 9Last Post: 01-24-2011, 09:45 PM -
do while loop?
By shroomiin in forum New To JavaReplies: 2Last Post: 11-13-2009, 10:32 AM -
For Loop
By YiBoog in forum New To JavaReplies: 6Last Post: 11-11-2009, 07:53 PM -
Loop Help!
By Keno777 in forum New To JavaReplies: 3Last Post: 11-04-2009, 01:11 AM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks