![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using variables created sequentially in a loop while still inside of the loop [bash] | DeCoTwc | Shell Programming and Scripting | 2 | 06-23-2009 04:59 PM |
| observation... | KevinADC | What's on Your Mind? | 9 | 02-13-2008 06:35 AM |
| Something strange... | B14speedfreak | SUN Solaris | 0 | 06-29-2006 10:48 AM |
| how to get the similar function in while loop or for loop | trynew | Shell Programming and Scripting | 3 | 06-17-2002 11:09 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
The C for() loop - A strange observation
Hello,
I am optimizing my low level C coding style. I have run into an strange C implementation fact: ---------------------------------------------------- Code:
unsigned int X;
for ( X = 0; X < 10; X++ ) printf("%u\n",X);
0 1 2 3 4 5 6 7 8 9 ---------------------------------------------------- Code:
unsigned int X;
for ( X = 10; X-- > 0; ) printf("%u\n",X);
9 8 7 6 5 4 3 2 1 0 --------------------------------------------------- In the first case, the X++ operation is completed after the body of the loop, so the initial value of X is run through the loop. In the second case, the X-- operation is completed before the body of the loop, so the initial value of X is skipped in the loop. Why? Is the final expression in the for() loop tied to the {} braces, whereas the loop condition is taking place distinctly before the {} braces? All the Best, Heavy J - Will the second loop really execute faster? or does compiler optimization take care of that problem even when the condition and initial value are not constants? Last edited by HeavyJ; 4 Weeks Ago at 08:14 PM.. |
![]() |
| Bookmarks |
| Tags |
| c++, for loop, optimize |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|