473,387 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

for loop stop condition

Hi,
In the loop

for(i=0; i< h+1; i++);

if h=10 say, then is h+1 evaluated every time or, between iterations,
does the for loop remember that the condition i<11 is being applied?
I'm guessing that it evaluates every time as it is possible that h may
have been changed by the body of the loop.

Douglas
Nov 14 '05 #1
3 2509
"Douglas" <mm***@yahoo.co.uk> wrote in message
news:4c**************************@posting.google.c om...
Hi,
In the loop

for(i=0; i< h+1; i++);

if h=10 say, then is h+1 evaluated every time or, between iterations,
does the for loop remember that the condition i<11 is being applied?
I'm guessing that it evaluates every time as it is possible that h may
have been changed by the body of the loop.

Douglas


The condition is fully evaluated every time. If you
want h+1 evaluated only once, then do that before
entering the loop and save it in a variable, then
use that variable for the condition.
Nov 14 '05 #2
xarax <xa***@email.com> wrote:
"Douglas" <mm***@yahoo.co.uk> wrote in message
news:4c**************************@posting.google.c om...
Hi,
In the loop

for(i=0; i< h+1; i++);

if h=10 say, then is h+1 evaluated every time or, between iterations,
does the for loop remember that the condition i<11 is being applied?
I'm guessing that it evaluates every time as it is possible that h may
have been changed by the body of the loop.
The condition is fully evaluated every time. If you
want h+1 evaluated only once, then do that before
entering the loop and save it in a variable, then
use that variable for the condition.


Can't a clever optimizing compiler determine, at least under some
circumstances (e.g. h ist never changed directly and isn't defined
as volatile and there are no changes done via pointers), that h can
not change it's value within the loop and than compare to 11 instead
of evaluation h+1 each time round?

At least a short look at the assembler produced by gcc for e.g.

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
int i, h;
h = 10;

for ( i = 0; i < h + 1; i++ )
printf( "%d\n", i );

return EXIT_SUCCESS;
}

looks a lot as if the comparison is done against the number 11 and not
the result of the calculation of h+1.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3
On 10 Jul 2004 10:23:21 -0700, mm***@yahoo.co.uk (Douglas) wrote in
comp.lang.c:
Hi,
In the loop

for(i=0; i< h+1; i++);

if h=10 say, then is h+1 evaluated every time or, between iterations,
does the for loop remember that the condition i<11 is being applied?
I'm guessing that it evaluates every time as it is possible that h may
have been changed by the body of the loop.

Douglas


The C abstract machine requires evaluation of the condition each time,
but the "as-if" rule allows it to be optimized away if the compiler
can analyze the code and determine that it is not modified in the loop
body.

This is called "loop invariant code hoisting", and is a very common
optimization technique used by compilers.

Consider:

void blank_lines(int x)
{
int i;
for (i = 0; i < x+1; ++i)
{
putchar('\n');
}
}

Since x is automatic, is not defined as volatile, is not modified
within the loop, and its address is not passed to a function, the
compiler can tell that it cannot be modified within the loop, so it
can optimize the calculation away.

On the other hand, given:

void stranger_func(int *);

void strange_func(int x)
{
int i;
for (i = 0; i < x+1; ++i)
{
stranger_func(&x);
}
}

Here, especially if the definition of stranger_func() is another
source file, the compiler cannot assume that the value of x remains
constant throughout the loop, and must calculate x+1 each time.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Mark Anderson | last post by:
A 'for' loop takes 3 arguments (initialize; test; increment). The 'test' must equate as true or false This doesn't work... x = 5; for (y=1; (y==5); y+=1) { alert(x * y); } ...nor does... x...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
2
by: Rvo | last post by:
I have written an application that manipulates files, while doing so the program is running in a loop and any actions on the GUI will not get any response. When I try to stop the processing of the...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
8
by: Jim Langston | last post by:
There's the thing about iterating though a map or vector when you may delete one of the elements, where you simply assign the iterator to the map.erase() statement or increment it if you don't. ...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
8
by: mtsweep | last post by:
Hi, I started a background thread to preform some time intensive tasks for a GUI frontend. This background thread uses a C++ object which requires a windows message loop so I started one in it...
3
by: normadiah | last post by:
Dear all, I'm really appreciated if anyone can help me. I would like to ask regarding the following code... B]System.out.println("Enter Pre_condition or q to quit):"); ...
33
by: dmoran21 | last post by:
Hi all, I am a mathematician and I'm trying to write a program to try out a formula that I've derived. However, it seems that I've got an infinite loop and I don't quite understand why. I was...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.