October 21st, 2007, 08:15 PM
|
#1 (permalink)
| | Member
Join Date: Mar 2005 Location: Atlanta, Georgia
Posts: 383
| Question about Java programming...
How would I convert this 'for' loop into a 'while' loop?
int sum=0;
for (int i=0; i<100; i++)
{
sum+=i;
}
Also, how would I convert it into a do-while loop?
__________________
Frank: Blue do you trust that I do not want to see you die here tonight?
Blue: Yes sir.
Frank: Blue you're my boy!
|
| |
October 21st, 2007, 08:27 PM
|
#2 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,549
|
Why would you want to? You're going to have to manually keep track of the i in either a while or a do-while loop.
Jkrohn
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
October 21st, 2007, 08:35 PM
|
#3 (permalink)
| | Member
Join Date: Mar 2005 Location: Atlanta, Georgia
Posts: 383
|
Actually I think I figured it out... do these look correct?
While loop:
int sum=0;
int i=0;
while (i<100){
sum+=i++;
}
Do-While:
int sum=0;
int i=0;
do{
sum+=i++;
}
while(i<100); |
| |
October 21st, 2007, 09:21 PM
|
#4 (permalink)
| | F@H shizzle.
Join Date: Sep 2004 Location: NY
Posts: 3,812
| Quote:
Originally Posted by jkrohn Why would you want to? You're going to have to manually keep track of the i in either a while or a do-while loop.
Jkrohn | This exactly. Unless you were instructed to change it by someone, I think for loop would work best here.
As for your code, look here: Code: While loop: //make While lower case
int sum=0;
int i=0;
while (i<100){
sum+=i++;
}
Do-While: //make this a comment
int sum=0;
int i=0;
do{
sum+=i++;
}
while(i<100); and btw, your code is just longer and not as efficient as a single for loop. This is exactly why loops are used. By the way, where's the end of your for loop? You want to i++ and then what else do you want to run with it? Or did you just forget a semi-colon?
__________________ Nobody made a greater mistake than he who did nothing because he could do only a little. - Edmund Burke |
| |
October 22nd, 2007, 03:14 AM
|
#5 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,549
| Quote:
Originally Posted by Ty44ler Actually I think I figured it out... do these look correct?
While loop:
int sum=0;
int i=0;
while (i<100){
sum+=i++;
}
Do-While:
int sum=0;
int i=0;
do{
sum+=i++;
}
while(i<100); | Those are quite wrong unfortunately.
How the for loop works.
1) Checks to see if I meets the condition
2) Adds I to sum
3) Increments I, back to 1
How your while loop works.
1) Checks to see if I meets condition
2) Increments I
3) Adds I to sum, back to 1
How your do-while works.
1) Increment I
2) Add I to sum
3) Check to see if I meets condition, back to 1
Also as you can see from above, the while checks *before* incrementing while the do-while checks *after* incrementing. As a result the while will not process I = 100 whereas the do-while will. Also the endpoints for these are off due to the incrementing of I before the addition.
I have a feeling this is schoolwork, no? |
| |
October 26th, 2007, 04:52 PM
|
#6 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,792
|
Yeah, CS 101, "Program Flows"
Sequential Statements
If Else Statements
For Loops
While Loops
Do While Loops
As JK mentions, a Do While loop will process it's contents AT LEAST once. |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |