473,394 Members | 1,797 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,394 software developers and data experts.

Looping Choice

In

http://fms.komkon.org/EMUL8/HOWTO.html

I read:

"After initial values are assigned, we start the main loop:

for(;;)
{

Note that this loop can also be implemented as

while(CPUIsRunning)
{

where CPUIsRunning is a boolean variable. This has certain advantages,
as you can terminate the loop at any moment by setting CPUIsRunning=0.
Unfortunately, checking this variable on every pass takes quite a lot of
CPU time, and should be avoided if possible. Also, do not implement this
loop as

while(1)
{

because in this case, some compilers will generate code checking
whether 1 is true or not. You certainly don't want the compiler to do
this unnecessary work on every pass of a loop."

I would like to read your opinions on these statements :)

Sep 30 '08 #1
12 1527
Lorenzo Villari said:
In

http://fms.komkon.org/EMUL8/HOWTO.html

I read:

"After initial values are assigned, we start the main loop:

for(;;)
{

Note that this loop can also be implemented as

while(CPUIsRunning)
{

where CPUIsRunning is a boolean variable. This has certain advantages,
as you can terminate the loop at any moment by setting CPUIsRunning=0.
Yes. But it's still a lousy name. LoopIsRunning would be better.
Unfortunately, checking this variable on every pass takes quite a lot of
CPU time, and should be avoided if possible.
Nonsense. If there's a lot happening in the loop, checking one int is
epsilon effort. When you're deciding how to kick a football, do you take
into account the path-distorting effect of the gravitational force exerted
by your team-mates? And if there's *not* a lot happening in the loop, then
you can spare a clock or two to test an int, right?

Also, do not implement this
loop as

while(1)
{

because in this case, some compilers will generate code checking
whether 1 is true or not.
No, that's not the reason. The reason not to use it is that it can generate
a completely pointless and annoying warning, which the equivalent
construct for(;;) avoids. If you really want an endless loop, for(;;) is
exactly what you want - and if you want the loop to finish some time, you
should say so in your loop control by inserting a conditional expression.
You certainly don't want the compiler to do
this unnecessary work on every pass of a loop."

I would like to read your opinions on these statements :)
My opinion is that those statements are of no value.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 30 '08 #2
Lorenzo Villari wrote:
In

http://fms.komkon.org/EMUL8/HOWTO.html

I read:

"After initial values are assigned, we start the main loop:

for(;;)
{

Note that this loop can also be implemented as

while(CPUIsRunning)
{

where CPUIsRunning is a boolean variable. This has certain advantages,
as you can terminate the loop at any moment by setting CPUIsRunning=0.
Unfortunately, checking this variable on every pass takes quite a lot of
CPU time, and should be avoided if possible. Also, do not implement this
loop as

while(1)
{

because in this case, some compilers will generate code checking
whether 1 is true or not. You certainly don't want the compiler to do
this unnecessary work on every pass of a loop."

I would like to read your opinions on these statements :)
It's nonsense. The overhead of checking CPUIsRunning should be quite
negligible. The overhead of checking 1 should be non-existent with any
decent compiler.

I'm not a fan of "forever loops". One of the first things I want to know
about a loop is how it terminates. A "forever loop" gives the
impression, usually falsely, that it never terminates. For that reason,
I believe that you should always move at least one of the conditions
that actually causes loop termination into the loop condition itself;
preferably one of the most common such conditions. It might make your
code prohibitively complicated to do so, but in my experience that's
usually not the case.

I don't like using boolean variables for this purpose unless the
variable's value is read at least 2-3 time for each time that it is set.
Sep 30 '08 #3
James Kuyper said:

<snip>
I'm not a fan of "forever loops". One of the first things I want to know
about a loop is how it terminates. A "forever loop" gives the
impression, usually falsely, that it never terminates.
How refreshing to find someone talking sense about loops for a change!

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 30 '08 #4
Lorenzo Villari wrote:
In
http://fms.komkon.org/EMUL8/HOWTO.html
I read:
"After initial values are assigned, we start the main loop:
for(;;)
{
Note that this loop can also be implemented as
while(CPUIsRunning)
{
where CPUIsRunning is a boolean variable. This has certain advantages,
as you can terminate the loop at any moment by setting CPUIsRunning=0.
Unfortunately, checking this variable on every pass takes quite a lot of
CPU time,
That is nonsense.
and should be avoided if possible. Also, do not implement this
loop as

while(1)
{

because in this case, some compilers will generate code checking
whether 1 is true or not. You certainly don't want the compiler to do
this unnecessary work on every pass of a loop."
That is nonsense. It is even sillier than that which came before.
I would like to read your opinions on these statements :)
Try using a reputable source in the future. The claims that you quoted
are complete bullshit.
Sep 30 '08 #5

"Martin Ambuhl" <ma*****@earthlink.netha scritto nel messaggio
news:gb**********@registered.motzarella.org...
Try using a reputable source in the future. The claims that you quoted
are complete bullshit.
The reason why I quoted that was to let you say what I already thought but
was not 100 % sure about... I find your opionions to be authoritative. Thank
you all.
Sep 30 '08 #6
James Kuyper <ja*********@verizon.netwrote:
>
I'm not a fan of "forever loops". One of the first things I want to know
about a loop is how it terminates. A "forever loop" gives the
impression, usually falsely, that it never terminates. For that reason,
I believe that you should always move at least one of the conditions
that actually causes loop termination into the loop condition itself;
preferably one of the most common such conditions. It might make your
code prohibitively complicated to do so, but in my experience that's
usually not the case.
There's always the problem of needing to do work both before and after
the test, the "exit in the middle" loop. Until C adopts my generalized
do-while loop:

do /statement/ while ( /expression/ ) /statement-opt/ ;

we're pretty much stuck with a "forever loop" with a break in the middle
or replicating code (which is much worse in my opinion).
--
Larry Jones

But Mom, frogs are our FRIENDS! -- Calvin
Sep 30 '08 #7
la************@siemens.com wrote, On 30/09/08 20:41:
James Kuyper <ja*********@verizon.netwrote:
>I'm not a fan of "forever loops". One of the first things I want to know
about a loop is how it terminates. A "forever loop" gives the
impression, usually falsely, that it never terminates. For that reason,
I believe that you should always move at least one of the conditions
that actually causes loop termination into the loop condition itself;
preferably one of the most common such conditions. It might make your
code prohibitively complicated to do so, but in my experience that's
usually not the case.

There's always the problem of needing to do work both before and after
the test, the "exit in the middle" loop. Until C adopts my generalized
do-while loop:

do /statement/ while ( /expression/ ) /statement-opt/ ;

we're pretty much stuck with a "forever loop" with a break in the middle
or replicating code (which is much worse in my opinion).
Well, you could do the following which is even worse instead...

goto loop_start;
do {
/* code after condition */
loop_start:
/* code before condition */
} while (condition);

I've deliberately used a do-while loop instead of a while-do loop
because I know some people dislike do-while loops and I wanted this to
be horrible for as many people as possible :-)
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Sep 30 '08 #8
Flash Gordon wrote:
la************@siemens.com wrote, On 30/09/08 20:41:
.... snip ...
>
>There's always the problem of needing to do work both before
and after the test, the "exit in the middle" loop. Until C
adopts my generalized do-while loop:

do /statement/ while ( /expression/ ) /statement-opt/ ;

we're pretty much stuck with a "forever loop" with a break in
the middle or replicating code (which is much worse in my
opinion).

Well, you could do the following which is even worse instead...

goto loop_start;
do {
/* code after condition */
loop_start:
/* code before condition */
} while (condition);

I've deliberately used a do-while loop instead of a while-do
loop because I know some people dislike do-while loops and I
wanted this to be horrible for as many people as possible :-)
Well, we could still stick to legal C and have:

flag = false;
do {
if (flag) code_after_condition();
flag = true;
code_before_condition();
} while (condition);

or (something that looks like loop and a half code):

do {
code_before_condition();
if (!condition) break;
code_after_condition();
} while (1);

You can also use "for (;;)" or "while (1) {}" if you prefer.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 1 '08 #9
CBFalconer wrote, On 01/10/08 01:06:
Flash Gordon wrote:
>la************@siemens.com wrote, On 30/09/08 20:41:
... snip ...
>>There's always the problem of needing to do work both before
and after the test, the "exit in the middle" loop. Until C
adopts my generalized do-while loop:

do /statement/ while ( /expression/ ) /statement-opt/ ;

we're pretty much stuck with a "forever loop" with a break in
the middle or replicating code (which is much worse in my
opinion).
Well, you could do the following which is even worse instead...

goto loop_start;
do {
/* code after condition */
loop_start:
/* code before condition */
} while (condition);

I've deliberately used a do-while loop instead of a while-do
loop because I know some people dislike do-while loops and I
wanted this to be horrible for as many people as possible :-)

Well, we could still stick to legal C and have:
Given some code after the label what in my example is *not* legal?
flag = false;
do {
if (flag) code_after_condition();
flag = true;
code_before_condition();
} while (condition);
A horrible use of a flag requiring an extra test and assignment to be
done on each loop. Ones I expect will not be optimised out. Also
horrible because when reading the code you have to check whether flag is
changed anywhere else (yes, I know with mine you need to check for
gotos, but at least I used a better name ;-) )
or (something that looks like loop and a half code):

do {
code_before_condition();
if (!condition) break;
code_after_condition();
} while (1);

You can also use "for (;;)" or "while (1) {}" if you prefer.
Read what Lawrence wrote. He said that whilst avoiding other things he
considers worse that is exactly what we are stuck with.
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Oct 1 '08 #10
Flash Gordon wrote:
CBFalconer wrote, On 01/10/08 01:06:
>Flash Gordon wrote:
.... snip ...
>>
>> goto loop_start;
do {
/* code after condition */
loop_start:
/* code before condition */
} while (condition);

I've deliberately used a do-while loop instead of a while-do
loop because I know some people dislike do-while loops and I
wanted this to be horrible for as many people as possible :-)

Well, we could still stick to legal C and have:

Given some code after the label what in my example is *not* legal?
Note I said 'still'. It didn't imply your code to be illegal.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 2 '08 #11
CBFalconer wrote, On 02/10/08 01:02:
Flash Gordon wrote:
>CBFalconer wrote, On 01/10/08 01:06:
>>Flash Gordon wrote:
... snip ...
>>> goto loop_start;
do {
/* code after condition */
loop_start:
/* code before condition */
} while (condition);

I've deliberately used a do-while loop instead of a while-do
loop because I know some people dislike do-while loops and I
wanted this to be horrible for as many people as possible :-)
Well, we could still stick to legal C and have:
Given some code after the label what in my example is *not* legal?

Note I said 'still'. It didn't imply your code to be illegal.
I read too fast and missed the "still", sorry. I've now gone back to the
still and got some moonshine. <hic>
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Oct 3 '08 #12
Flash Gordon wrote:
CBFalconer wrote, On 02/10/08 01:02:
>Flash Gordon wrote:
>>CBFalconer wrote, On 01/10/08 01:06:
.... snip ...
>>>Well, we could still stick to legal C and have:

Given some code after the label what in my example is *not* legal?

Note I said 'still'. It didn't imply your code to be illegal.

I read too fast and missed the "still", sorry. I've now gone back
to the still and got some moonshine. <hic>
Prosit. Skoal.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 3 '08 #13

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

Similar topics

2
by: Trev | last post by:
I have the following view (vProcurementPlan) SELECT dbo.tblProcurementPlan.*, dbo.tblRequisition.RequisitionID AS ReqReqID, dbo.tblRequisition.ReqNo AS ReqNo, dbo.tblRequisition.Am AS Am,...
2
by: Ivo | last post by:
Hi, I have an audio file (.mid or .wav or .mp3) in an object element: <object id="snd" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"...
45
by: Trevor Best | last post by:
I did a test once using a looping variable, first dimmed as Integer, then as Long. I found the Integer was quicker at looping. I knew this to be true back in the 16 bit days where the CPU's (80286)...
4
by: ={ Advocated }= | last post by:
Hi there, im in need to use a loop, im not sure exactly how to do it :S ######################## /* * File: Payroll.c * Program to calculate the wage of a user */ #include <stdio.h> int...
4
by: Norman Fritag | last post by:
Hi there, >>>__ 1020.83, 2305.22, 1176.86, 755.12, 123.41 __ 1976.1, 1325.99, 947, 718.03, 414.32 __ 1020.83, 1976.1, 352.5, 947, 718.03, 366.98 Their IDs were as...
14
by: John Salerno | last post by:
Here's an exercise I was doing to guess a number from 1-100. Just for fun (ha ha) I decided to add some error checking too, and now when I run it, the DOS prompt flashes real quick and disappears....
5
by: sandyw | last post by:
I need help in two areas: Both are at the bottom of my program where a client has two choice to make, 1. When the client enter "Y" it takes him to the begin of the program. I would like to know...
1
by: assgar | last post by:
Hello I have changed the process code abit so it receives the data from the form and ensures the data in array format. This has eliminated my previous error. The problem I am experiencing...
20
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1...
3
by: chiku1523 | last post by:
Hi, Please find the following code. In function setAnswers, I am looping with each question. I have inner loop, which is looping for each answers of the questions. If any of the answer for question...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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.