home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 3208
Discussions: 188,374, Posts: 2,243,434, Members: 232,602
Old October 21st, 2007, 08:15 PM   Digg it!   #1 (permalink)
Member
 
Ty44ler's Avatar
 
Join Date: Mar 2005
Location: Atlanta, Georgia
Posts: 383
Send a message via AIM to Ty44ler
Question
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!

Ty44ler is offline   Reply With Quote
Old October 21st, 2007, 08:27 PM     #2 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,549
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to 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
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.

jkrohn is offline   Reply With Quote
Old October 21st, 2007, 08:35 PM     #3 (permalink)
Member
 
Ty44ler's Avatar
 
Join Date: Mar 2005
Location: Atlanta, Georgia
Posts: 383
Send a message via AIM to 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);

Ty44ler is offline   Reply With Quote
Old October 21st, 2007, 09:21 PM     #4 (permalink)
F@H shizzle.
 
xelnanga's Avatar
 
Join Date: Sep 2004
Location: NY
Posts: 3,812
Blog Entries: 2
Send a message via AIM to xelnanga Send a message via MSN to xelnanga
Quote:
Originally Posted by jkrohn View Post
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
xelnanga is offline   Reply With Quote
Old October 22nd, 2007, 03:14 AM     #5 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,549
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
Quote:
Originally Posted by Ty44ler View Post
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?
jkrohn is offline   Reply With Quote
Old October 26th, 2007, 04:52 PM     #6 (permalink)
Ultimate Member
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 2,792
Send a message via AIM to Rootstonian
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.
Rootstonian is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
General programming question lost-and-found Webmastering and Programming 3 May 19th, 2007 09:21 AM
Java Programming carl33p Webmastering and Programming 5 October 24th, 2006 06:23 PM
just a quickie programming question: oliver w Webmastering and Programming 2 December 23rd, 2005 04:24 AM
java programming c44151 General Tech Discussion 1 June 27th, 2004 09:53 PM
Question about programming with TV card? carfield Multimedia and Audio 1 May 23rd, 2003 09:07 AM

Most Active Discussions
Is It Just Me? (2881)
The United States Debt (20)
Looks like Burris will get his Sena.. (7)
Upgrading RAM (5)
I think I just killed my computer w.. (24)
Folderchat Weekday thread (435)
Antec 300 bulk purchase? (11)
Worth the upgrade?? (14)
Titan quest and Immortal Throne, an.. (17)
Recent Discussions
Folderchat Weekday thread (436)
Help with an Ati Radeon HD 4850.. (22)
CPU wont boot (2)
Building first computer, will t.. (1)
get this error, "res://C:\.. (73)
Problem with boot and motherboa.. (0)
MS to offer free Windows 7 upgr.. (1)
System Security Virus (20)
no power ! (7)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 07:21 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28