Section 15.4 The Dangerous Loop
This chapter would not be complete without a mention of
while-end loops. They will be described below, however, I encourage you to only use while-end loops in cases where it is impossible to use for-end loops.

The reason I am warning about while-end loops before discussing them is that they are the only thing that we will learn in this book that can create \(\infty\) loops.
What is an \(\infty\) loop? Well, why don’t you go ahead and try one and see?
How to Kill (and Create) an Infinite Loop
Before I show you how to create an infinite loop, I will first tell you how to kill one.
To stop an infinite loop, select the MATLAB command window and hit
ctrl+c on your keyboard. OK, ready to create an infinite loop? Create the following script, save it as to_infinity_and_beyond.m, and click run. Once you get bored of seeing the numbers fly by, hit Ctrl+c on your keyboard to stop it.

What was going on there? The reason that
while loops are dangerous is because they do not have a predetermined number of times that they run. Instead, while loops run until the condition they were initialized with is false. The reason this loop keeps running forever is that the initialized condition, z >= 0 was never made false. Since z started equal to 0, and all we did in the loop was keep adding positive values to it, z >= 0 was always true.
You have attempted of activities on this page.
