473,695 Members | 3,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to do an infinite loop

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

Mar 1 '06 #1
59 79745
while (1)
{
// you code here
}

Mar 1 '06 #2
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

Mar 1 '06 #3
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)
Mar 1 '06 #4
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
==============
Mar 1 '06 #5

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

Mar 1 '06 #6

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.

Mar 1 '06 #7
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
==============
Mar 1 '06 #8

"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 "conditiona l 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
Mar 1 '06 #9
-----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

iD8DBQFEBaTAagV FX4UWr64RAiaiAK DbFvIGUrKGQxe7r 4M9eOK8/V4zjgCfc+eb
huRZZhoqn/pUhwWw/IlHSo0=
=mMqY
-----END PGP SIGNATURE-----
Mar 1 '06 #10

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

Similar topics

43
5580
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a fallacy built on the assumption of mythical infinite all powerfull machines. In reality we deal with finite machines that are capable of two states in a loop, they either terminate, or repeat themselves. In the mythical halting problem scenario...
5
4939
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
4
2632
by: LOPEZ GARCIA DE LOMANA, ADRIAN | last post by:
Hi all, I have a question with some code I'm writting: def main(): if option == 1: function_a()
10
7645
by: Steven Woody | last post by:
i have a program which always run dead after one or two days, i think somewhere a piece of the code is suspicious of involving into a infinite loop. but for some reason, it is very hard to debug. so i thing there might be tool which can statically analysis the code and notice me this kind of potential problems if found. any suggestion? ( i prefer linux open source tools ) thanks. -
44
4320
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
0
8555
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9112
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8971
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8824
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8817
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7651
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5831
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4571
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2994
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.