COP2271 Notes while loops 1 while loops The while loop allows your program to repeatedly execute a series of statements if a given condition is true. Here is the syntax: while condition statements end The condition is any valid MATLAB expression that can be interpreted as true or false. The condition for a while loop has the same behavior as a condition for an if statement. Replace statement with the code you want repeatedly executed until the condition becomes false. Make sure all code is listed before the end keyword. Here is our first example that prints the numbers 1-5 to the screen: printMe = 1 ; w h i l e printMe <= 5 d i s p ( printMe ) ; printMe = printMe + 1 ; end First, the code assigns the value 1 to printMe. The condition for the while loop checks if printMe is less than or equal to 5. The first time the program encounters the while loop, printMe is 1 which is less than 5, making the condition true. The program then performs the statements before the end keyword and then returns to top of the while loop to check the condition again. This process is repeated until the condition is false. The table below explains the loop in detail: Iteration Condition Condition Result First 1 <= 5 true Second 2 <= 5 true Third 3 <= 5 true Fourth 4 <= 5 true Fifth 5 <= 5 true Sixth 6 <= 5 false display display display display display 1 2 3 4 5 Statement and increment printMe and increment printMe and increment printMe and increment printMe and increment printMe exit loop, skipped to to to to to 2 3 4 5 6 The key idea here is that after MATLAB reaches the end keyword, it jumps back to the while keyword above. This action causes the code to loop and will repeat until the condition becomes false! If your script ever gets stuck in an endless loop, you can hit CTRL + C to stop it from running (CMD+C on OSX). COP2271 Notes while loops 2 Here is a more complex example: clc ; clear ; userNumber = i n p u t ( ' Try t o g u e s s my s e c r e t number : numOfGuesses = 1 ; w h i l e userNumber ˜= 42 i f userNumber < 42 d i s p ( ' Your g u e s s i s t o o low ! ' ) e l s e i f userNumber > 42 d i s p ( ' Your g u e s s i s t o o h i g h ! ' ) end userNumber = i n p u t ( ' Next g u e s s : numOfGuesses = numOfGuesses +1; end '); '); i f ( numOfGuesses == 1 ) d i s p ( ' You g o t i t on t h e f i r s t g u e s s ! ' ) else f p r i n t f ( ' I t took you %d g u e s s e s ! \ n ' , numOfGuesses ) end Jump statements: break and continue The break statement allows us to interrupt the normal execution of loops (while and for). break immediately stops the execution of the loop regardless of the condition. Note that the break statements stops the closest loop only, regardless of how many loops are nested. In the following example, we set the condition of the while loop to true (a MATLAB keyword), which means this loop will keep running until a break is found: printMe = 5 ; while true f p r i n t f ( '%d \n ' , printMe ) ; printMe = printMe −1; i f printMe <= 0 break ; end end f p r i n t f ( ' Go ! \n ' ) ; Notice that the loop is now controlled by the if statement. Essentially, break allows us to check when to stop the loop using code inside of the loop itself! Break is often used in conjunction with an if statement. COP2271 Notes while loops 3 Alternatively, the continue statement skips the rest of the current loop and begins the next iteration. This allows us to skip lines of code inside a loop if needed. Try the following example which only prints the positive numbers entered by the user. u s e r V a l u e = −1; while u s e r V a l u e ˜= 0 u s e r V a l u e = input ( ' P l e a s e e n t e r p o s i t i v e numbers ( 0 t o q u i t ) ! ' ) ; i f userValue < 0 continue ; end f p r i n t f ( ' You p i c k e d p o s i t i v e number : %d \n ' , u s e r V a l u e ) ; end