Two Plus Two Newer Archives  

Go Back   Two Plus Two Newer Archives > Internet Gambling > Software
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 08-30-2006, 08:06 AM
Rounder 24/7 Rounder 24/7 is offline
Senior Member
 
Join Date: Sep 2003
Posts: 139
Default C++ do-while loop

Hi I've got a test on friday on C++ and Im trying to revise.

Obviously Im trying to rush through everything. Im having trouble understanding how to use do-while loops. I've managed to write a small program using the for loop displaying 8 times tables. But need to use the do while loop to do the same thing. Would anyone be able to help me with this?

#include <iostream>
using namespace std;

int main()

{

cout << "Eight times table" << endl;

int Ans;
int i;

for (i=1; i<11; i++)
{ Ans = (i*8);
cout << i<< " x 8 = " <<Ans << endl;
}

return 0;

}
Reply With Quote
  #2  
Old 08-30-2006, 10:14 AM
Dadswell Dadswell is offline
Senior Member
 
Join Date: Jun 2006
Location: Canada
Posts: 763
Default Re: C++ do-while loop

#include <iostream>
using namespace std;

int main()

{

cout << "Eight times table" << endl;

int Ans=0;
int i=1;

do
{
Ans = (i*8);
cout << i<< " x 8 = " <<Ans << endl;
i++;
}
while (i<11);
return 0;

}
Reply With Quote
  #3  
Old 08-30-2006, 11:00 AM
tigerite tigerite is offline
Senior Member
 
Join Date: Sep 2004
Posts: 9,815
Default Re: C++ do-while loop

The neater way would be to remove the i++ in the loop and put while (++i < 11); for complete similarity with the for loop - but yeah [img]/images/graemlins/smile.gif[/img]
Reply With Quote
  #4  
Old 08-30-2006, 11:09 AM
tigerite tigerite is offline
Senior Member
 
Join Date: Sep 2004
Posts: 9,815
Default Re: C++ do-while loop

Who deleted their post thinking I'd got ++i wrong and it should be i++, come on own up [img]/images/graemlins/tongue.gif[/img]
Reply With Quote
  #5  
Old 08-30-2006, 11:11 AM
PokerAce PokerAce is offline
Senior Member
 
Join Date: Jan 2005
Posts: 2,582
Default Re: C++ do-while loop

In C++, indexes usually start at 0 so you should try to remain consistant with that. Something like this:

<font class="small">Code:</font><hr /><pre>
#include &lt;iostream&gt;
using namespace std;

int main() {
cout &lt;&lt; "Eight times table" &lt;&lt; endl;

int idx = 0;
do {
cout &lt;&lt; idx+1 &lt;&lt; " x 8 = " &lt;&lt; ((idx+1) * 8) &lt;&lt; endl;
} while( ++idx &lt; 10 );

return (0);
}
</pre><hr />

This makes your code easier to understand at first glance by experienced coders. I'll say for the record that I despise do-while loops and do whatever is needed to avoid them.
Reply With Quote
  #6  
Old 08-30-2006, 12:10 PM
jamzfive jamzfive is offline
Senior Member
 
Join Date: Mar 2006
Location: PA, USA
Posts: 181
Default Re: C++ do-while loop

[ QUOTE ]
I'll say for the record that I despise do-while loops and do whatever is needed to avoid them.

[/ QUOTE ]

I'd go with the first solution offered. I think the understanding of what the loop is doing is obscured by artificially trying to use the "start at 0" concept. The variable represents a multiplicand, not an index, so just use it naturally. Also, it's more compact to do the in-line increment, but most professional guides I've read frown on having one line of code do two things. Increment separately from the multiplication.

For the record, do-while loops are perfect for *some* situations. I have a need for them about 0.5% of the time I'm using a loop, and in that 0.5% of the time, the resulting code is much cleaner and much more straightforward for having used it than trying to rig up a "for" or a "while" with all kinds of "first time through?" checks.

jb
Reply With Quote
  #7  
Old 08-30-2006, 12:52 PM
PokerAce PokerAce is offline
Senior Member
 
Join Date: Jan 2005
Posts: 2,582
Default Re: C++ do-while loop

The problem you have with starting at 1 is that you have to end at 11. At first glance, anyone familiar with C++ will assume that the loop is happening 11 times. Any time I see C++ code where someone starts a loop variable at 1, I assume the author is very inexperienced, as it's not good practice.

My thoughts on the do-while loop is that it can be done just as elegantly with a regular while loop. If I recall correctly, Bjarne Stroustrup (the guy who designed C++) agrees with me and didn't even want to include them in the design but did so as a compromise.

As for each line doing one thing, that's a bit silly. Of course, you don't want to have one line doing too many things, but it's unavoidable. Take the following code for instance:

<font class="small">Code:</font><hr /><pre>int num = idx+1;
int ans = num * 8;
cout &lt;&lt; num;
cout &lt;&lt; " x 8 = ";
cout &lt;&lt; ans;
cout &lt;&lt; endl;
</pre><hr />
There, each line does one thing, but it's hardly more readable than my original line of code.
Reply With Quote
  #8  
Old 08-30-2006, 06:26 PM
jamzfive jamzfive is offline
Senior Member
 
Join Date: Mar 2006
Location: PA, USA
Posts: 181
Default Re: C++ do-while loop

[ QUOTE ]
The problem you have with starting at 1 is that you have to end at 11. At first glance, anyone familiar with C++ will assume that the loop is happening 11 times. Any time I see C++ code where someone starts a loop variable at 1, I assume the author is very inexperienced, as it's not good practice.

[/ QUOTE ]

I would agree with you if "i" were just a counter. But it's not. The trouble I see in the loop setup is that "i" is a bad name for a variable that is *not* a counting-type loop control variable. It should be called "multiplier" or something like that, to make it clear that it's not keeping a straightforward count. What if the assignment were to print out the squares of the integers from 107 to 116?

My solution would be:
for (int root = 107; root &lt;= 116; i++)
{
cout &lt;&lt; root &lt;&lt; ": " &lt;&lt; root * root &lt;&lt; endl;
}

Not:
for (int root = 0; root &lt; 11; i++)
{
cout &lt;&lt; (root + 107) &lt;&lt; ": " &lt;&lt; (root + 107) * (root + 107) &lt;&lt; endl;
}

The distinction is that the original solution started at 1 because of the specific limits for the stated problem.

[ QUOTE ]
My thoughts on the do-while loop is that it can be done just as elegantly with a regular while loop. If I recall correctly, Bjarne Stroustrup (the guy who designed C++) agrees with me and didn't even want to include them in the design but did so as a compromise.

[/ QUOTE ]

I use do-while when I know the contents of the loop need to execute at least once (helps solve all types of "fencepost" issues), and while/for loops everywhere else.

Either way, the solution to *this* problem is certainly not better using a do-while.


[ QUOTE ]
As for each line doing one thing, that's a bit silly. Of course, you don't want to have one line doing too many things, but it's unavoidable.

[/ QUOTE ]

Absolutely. In my haste this morning I didn't say quite what I meant. It's poor practice to include in a line of code expressions that have side effects. Thus, incrementing a variable should take place on a different line than the one in which the value from that variable is used. It probably makes no difference in the compiled binary, but is more maintainable.

PS - What geeks we are

PPS - Love PaHUD. Keep up the good work, no matter what your code looks like.
Reply With Quote
  #9  
Old 08-30-2006, 11:59 PM
Mogobu The Fool Mogobu The Fool is offline
Senior Member
 
Join Date: Sep 2005
Location: On teh internets.
Posts: 617
Default Re: C++ do-while loop

I agree with jamzfive here. The loop indexer is also used in the loop to iterate the tables, so it's appropriate to keep the code generating the iterations as clean as possible; that's the most important part to understand.

I also agree with incrementing the indexer inside the do loop; it's bad practice to make a logical test with a side effect unless there's a pressing need. If it were in a line of procedural code I wouldn't mind, but it can throw people off if it's inside a logical test. Besides, they compile down the same, anyway.

Lastly, if you find the "&lt; 11" notation disturbing, you can always represent it as "&lt;= 10". Again, they compile down the same, but one is clearer to the reader.

I also like the idea of keeping the three equivalent parts of the For statement in seperate lines: the initializer before the Do While; the logical test at the end; the incrementer inside the loop; the three elements of the For repeated visibly in the Do While.

Personally, I prefer to start the indexer at 10 and count down, stopping when the index = 0. Strictly speaking, this is faster - comparison to zero is faster than comparison to a number loaded into another register.

If the indexer were NOT used in the loop, most modern compilers would actually compile it down to do just that - count down to zero. But if the indexer is used INSIDE the loop, the compiler can't do this, because it will change the behavior of the loop.
Reply With Quote
  #10  
Old 08-30-2006, 11:59 PM
PokerAce PokerAce is offline
Senior Member
 
Join Date: Jan 2005
Posts: 2,582
Default Re: C++ do-while loop

This is one of those agree to disagree moments (and I have a a bit too much alcohol in my blood to come up with valid counterarguments right now).

BTW, the PA Hud code is generally clean and proper with a few shady spots. I take pride in my code. [img]/images/graemlins/smile.gif[/img]
Reply With Quote
Reply


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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:40 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.