-1

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?

closed as not a real question by Arseni Mourzenko, 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
  • 5
    The existence of Hello, World should be the key to finding the answer. – Blrfl Jun 15 '13 at 11:50
  • Every program which runs for more than few seconds has to do some looping, out of necessity. CPU core running at 2GHz performs something on the order of 2*10^9 instructions per second. If you look at size of most binaries, then running it from top to bottom at such speed, assuming one instruction is 4 bytes (just guessing here), would finish in a fraction of a second. Therefore, programs must either loop, wait, or perform I/O, otherwise they won't run very long. – user7610 Aug 17 '18 at 21:03
2

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.

2

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.

1

“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.

  • 2
    "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.