Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2005, 1:25 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
I don't get it

If I write this code(which outputs the numbers 1 to 10 and each number's square) it runs fine:

int counter = 1;

do
{
cout << counter << "\t" << counter * counter << endl;
}while(++counter <= 10)

As I said above it outputs 1 to 10 and each numbers square. But if I change the way I increment the control variable to counter++ so it looks like this:

int counter = 1;

do
{
cout << counter << "\t" << counter * counter << endl;
}while(counter++ <= 10)

It actually outputs all numbers plus and including 11 and it's square. I don't understand why that is. It seems to me that it shouldn't matter because either way when 10 gets incremented to 11 it should cause the test condition to fail, meaning the last number output should still be 10.

Please explain what's goin on here. I'm just a beginner(again).
357mag is offline   Reply With Quote
Old Apr 26th, 2005, 1:35 AM   #2
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
because when you put the ++ afterwards it looks at the last 10, says ok, we're still good for the looping condition, THEN it increments the variable and does the loop again. it doesn't TEST the 11 until it comes back around. this is a valuable lesson indeed and would be a great post for all beginners to read. thanks. :-)
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Apr 26th, 2005, 11:39 AM   #3
STLFBr1
Newbie
 
Join Date: Apr 2005
Posts: 15
Rep Power: 0 STLFBr1 is on a distinguished road
Thats a great question and I found it tested in two consecutive programming classes in college. Like ninja said with the ++ after the variable it does everything else in the line (including the test for the while loop) then increments but if its before the variable, then the increment is the first thing done in the line. Sorry about being repetitive but I wanted to emphasize the fact that professors love testing on this guy.
STLFBr1 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:44 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC