Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 12-16-07   #11 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,119

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 284
Trader Rating: 1
Default

Quote:
Originally Posted by Licht View Post
Code:
for(int i=0; i<6; i++){
    //do something
}
I wanna focus on this, i have a few questions. What exactly is the syntax for this?

Code:
for(first argument; 2nd argument; 3rd argument)
What exactly gos in for each argument? Is it always used to initiate a variable, run a check, then perform a mathematical change to the variable?
The syntax of a for loop is fairly simple, it is:

for(executed before; conditional statement; run after each iteration)

so something like:

Code:
for(int i = 0; i < 10; i++){
   cout << i << endl;
}
this means the int i = 0; is run before the loop starts. This is usually an intializer for the counting variable. The middle statement is checked before every iteration. If the condition is not met (i.e. if i is greater than or equal to 10) then the loop will end. Lastly, the final one is what is run after every iteration. This is usually an incrementer of your counting variable.

The for loop is not actually a function like it looks. Its (I believe) a macro that gets turned into a while loop behind the scenes.

Ironically, you can actually do:

Code:
for(;;){
}
and it is correct. this will just run an infinite loop. there is no conditional statement to break it out of.


Edit: Bah, to slow on the stick. Nice job Polska.
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Old 12-16-07   #12 (permalink)
Miscelaneous
 
Licht's Avatar
 
amd ati

Join Date: Mar 2007
Location: Bel Air
Posts: 9,933
Blog Entries: 3

Rep: 289 Licht is a proven memberLicht is a proven memberLicht is a proven member
Unique Rep: 188
Folding Team Rank: 904
Trader Rating: 0
Default

Quote:
Originally Posted by Polska View Post
The general Syntax would be:

syntax:
for (statement1; expression; statement2)
statement[s]3

curly brackets used to nest several statements
for (statement1; expression; statement2) {
statement3;
statement4;
statement5;
}

statement2 doesn't necessarily have to correlate to statement1, but generally it is. You could however write

for (int i = 0; i < 6; ) {
Console.WriteLine("hi");
i++;
}

and it is equivalent to;

for (int i = 0; i < 6; i++) {
Console.WriteLine("hi");
}
Ok so the arguments for the for loop are

Code:
for(any statement;expression determining when to break without being called;any statement)
?

System: Uzicht #4
CPU
Athlon X2 5200+ Windsor
Motherboard
Gigabyte 790X AM2+
Memory
6GB Kingston DDR2 667MHZ
Graphics Card
Saphire HD 3870
Hard Drive
4x WD1600AAJS RAID0
Sound Card
X-Fi Extreme Gamer Fatality Professional
Power Supply
OCZ Game-X-Stream 700w
Case
NZXT Black Steel
CPU cooling
Xigmatec Rifle
GPU cooling
Stock Saphire 3870 Cooling
OS
Vista Home Premium x64 SP1
Monitor
Samsung SyncMaster 19"Wide
Licht is online now I fold for Overclock.net Overclocked Account Licht's Gallery   Reply With Quote
Old 12-16-07   #13 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,119

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 284
Trader Rating: 1
Default

Quote:
Originally Posted by Licht View Post
Ok so the arguments for the for loop are

Code:
for(any statement;expression determining when to break without being called;any statement)
?
Correct. Thought the first "any statement" will only be run before the loop starts, and the second will be run after each iteration.
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Old 12-16-07   #14 (permalink)
Miscelaneous
 
Licht's Avatar
 
amd ati

Join Date: Mar 2007
Location: Bel Air
Posts: 9,933
Blog Entries: 3

Rep: 289 Licht is a proven memberLicht is a proven memberLicht is a proven member
Unique Rep: 188
Folding Team Rank: 904
Trader Rating: 0
Default

Quote:
Originally Posted by kdbolt70 View Post
Correct. Thought the first "any statement" will only be run before the loop starts, and the second will be run after each iteration.
Ok so,

Code:
for(run at start; continue if true; run every cycle)
Right?

And

Code:
<variable>++
Means

Code:
<variable> += 1

System: Uzicht #4
CPU
Athlon X2 5200+ Windsor
Motherboard
Gigabyte 790X AM2+
Memory
6GB Kingston DDR2 667MHZ
Graphics Card
Saphire HD 3870
Hard Drive
4x WD1600AAJS RAID0
Sound Card
X-Fi Extreme Gamer Fatality Professional
Power Supply
OCZ Game-X-Stream 700w
Case
NZXT Black Steel
CPU cooling
Xigmatec Rifle
GPU cooling
Stock Saphire 3870 Cooling
OS
Vista Home Premium x64 SP1
Monitor
Samsung SyncMaster 19"Wide

Last edited by Licht : 12-16-07 at 09:15 PM.
Licht is online now I fold for Overclock.net Overclocked Account Licht's Gallery   Reply With Quote
Old 12-16-07   #15 (permalink)
Programmer
 
Polska's Avatar
 
intel nvidia

Join Date: Oct 2007
Location: Toronto, Ontario, canada
Posts: 188

Rep: 16 Polska Unknown
Unique Rep: 15
Trader Rating: 0
Default

Here is a more formal definition from a book I have.

Quote:
for(initial; boolean-expression; loop)
statement
  • The initial expression performs operation that precede the first iteration. The initial expression does not have to be a declaration of a new variable. It is possible, for example, to declare the variable beforehand and simply initialize it in the for loop. Variables declared here, however are bound within the scope of the for statement.
  • The boolean-expression portion of the for loop specifies an end condition. The for loop will repeat only as long as boolean-expression evaluates to true.
  • The loop expression executes after each iteration.
  • The statement portion of the for loop is the code that executes while the conditional expression remains true.
__________________
Quote:
Originally Posted by roningai
Never,eva go AS5 to mouth....
GPU-Z Validation
CPU-Z Validation

System: Big Polish Sausage
CPU
E6850 3.4Ghz @ 1.4v
Motherboard
Asus P5KC
Memory
Corsair Dual Channel TWINX 4096MB PC6400 DDR2 800
Graphics Card
EVGA 8800 GTS 640MB @ 650/1000
Hard Drive
WD 150Gb Raptor, Seagate 500Gb
Sound Card
Turtle Beach - Montego DDL
Power Supply
Ultra 700W
Case
Ultra Grid ATX Case
CPU cooling
zalman 9700
GPU cooling
stock
OS
Vista Ultimate SP1 (32 bit)
Monitor
Samsung 226BW
Polska is offline   Reply With Quote
Old 12-16-07   #16 (permalink)
Miscelaneous
 
Licht's Avatar
 
amd ati

Join Date: Mar 2007
Location: Bel Air
Posts: 9,933
Blog Entries: 3

Rep: 289 Licht is a proven memberLicht is a proven memberLicht is a proven member
Unique Rep: 188
Folding Team Rank: 904
Trader Rating: 0
Default

Quote:
Originally Posted by Polska View Post
Here is a more formal definition from a book I have.
Handy.

System: Uzicht #4
CPU
Athlon X2 5200+ Windsor
Motherboard
Gigabyte 790X AM2+
Memory
6GB Kingston DDR2 667MHZ
Graphics Card
Saphire HD 3870
Hard Drive
4x WD1600AAJS RAID0
Sound Card
X-Fi Extreme Gamer Fatality Professional
Power Supply
OCZ Game-X-Stream 700w
Case
NZXT Black Steel
CPU cooling
Xigmatec Rifle
GPU cooling
Stock Saphire 3870 Cooling
OS
Vista Home Premium x64 SP1
Monitor
Samsung SyncMaster 19"Wide
Licht is online now I fold for Overclock.net Overclocked Account Licht's Gallery   Reply With Quote
Old 12-16-07   #17 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,119

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 284
Trader Rating: 1
Default

Quote:
Originally Posted by Licht View Post
Ok so,

Code:
for(run at start; continue if true; run every cycle)
Right?

And

Code:
<variable>++
Means

Code:
<variable> += 1
Nail on the head my friend.
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Reply



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



All times are GMT -4. The time now is 03:53 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.33091 seconds with 8 queries