![]() |
|
#1
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
||||
|
||||
Re: converting a "do while loop" into a "For loop"CPP / C++ / C Code:
Is equivalent to CPP / C++ / C Code:
is equivalent to CPP / C++ / C Code:
__________________
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
|
|||
|
|||
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
|
||||
|
||||
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.
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
|
|||
|
|||
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
|
|||
|
|||
Re: converting a "do while loop" into a "For loop"Quote:
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:
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:
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
|
|||
|
|||
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:
Last edited by cable_guy_67 : 21-Apr-2006 at 07:32.
Reason: Please enclose c code in [c] ... [/c] tags
|
|
#10
|
||||
|
||||
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:
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 GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The