Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 08-07-2005, 01:07 AM   #1 (permalink)
 
Wizard Techie

Join Date: Mar 2004

Posts: 3,784

waynejkruse10

Default Easy C++ Question

Im sure this is very simple. I would like to loop something in C++.

eg.

Repeat
my code
Until x=50

so, i want to repeat my code until x = 50.

I dont want to use a while loop

Wayne
__________________
Core 2 Duo E6400, DFI Infinity 975X/G, 2x 512mb DDR2 667mhz, Albatron 7900gt, WD 200gb SATA, Samsung DVD-RW, Silverstone ST-50EF 500w PSU.
waynejkruse10 is offline  
Old 08-07-2005, 01:16 AM   #2 (permalink)
 
Super Techie

Join Date: Feb 2004

Posts: 416

chog

Send a message via AIM to chog
Default

for(x=0;x=50;x++)
{
insert code here
};<----dont remember if that needs a semi or not but put one just in case
chog is offline  
Old 08-07-2005, 01:31 AM   #3 (permalink)
 
Wizard Techie

Join Date: Mar 2004

Posts: 3,784

waynejkruse10

Default

thanks
__________________
Core 2 Duo E6400, DFI Infinity 975X/G, 2x 512mb DDR2 667mhz, Albatron 7900gt, WD 200gb SATA, Samsung DVD-RW, Silverstone ST-50EF 500w PSU.
waynejkruse10 is offline  
Old 08-07-2005, 02:02 AM   #4 (permalink)
 
Super Techie

Join Date: Feb 2004

Posts: 416

chog

Send a message via AIM to chog
Default

Wait, change the x=50 to x<=50, sorry about that it has been awhile since I have coded.
chog is offline  
Old 08-07-2005, 02:25 AM   #5 (permalink)
 
Wizard Techie

Join Date: Mar 2004

Posts: 3,784

waynejkruse10

Default

cool, thanks.

Could you explain the x=0 and x++ parts, what do they do?
__________________
Core 2 Duo E6400, DFI Infinity 975X/G, 2x 512mb DDR2 667mhz, Albatron 7900gt, WD 200gb SATA, Samsung DVD-RW, Silverstone ST-50EF 500w PSU.
waynejkruse10 is offline  
Old 08-07-2005, 10:57 AM   #6 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

Does x ever have an initial value in your code? Or does it always start at 0?

Something more closely approximating your code would look like:

do
{
// Your code
x++;
} (while x<50);

Som that variation runs the code *then* checks the size of x. The effect of this is creating a loop that you can always guarantee will run at least once.

x++ => Increment X by 1, shorthand for x = x + 1;
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 08-07-2005, 10:59 AM   #7 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

Quote:
Originally posted by chog
Wait, change the x=50 to x<=50, sorry about that it has been awhile since I have coded.
for(x=0;x<50;x++) is correct.

x<=50 would mean that it runs 51 times, since you are starting at 0.
x<50 runs 50 times.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 08-07-2005, 04:51 PM   #8 (permalink)
 
Wizard Techie

Join Date: Mar 2004

Posts: 3,784

waynejkruse10

Default

x is always dynamic, i want this code to run "forever"
__________________
Core 2 Duo E6400, DFI Infinity 975X/G, 2x 512mb DDR2 667mhz, Albatron 7900gt, WD 200gb SATA, Samsung DVD-RW, Silverstone ST-50EF 500w PSU.
waynejkruse10 is offline  
Old 08-08-2005, 10:26 AM   #9 (permalink)
 
Super Techie

Join Date: Feb 2005

Posts: 262

jinexile

Default

If you want something to run forever then use:

Code:
While(true)  //when is "true" never true?
{
   //code to do forever
   //I'd suggest testing for a key to stop
}
Want to do it in a for loop?
Code:
for(i=1; i<0; i=1)
{
   //put code here
   //suggest testing for input to exit loop
}
Its not as elegant as While(true) but it should work.
__________________
AMD Athlon 64 3000+ (Overclocked to 3300+)
ASUS K8V-Deluxe
1GB PC3200 DDR
2x200GB Seagate SATA RAID 0
BFG 6800GT OC w\\128MB Ram
2x 19\" Samsung 930B LCDs
jinexile is offline  
Old 08-08-2005, 11:45 AM   #10 (permalink)
Calzinger's Avatar
 
Beer

Join Date: Apr 2005

Location: New York

Posts: 565

Calzinger is on a distinguished road

Send a message via AIM to Calzinger
Default

@jinexile
Make sure to use a lowercase "while". It depends upon your compiler, but it is usually case sensitive. "true" will also have to be defined as 1, but this is also dependent upon your compiler. Devcpp recognizes true as 1 whereas Turbo C++ will return "true" as undefined. You can just set its type as int and define it as 1. To avoid that, you can simple just use while( 1 ).
__________________
Calzinger is offline  
 
Closed Thread

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
Trackbacks are On
Pingbacks are On
Refbacks are On