Python – Configuring “While” and “For” Loop Types, “Continue” and “Break” statements, and range function reviewed!

LoopPic5

In continuing through Automating the Boring Stuff book, I thought posting some demonstrations of Loops and how to program them to avoid infinite program loops would be a critical concept to get documented to reflect on.

First concept of Loops, using the “while” keyword rather than “if” statements

While is a loop statement / expression (I still am terrible at programming terminology) that says “While this condition is True go back to the beginning of the programs while statement” which hence, it being a “loop” in the program.

This is in contrast to If / Else / Elif Statements that just execute one “iteration” of the code, whereas while will go through several iterations until it allows the program to proceed:

Using “if” statement only going through a single iteration

LoopPic1

In Visual Studio Code no matter how many times I hit the green play button to execute the program, it might have added one but then the program was over, and executing it again would start it new thus being a single “Iteration” each time.

Using “while” to create a loop in the program:

LoopPic2

Because the Variable Spam starts out as the Integer 0 it runs 10 times to equal 10, as it is adding +1 each “Iteration” of the loop, this is a difference that I did not account for in my previous post with a flow chart – My program did not have any Loop statements to being another “Iteration” as I was using If / Elif / Else.

All of this uses a strain True / False / If True / If not True / While True / While False / Etc.

The first example hit a False and printed the statement “Still less than 10!” and added +1 to the Variable and then the program stopped, whereas While adds +1 to the Variable then returns to execute it again until it equals True.

Reviewing how to break a bad program while loop using “Break” statement

Below is an example of a poorly written program, that will not function as it should with using the while loop, because it is kicked off by making name = ” variable to make the while statement True to continue to execute its following Clause or Code Block:

LoopPic3

This is a bad way to configure a program because it will only accept ‘your name’ as a True statement that allows the program to move forward, however we can insert a break statement to exit the program moving forward if an issue like this may be implemented into the code:

LoopPic4

This is a purely for demonstration, as you would never want a program that continues Looping asking for a name until you figure out how to hit that break clause, however you can use it to exit the program if we are using the following “Continue” statement.

The “continue” statement will require the “break” to state an infinite Loop program

I’ve added an extra element on Line 3 to this program, the print function to demonstrate that Loops will only return to the beginning of the Loop, not the entire program:

LoopPic5

Note it does run “Hello World!” once as a standalone function before the While Loop begins, and when it does fail it begins a second “Iteration” of the Loop beginning at the while statement, however note that it restarted despite True statements!

This is where “break” comes to the rescue – It “breaks” the While Loop if True!

For this program I want the program to “Break” or exit after the User and Password are both True, then they are granted access and the show is over, to do this I use “Break” :

LoopPic6

Now the program will re-ask for a name immediately if not found as True (Dave) because “Continue” allows it to continue executing, whereas once the Password is also True it will “Break” the Loop with an “Access Granted” message, or if the input is False it will hit the “Else” statement and start another Iteration at the beginning line of the While Loop.

Note – Continue and Break are both used inside Loops, and must be at the same indentation level to be understood by the program when executing!

Just like other Code Blocks / Clauses things needs to be indented at a common level, though it can be any amount of spaces, it has to be the same for similar statements.

Indentation is as important as considering Iterations (how times the loop runs) when you are using other types of Loop Statements, as shown in the very top “while x <= 10” program we looked at.

There are a couple more Loop types to hit quick.

Reviewing “For” Loops and the “range” function within the Loop

The “While” Loop is used to continue looping until the statement(s) = True, whereas “For” Loops will repeat the loop a number of times, and this brings in the range function:

LoopPic7

This program does also use indentations, and can also include the “continue” and “break” statements to exit the program at a determined point, though that would be used in a more robust program rather than a single range statement.

Using the While Equivalent Loop (very similar to the top example) using range

The last program can be tweaked just a bit to use “While” instead of “For” along with the Range command to perform a similar function as the very top:

LoopPic8

Exact same function really, just a different way / more complex way of writing the code.

A quick look at how the range function works with “For” loops

Wanted to demonstrate quick how you the range function works on its own with counting upwards and downwards with the “For” loop function with this first demo:

RangePic1

In this program we define the Integer Range 0-20, however we never see 20 printed, because this program uses the “in” statement, with a third # / integer is how much is added before the next “Iteration” of the For Loop starts.

For example, if we adjust the second # to 21, then we will see 20 included in there:

RangePic2

Without any third # it will simply assume the count is increasing by +1 when printed:

RangePic3

And so on, again it does not include the integer 5, as it is printing the integers “in” the range of the “for” Loop.

I won’t continue the redundancy of examples, but it works backwards and such as well.

And that is about all I have to say about Python Loops (for now)!

This is just demo’s of how the different Loops works, I am sure as I move into more advanced topics, I will be getting much deeper into Loops / Ranges / All Data Types.

Until then, keep it geeky! 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s