MATLAB While Loop (Week 7)
Genaro Rivera October 25, 2022 MATLAB While Loop (Week 7) While Loop This week, I focused on the while loop. A while loop repeats until a condition is met. The best way to think about coding is by visualizing yourself talking to someone. For example, above, you are saying “X is one. While X is less than 5, add 1 to it. If x is below 5, keep adding 1 to it.” Eventually, x becomes 5 which is where the loop ends. However, infinite loops exist as seen below. Here, x will always be less than 5; if you keep subtracting 1 from 1, it will never be greater than or equal to 5. The picture on the left displays the output (AKA the command window). The command window rapidly subtracts 1 for ever until you stop it by pressing control c; I stopped it at -214106. In conclusion, while loops repeat until a condition is met. However, if the condition is never met, the loop repeats forever. While Loop and For Loops Differences Here, both functions do the same thing, but the for loop is better....