Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was wondering whether all programs run in a Loop, checking for variable changes then acting upon said changes followed by another Loop. Or do they run in a While statement, waiting for a variable to change before they execute a routine? Or am I just completely wrong?

share|improve this question

closed as not a real question by MainMa, gnat, Giorgio, Thomas Owens Jun 15 '13 at 12:45

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

A while statement initiates a loop (in a number of languages), firstly. Secondly, if all programs ran in a loop, nothing would stop running. – Mr_Spock Jun 15 '13 at 7:26
Learn about CPU instructions and assembly. – Florian Margaine Jun 15 '13 at 8:10
The existence of Hello, World should be the key to finding the answer. – Blrfl Jun 15 '13 at 11:50
up vote 3 down vote accepted

No, not all programs run in a loop. Interactive programs have a main (message) loop to respond to user input.

Programs also run in some sort of loop (while not Terminated do) when they stay around and wait for input other than user actions. For example HTTP servers that wait for requests to arrive, image processers that wait for files to arrive in a folder, etc.

Programs that take their input parameters, do their thing, report back and then finish, will not run in a loop. That doesn't mean they do not use any loops at all. The work they do may require plenty of loops, but they don't run a loop waiting for more input.

share|improve this answer

You should learn something about computer science. It will show you that when a program runs, it is the CPU fetch instructions to run. So there is a possibility that cpu do nothing but wait for a intterrupt to wake it up.
The event driven is the best practice nowadays. It costs little and responsive quickly. Though there is not pure event driven os. Linux like OSes use kernel to reponse to interrupts and manage processes by loop-calling schedule method.

share|improve this answer

“While” is a form of loop. For the scope of your question it would make no difference.

CLI programs usually don't run in a loop. GUI programs usually do. Some have parallel execution flows. I suggest you read the Wikipedia article on event loop so that you grasp the different styles.

share|improve this answer
"CLI programs usually don't run in a loop" -- this is not an accurate statement; CLI programs intended to interact with user (Windows cmd, Unix shells) or provide a long-running service often run in the loop – gnat Jun 15 '13 at 8:33
@gnat: Exactly. The distinguishing factor isn't the interface - commandline or (G)UI - but whether the program is interactive or not. – Marjan Venema Jun 15 '13 at 9:34
@MarjanVenema funny thing is, even this may be not quite relevant to this very vague question. It may well be OP is interested to understand something like busy-wait / monitor-wait but oh well – gnat Jun 15 '13 at 9:38
@gnat: yeah, you are right. – Marjan Venema Jun 15 '13 at 9:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.