I have run into an strange C implementation fact:
----------------------------------------------------
Produces:
0
1
2
3
4
5
6
7
8
9
----------------------------------------------------
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?
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.
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)
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)
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)
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)
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)
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)
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)
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)