Control structures rule! Laid out for you is the next step of comprehension for the fireball programming language of Python. This guide will provide you with tools to help aid conceptualisation of control flow within programming scripts so you can hit the ground running. Simply put, the control flow is a general term which describes the order in which your lines of code are carried out.

Transcript

Hey gang! Tim here at Core Electronics and welcome to the next step of grasping the fireball programming language of Python. This guide will provide you with the tools to aid conceptualization of the control flow within your programming scripts!

The control flow is the sequence in which individual statements, instructions or functions of a software program are evaluated and executed. The control flow can also be referred to as the flow of control, simply put the program flow is the general term which describes the order in which the lines of your code are carried out. This flow can become increasingly intricate as your courageous coding capers climb in complexity this is when utilization of control structures become invaluable. Therefore, this guide will introduce control structures and establish them as a fantastic tool for control flow conceptualization. Control structures are a flowchart method to represent the flow of programming languages. These flowcharts are used worldwide as a method of grasping the movement of the code executed by the computing device, once learned, this knowledge will be applicable no matter what programming language you end up utilizing.

So, jumping into the computer we'll look at the online write-up for this guide. So, if we scroll down, we'll see these are the different shapes that you'll run into when using control structured flowcharts. The shape of the box can represent either a command statement, a variable creation, a condition or a decision. When drawn the flow tends to be from the top to the bottom and each box is related to each other via arrows, so scrolling further down you can see the simplest examples of these control structures being employed in a flowchart, to represent flow structures of very simple code. So, for instance in a sequence code, an action or event leads to the next ordered in a predetermined manner.

Next is a selection structure which can also be referred to as a decision, so in a selection structure a question is asked and depending on the answer the program takes one of two courses of action, after which the program moves on to the next step of code, this question is usually a Boolean type. An iteration structure will execute a sequence of statements over and over while a condition holds true. The condition is also usually a Boolean question or a task, once the condition is no longer true and the flow of control returns to the condition block it will stop the iteration and the program will move on to the next step of code.

Now on screen you can see a simple code in the Python programming window, setting up several variables and then printing them to the Python idle shell. See when I run this code it will print “A”, “B” and “C” you can see the results printed effectively instantly to the human mind. Brought to the table I have a control structure flowchart representing this code “A = 2” being the first step and this will happen before “print (A, B, C)” so even though the execution seems instantaneous it is important to realize a split second or more correctly a split millisecond, this has occurred before this. Now this is an elegant way to demonstrate precedence. Now worth noting you may see out in the wild, rectangles with rounded corners like so, and these are used to denote the start and end of code and depending on whether it helps your conceptualization of what's going on in the code you can use them.

On the screen you can see a simplification of a bouncer performing his job in Python script form. There are two variables defined and a comparison operator. The first variable “A” represents the client's age and if the client is over 18 years old (the drinking age within Australia) then they are welcomed into the club then “B” represents the future actions of the bouncer. I have brought to the table a control structure flowchart which clearly demonstrates there are two potential ways for the control flow to get to the stage to produce the variable “B”, down this way “If A >= 18:” is not reached then it can get this way “If A = 18” or above it can go this way. So if I was to run this script right now when “A = 18” you'll see “come into the club buddy” however if this number was less than “18”, let’s say “16” run this, save, it won't prevent anything. So, in this example if the client is under 18 years old the bouncer will completely ignore the client.

Finally, an iteration structure is one which will execute a sequence of statements repeatedly if a condition holds true or a task is incomplete. This specified condition can be thought of as a question or task to the computer. The same question or task is asked again and again until criteria is met, or no further action is required. Each time the question is asked or the task occurs, this is referred to as an iteration, once the condition is no longer true and the flow of control returns back to the condition block it will stop the iteration and the program will continue to the next step of code.

Loops are among the most basic yet also the most powerful programming capabilities, they are utilized by all modern programming languages. The code that I have pulled up enacts a looping structure using a “for” keyword. The code adds up all the elements inside this list and then displays the result to the Python idle shell which can be seen below, so I’ll do this right now and you can see the result comes out as 98. Running through quickly we have two variables created, this is a list variable, and this right here is just a normal integer variable. “For” element, so each of these is referred to as an element in “A” so for all of these in “A” total equals total plus element. So total is originally “0” so what it would be is total equals total “0 “plus element “1” and then it loops back around, so you can see that this can become a little bit complicated so that's why I brought a control structure flowchart to the table.

So here we have the two variables being created. Here we have the loop being initialized and here is the total being added up to the element and this keeps going round and round and round until all of these elements have been looked at and when all these elements get looked at, they get added together so the final result, the print total, will be the final result of total after this loop has been completed and that is the sum of all the elements in “A” and that's it having a strong grasp on control structures will instantly make you a better coder, also future tutorials will be referring back to these control structures as I, a very visual focused individual love using these. Until next time stay cosy!

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.