The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
HeavyJ HeavyJ is offline
Registered User
  
 

Join Date: Oct 2009
Location: Toronto
Posts: 18
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);
Produces:
0
1
2
3
4
5
6
7
8
9
----------------------------------------------------
Code:
unsigned int X;

for ( X = 10; X-- > 0; ) printf("%u\n",X);
Produces:
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..
  #2 (permalink)  
Old 4 Weeks Ago
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
Not very strange. Your second example relates to a controlling expression which is always evaluated before each execution of the for loop. See ISO C99 6.8.5.3.

The statement for ( clause-1 ; expression-2 ; expression-3 ) statement
behaves as follows: The expression expression-2 is the controlling expression that is
evaluated before each execution of the loop body. The expression expression-3 is
evaluated as a void expression after each execution of the loop body. If clause-1 is a
declaration, the scope of any variables it declares is the remainder of the declaration and
the entire loop, including the other two expressions; it is reached in the order of execution
before the first evaluation of the controlling expression. If clause-1 is an expression, it is
evaluated as a void expression before the first evaluation of the controlling expression.
Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a
nonzero constant.
  #3 (permalink)  
Old 4 Weeks Ago
HeavyJ HeavyJ is offline
Registered User
  
 

Join Date: Oct 2009
Location: Toronto
Posts: 18
Thank you for the answer...

Is there any difference between these two loops?

for ( X = 0; X < 10; X++ ){
use X;
}

for ( X = 0; X < 10; ++X ){
use X;
}
  #4 (permalink)  
Old 4 Weeks Ago
jp2542a jp2542a is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 142
No.. expression-3 is something that is executed after the body of the for statement. Then its back to the expression-2 evaluation.
Reply

Bookmarks

Tags
c++, for loop, optimize

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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




All times are GMT -4. The time now is 12:56 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0