The ANNOYING loop…

Helenjoy
4 min readJun 12, 2023

--

Here’s a small example program that will keep asking you to type, literally, your name.

First, the program sets the name variable to an empty string. This is so that the name! = ‘your name’ condition will evaluate to True and the program execution will enter the while loop’s clause.

The code inside this clause asks the user to type their name, which is assigned to the name variable. Since this is the last line of the block, the execution moves back to the start of the while loop and re-evaluates the

condition. If the value in name is not equal to the string ‘your name’, then the condition is True, and the execution enters the while clause again. But once the user types your name, the condition of the while loop will

be ‘your name’ != ‘your name’, which evaluates to False. The condition is now False, and instead of the program execution re-entering the while loop’s clause, Python skips past it and continues running the rest of the program.

Run it, and enter something other than your name a few times before you give the program what it wants. If you never enter your name, then the while loop’s condition will never be False, and the program will just keep asking forever. Here, the input () call lets the user enter the right string to make the program move on. In other programs, the condition might never actually change, and that can be a problem.

break Statements

There is a shortcut to getting the program execution to break out of a while loop’s clause early. If the execution reaches a break statement, it immediately exits the while loop’s clause. In code, a break statement simply contains the break keyword. Pretty simple, right? Here’s a program that does the same thing as the previous program, but it uses a break statement to escape the loop. Enter the following code

The first line creates an infinite loop; it is a while loop whose condition is always True. (The expression True, after all, always evaluates down to the value True.) After the program execution enters this loop,

it will exit the loop only when a break statement is executed. (An infinite loop that never exits is a common programming bug.)

Just like before, this program asks the user to enter your name. Now, however, while the execution is still inside the while loop, an if statement checks whether name is equal to ‘your name’. If this condition is True, the break statement is run, and the execution moves out of the loop to print (‘Thank you!’) . Otherwise, the if statement’s clause that contains the break statement is skipped, which puts the execution at the end of the while loop. At this point, the program execution jumps back to the start of the while statement to recheck the condition. Since this condition is merely the True Boolean value, the execution enters the loop to ask the user to type your name again. See Figure for this program’s flowchart.

continue Statements

Like break statements, continue statements are used inside loops. When the program execution reaches a continue statement, it immediately jumps back to the start of the loop and re-evaluates the loop’s condition. (This is also what happens when the execution reaches the end of the loop.)

If the user enters any name besides Joe , the continue statement causes the program execution to jump back to the start of the loop. When the program re-evaluates the condition, the execution will always enter the loop, since the condition is simply the value True. Once the user makes it past that if statement, they are asked for a password. If the password entered is swordfish, then the break statement is run, and the execution jumps out of the while loop to print Access granted . Otherwise, the execution continues to the end of the while loop, where it then jumps back to the start of the loop.

--

--

Helenjoy

Research aspirant in deep learning based video compression