Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to do an infinite loop

Reply
Thread Tools

how to do an infinite loop

 
 
rami
Guest
Posts: n/a
 
      03-01-2006
please everybody ,can anyone tell me how to do an infinite loop in C

 
Reply With Quote
 
 
 
 
prettyboy988-google@yahoo.com
Guest
Posts: n/a
 
      03-01-2006
while (1)
{
// you code here
}

 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      03-01-2006
rami wrote:
> please everybody ,can anyone tell me how to do an infinite loop in C


well the obvious way that applies to pretty well any programming
language
(I'm sure some will take it as a challange to give a counter example
(*)
is to use a while statement.

while (expression)
statement;

This executes "statement" while "expression" is true. So all you have
to
do is choose an expression that is always true

while (1 == 1)
statement;

as C treats any non-zero value as true then more succinctly

while (1)
statement;

The idiomatic (C-like) way to do it is

for (;
statement;

read your textbook to understand why this works. Even if you
don't like this you must recognise it as you will see it in other
people's code


(*) I bid Algol-60

--
Nick Keighley

"ALGOL 60 was a language so far ahead of its time that it
was not only an improvement on its predecessors but also
on nearly all its successors".
--C.A.R. Hoare

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-01-2006
rami said:

> please everybody ,can anyone tell me how to do an infinite loop in C


I have written a program for you, that contains an infinite loop, but it
wouldn't be right to show you the code until its test run is complete.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      03-01-2006
rami wrote:
> please everybody ,can anyone tell me how to do an infinite loop in C


I have seen the

while(1)

construct produce warnings with some compilers - 'constant in conditional
expression' or some such thing.

The proper C way is I believe to use

for( ; ; )

that should *not* produce any complaints.


--
==============
Not a pedant
==============


 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-01-2006

pemo wrote:
> rami wrote:
> > please everybody ,can anyone tell me how to do an infinite loop in C

>
> I have seen the
>
> while(1)
>
> construct produce warnings with some compilers - 'constant in conditional
> expression' or some such thing.
>
> The proper C way is I believe to use
>
> for( ; ; )
>
> that should *not* produce any complaints.


IMHO, this is a matter of taste (I prefer `while (1)`). They're both
correct.

I see no reason for either to produce diagnostic messages, although I
could think of a weak rationale for the one you were seeing.

--
BR, Vladimir

 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      03-01-2006

rami wrote:
> please everybody ,can anyone tell me how to do an infinite loop in C


Pick any of the following:

1. for(; {...}
2. while(1) {...}
3. do {...} while(1);

I personally use 1.

 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      03-01-2006
Vladimir S. Oka wrote:
> pemo wrote:
>> rami wrote:
>>> please everybody ,can anyone tell me how to do an infinite loop in C

>>
>> I have seen the
>>
>> while(1)
>>
>> construct produce warnings with some compilers - 'constant in
>> conditional expression' or some such thing.
>>
>> The proper C way is I believe to use
>>
>> for( ; ; )
>>
>> that should *not* produce any complaints.

>
> IMHO, this is a matter of taste (I prefer `while (1)`). They're both
> correct.
>
> I see no reason for either to produce diagnostic messages, although I
> could think of a weak rationale for the one you were seeing.


I must admit that I haven't seen this warning for quite a while now, but, I
can see why a compiler might see some merit in it, e.g., while(x = 1) might
produce the same output - and, of course, be rather useful.

For those that don't read post too fully - yes, I know that x = 1 should
normally be x == 1

--
==============
Not a pedant
==============


 
Reply With Quote
 
John F
Guest
Posts: n/a
 
      03-01-2006

"pemo" wrote:
> Vladimir S. Oka wrote:
>> pemo wrote:
>>> rami wrote:
>>>> please everybody ,can anyone tell me how to do an infinite loop in C


<snip> [while(1) vs. for(;]

>> IMHO, this is a matter of taste (I prefer `while (1)`). They're both
>> correct.
>>
>> I see no reason for either to produce diagnostic messages, although I
>> could think of a weak rationale for the one you were seeing.

>
> I must admit that I haven't seen this warning for quite a while now, but,
> I can see why a compiler might see some merit in it, e.g., while(x = 1)
> might produce the same output - and, of course, be rather useful.


Yes, that's the rationale for the warning (which can be quite annoying when
porting code from another compiler. But it is better to be warned in a fwe
cases than falling into an endless loop (evenmore on embedded contol
devices, which is OT here, but a huge field where C is used in various
dialects)). The message usually gives something like "conditional expression
in ... is always true".
or "... false", which is annoying when someone is using do{/*code goes
here*/}while(0) to encapsulate a function like macro.

--
regards
John


 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      03-01-2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

John Bode wrote:
> rami wrote:
>> please everybody ,can anyone tell me how to do an infinite loop in C

>
> Pick any of the following:
>
> 1. for(; {...}
> 2. while(1) {...}
> 3. do {...} while(1);


add to that list
4. label: ... goto label;


- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEBaTAagVFX4UWr64RAiaiAKDbFvIGUrKGQxe7r4M9eO K8/V4zjgCfc+eb
huRZZhoqn/pUhwWw/IlHSo0=
=mMqY
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
infinite loop unexpectly dies Vedran Vukotic Perl 0 03-02-2006 07:43 AM
loop thru a STL list causes an infinite loop Allerdyce.John@gmail.com C++ 5 01-31-2006 03:21 PM
Infinite loop when using Server.Transfer Alexander Bosch ASP .Net 11 11-10-2004 08:53 AM



Advertisments