The C for() loop - A strange observation


 
Thread Tools Search this Thread
Top Forums Programming The C for() loop - A strange observation
# 1  
Old 10-19-2009
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; 10-19-2009 at 09:14 PM..
# 2  
Old 10-20-2009
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  
Old 10-20-2009
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  
Old 10-20-2009
No.. expression-3 is something that is executed after the body of the for statement. Then its back to the expression-2 evaluation.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

2. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

3. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

4. Shell Programming and Scripting

Strange exit of while loop

This code is used to check for duplicate ip and hostnames in an /etc/hosts file CENTRAL is path to /etc/hosts AWK =awk #check CENTRAL for duplicate ips or hostnames# grep -v "^#" $CENTRAL | $AWK '{ print $1, $2; }' | \ while read ip hostname do if... (5 Replies)
Discussion started by: trimike
5 Replies

5. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

6. Shell Programming and Scripting

Null Handling in Until loop. . .loop won't stop

Hi Im running this script, which is supposed to find the max value build some tables and then stop running once all the tables are built. Thing is , it keeps assigning a null value to $h and then $g is null so it keep building tables i.e. testupdateNUL. How can I stop this? Here is what I have: ... (4 Replies)
Discussion started by: brandono66
4 Replies

7. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. What is on Your Mind?

observation...

That is one thing I find unusual about this forum, people post a list of program requirements and many people are happy to post complete solutions even though the person asking has shown no effort at all to solve the problem. Most other forums ( or the forum members anyway ) will not do that.... (9 Replies)
Discussion started by: KevinADC
9 Replies

9. Solaris

Something strange...

Hi all, Thanks for any replies and for reading in advance. We have upgraded one of our database instances to 10g on a Solaris 8 box, anyhow the other day it started trying to ping loads of weird IP addresses that we don't use, since our systems all run on pretty similar IP's. It all behind... (0 Replies)
Discussion started by: B14speedfreak
0 Replies
Login or Register to Ask a Question