A common programming mistake among learners is to accidentally write a program that runs forever. For example:
x = Get next input
while x > 0
Print x*x to screen
Oops, the programmer forgot to read the next input at the end of the loop's body. So if x is initially 5, x stays 5 forever, since x > 0 is always true, the loop will loop forever -- known as an "infinite loop". There are many ways that new programmers create infinite loops, such as:
- Not updating the variable used in the loop condition
- Updating the variable in the wrong direction, as in: for (i = 10; i >=0; i++) (the programmer meant i--)
- Creating an expression that is always true, as in: while (x = 1) (in a language like C or C++, where = is assignment; the programmer meant ==).
- And so on...
Other reasons that programs don't end include:
- Waiting for input that doesn't exist, such as a program that is supposed is only provided one input but the program tries to read two inputs.
- Infinite recursion: Calling a function recursively in a manner that never reaches a base case, so the recursive calls just keep occurring.
There are other ways too!
The way our system works, a student's program is sent to Amazon Web Services (AWS) for execution in a protected environment. Our system provides input to the program, runs the program, and collects the output. Upon program completion, the system sends that output back to the web browser for display.
If students write programs that run forever, well our AWS would soon be filled with hundreds or thousands of such programs, just cranking away with no end in sight, using up valuable computer time. So, our system only allows a program to run for a few seconds, which is usually way more than the few microseconds or milliseconds that most programs in a zyBook require. After a few seconds, the system kills the program and just returns a "timeout" or "end not reached" message, and doesn't bother trying to return the output (which could be enormous).