GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Apr-2006, 21:35
nosman nosman is offline
New Member
 
Join Date: Apr 2006
Location: sydney
Posts: 12
nosman is on a distinguished road
Smile

converting a "do while loop" into a "For loop"


Hey guys, I'm doing a c++ assignment @ uni. In my assignment i have used a "do while loop" to perform a particular task. After showing my assignment to my prac teacher he said that though it did the job, my style looked quite messy and that that i would loose marks when my assignment was marked for style. He recommended to me to use a "For loop" instead of the "do while loop" as it would look a lot more neat. Anyway so i guess what i'm asking here is if someone is able to help me convert what i have done in using a do while loop into a for loop.

I don't want to post what i have done on here as i am afraid of someone who is also doing the same assignment of copying what i done. if your interested in helping me out i can email it to you, private mail it or if u know a better way of how i can get it to you just let me know.

my email is: nos_osman[at]hotmail[dot]com

Thanks,
Last edited by LuciWiz : 20-Apr-2006 at 09:05. Reason: Added some spam-bot protection (edited verbatim email address)
  #2  
Old 19-Apr-2006, 22:17
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: converting a "do while loop" into a "For loop"


You can send me a private message, but I make no guarantees of any solution or a timely response.
  #3  
Old 20-Apr-2006, 00:38
nosman nosman is offline
New Member
 
Join Date: Apr 2006
Location: sydney
Posts: 12
nosman is on a distinguished road

Re: converting a "do while loop" into a "For loop"


Hey Sokar

Thanks, I appreciate you taking the time to help me out.

I was trying to send u a private message, but I got message telling me that I can't private message you because I don't have sufficient privileges.
What should I do? Is there another way that I can send u my stuff
  #4  
Old 20-Apr-2006, 08:39
TreyAU21's Avatar
TreyAU21 TreyAU21 is offline
Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 116
TreyAU21 has a spectacular aura aboutTreyAU21 has a spectacular aura about

Re: converting a "do while loop" into a "For loop"


CPP / C++ / C Code:
int i=0;

while(i<10) {
 //blah blah blah
 i++;
}

Is equivalent to

CPP / C++ / C Code:
int i;

for(i=0; i<10; i++) {
  //blah blah blah
}

is equivalent to

CPP / C++ / C Code:
int i=0;

do {
  //blah blah blah
  i++;
} while(i<10);
__________________
If practice makes perfect and nobody's perfect... why practice?

Homepage: http://www.treywhite.com
Blog: http://www.treywhite.com/blog.php
Web Design Company: http://www.ewebproductions.com
  #5  
Old 20-Apr-2006, 10:53
nosman nosman is offline
New Member
 
Join Date: Apr 2006
Location: sydney
Posts: 12
nosman is on a distinguished road

Re: converting a "do while loop" into a "For loop"


Thanks, TreyAU21

Its the "blah blah blah" part that I am having difficulties with, being new to c++ i just don't know what goes where.

nosman
  #6  
Old 20-Apr-2006, 11:47
TreyAU21's Avatar
TreyAU21 TreyAU21 is offline
Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 116
TreyAU21 has a spectacular aura aboutTreyAU21 has a spectacular aura about

Re: converting a "do while loop" into a "For loop"


Well, as long as you get the loop to go through the same number of iterations in the for loop style as the while loop, the "blah blah blah" should remain the same. If you don't want to reveal your code, give us an example similar to one you are trying to convert in both a do-while and for loop. Then if you have any questions or concerns, we'll try to help as best we can.

Trey

On a side note... there was another Aussie on here referring to a 'uni'. I'm guessing that's short for university? Anywho... do you have any "shrimp on a bar-b" cooking? Speaking of which... Outback sure sounds good... I might have it for supper tonight!
__________________
If practice makes perfect and nobody's perfect... why practice?

Homepage: http://www.treywhite.com
Blog: http://www.treywhite.com/blog.php
Web Design Company: http://www.ewebproductions.com
  #7  
Old 20-Apr-2006, 11:57
nosman nosman is offline
New Member
 
Join Date: Apr 2006
Location: sydney
Posts: 12
nosman is on a distinguished road

Re: converting a "do while loop" into a "For loop"


Yer, us aussies sure love our slang language that's for sure.

int q=0;

do {
if(n%2==0)
n=n/2;

else
n=3*n+1;
q++;
}
while (n!=1);
return q;

if ur able to help me convert this could you email me
nos_osman@hotmail.com
  #8  
Old 20-Apr-2006, 17:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: converting a "do while loop" into a "For loop"


Quote:
Originally Posted by nosman
CPP / C++ / C Code:
    int q=0;
     
       do {
           if(n%2==0)
              n=n/2;
         
           else
               n=3*n+1;
               q++;
          } 
          while (n!=1);
          return q;

I would prefer a style that uses braces for all conditionals and loops. And indentation consistent with control structures. And maybe a little whitespace in expressions. One such style is

CPP / C++ / C Code:
int func(int n)
{
    int q = 0;
     
    do {
        if(n % 2==0) {
            n = n / 2;
        }
        else {
            n = 3 * n + 1;
        }
        q++;
    } while (n != 1);

    return q;
}

Now, as to whether a "for" loop would be more appropriate, the first thing I would ask is, "Are you sure that the code you posted meets all functional requirements of the assignment?" What is the answer supposed to be for n = 1? n = 2? n = 0? n = -1? etc.



If your code is exactly correct, then, I claim, there is no way to implement the exact functionality with a "for" loop that "looks more neat". Now, since "neat" is a subjective term, and I am the (self-appointed) judge, no one can prove me wrong. Since your instructor controls your destiny, you can't exactly argue with him/her (and I won't).

So, you have to find out (maybe from class examples or whatever...) what your instructor thinks looks "neat".

Here's your code in pseudo-code form:

Code:
1. set a counter, q, to zero 2. go through the following loop at least once: if n is even, then set n equal to n / 2. else set n equal to 3*n + 1 increment q if n is not equal to 1, then go back through the loop again. if n is equal to 1 then go to step 3 3. Return the value of q

I personally don't immediately see how this lends itself more appropriately to a "for" loop that "looks neater". I can make a "for" loop that is more obfuscatory; some traditionally-trained C programmers might say, "Neat!" Maybe that counts?

Regards,

Dave
  #9  
Old 21-Apr-2006, 02:47
nosman nosman is offline
New Member
 
Join Date: Apr 2006
Location: sydney
Posts: 12
nosman is on a distinguished road

Re: converting a "do while loop" into a "For loop"


ok, what do u guys think is better way to go about wat I have done.
CPP / C++ / C Code:
int q=0;

do {
if(n%2==0)
n=n/2;

else
n=3*n+1;
q++;
} 
while (n!=1);
return q;
Last edited by cable_guy_67 : 21-Apr-2006 at 07:32. Reason: Please enclose c code in [c] ... [/c] tags
  #10  
Old 21-Apr-2006, 09:45
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 995
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: converting a "do while loop" into a "For loop"


I agree with davekw7x about "looks", heck I'll have to tell you sometime about one of my professor's 50%-off, and 100%-off "clubs" for coding aspects that he just didn't want to see. Which by the way, a part of your code hits his 50%-off condition...

Basically, the "blah, blah" portion is the body of the loop, which in your case IS the 'if' and 'else' conditions and their statements.

All you need to understand is the syntax differences (and purposes) of the loops:
the 'for' loop has
Code:
for ( init; continuation-condition; next ) { YOUR "blah-blah" statement(s) go here }
so, the condition of your while-loop, n != 1, belongs in the "continuation-condition."
the 'init' could be your: q = 0. In C++ the whole 'int q = 0' can go in the init portion.
the 'next' could be your q++.

If 'n' happens to initially be 1, the loop will not run. Also, be careful, since the 'condition' is not checking 'q' there is a potential to have an infinite loop here if 'n' never resolves to 1.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not 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
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:47.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.