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: 1773
Discussions: 188,401, Posts: 2,243,601, Members: 232,629
Old November 16th, 2004, 01:08 AM   Digg it!   #1 (permalink)
Member
 
Join Date: Nov 2004
Posts: 56
c++ for loop help

i'm writing a simple for loop for extra credit. it is supposed to print to the screen only the even numbers from 2 - 100. this is what i have and it is infinite looping. i'm using dev c++ version 4.0.

#include <iostream.h>
#include <stdlib.h>

int main()
{
int num;

for(num=2;num<=100;num+2)
{
cout << num <<"\n";
}

system("PAUSE");
return 0;
}
help is appreciated,
thanks
sirhc

boogieman063 is offline   Reply With Quote
Old November 16th, 2004, 01:20 AM     #2 (permalink)
Senior Member
 
TechKnickle's Avatar
 
Join Date: Aug 2004
Location: LA, California
Posts: 808
Send a message via AIM to TechKnickle Send a message via MSN to TechKnickle
Try this:

Code:
#include <iostream.h>
#include <stdlib.h>

int main()
{
int num;

for(num=2;num<=100;num+=2)
{
cout << num <<"\n";
}

system("PAUSE");
return 0;
}
Did you want it to loop?

Remember in c++ you have to put the += together if you are adding numbers together (proper syntax).
__________________
People are like coins, there's always two sides.


Last edited by TechKnickle : November 16th, 2004 at 01:24 AM.
TechKnickle is offline   Reply With Quote
Old November 16th, 2004, 02:21 AM     #3 (permalink)
Member
 
Join Date: Nov 2004
Posts: 56
no it was not supposed to loop, thanks that was perfect. let me ask you though, why does it have to be +=, it works, but how?

boogieman063 is offline   Reply With Quote
Old November 16th, 2004, 02:30 AM     #4 (permalink)
Senior Member
 
TechKnickle's Avatar
 
Join Date: Aug 2004
Location: LA, California
Posts: 808
Send a message via AIM to TechKnickle Send a message via MSN to TechKnickle
Yeah, sure no problem.

In c++, the '+' (unary) by itself effectively does nothing. Same concept with the '-', but this does something, it doesn't subtract though. It negates your statement (the value entered or computed earlier).

+= is an assignment operator, telling the computer "this needs to be done, so do it now".

Basically.
TechKnickle is offline   Reply With Quote
Old November 16th, 2004, 03:15 AM     #5 (permalink)
Ultimate Member
 
lost-and-found's Avatar
 
Join Date: Oct 2001
Location: Illinois
Posts: 2,959
Send a message via AIM to lost-and-found
basically what you had was a temporary statement: add 2 to num and continue. += says: add 2 to num and store the result as num.
__________________
lost-and-found is offline   Reply With Quote
Old November 17th, 2004, 02:20 PM     #6 (permalink)
Ultimate Member
 
elmers's Avatar
 
Join Date: Sep 2003
Location: Philadelphia
Posts: 1,462
"num+=2" means "num=num+2"
elmers 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
My first application, try and suggest.. TechKnickle Webmastering and Programming 24 October 1st, 2004 09:39 AM
Kinda cool web page ooops IMO Community 10 May 11th, 2004 05:27 PM
Excel Macro help jjst34 Applications and Operating Systems 15 October 3rd, 2003 12:10 PM
My lastest web site - opinions please... edwelly Webmastering and Programming 27 June 7th, 2003 08:52 AM
FLASH and audio? aznlurkerz Graphic Design and Digital Photography 7 June 29th, 2002 03:47 PM

Most Active Discussions
Is It Just Me? (2906)
Unarmed man on his stomach shot by .. (6)
Misery Loves Company... (2144)
New Build ( Finally ) (6)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
Folderchat Weekday thread (444)
Upgrading RAM (6)
Recent Discussions
Laptop proccesor to desktop mob.. (1)
What is the best external enclo.. (0)
Partition Magic 7.0 (Unallocate.. (17)
For cheap price and good qualit.. (1)
Left 4 Dead Small Freezes (3)
RCA 52Inch HDTV wont turn on (4)
wishin i could edit my aol prof.. (0)
Sporadic internet connectivity (2)
Assassins Creed PC Problems (40)
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 03:40 AM.
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