Control Structures with Python

Updated 12 April 2022

Welcome to the next step of comprehension for the fireball programming language of Python. This guide will provide you with tools to help aid conceptualisation 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 executed and evaluated. The control flow can also be referred to as the flow of control. Simply put, the program flow is a general term which describes the order in which your lines of code are carried out. This flow can become increasingly intricate as your courageous coding capers climb in complexity. This is when utilisation of control structures becomes invaluable.

Therefore, this guide will introduce control structures and establish them as a fantastic tool for control flow conceptualisation. 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 execution by the computing device. Once learned this knowledge will be applicable no matter what programming language you end up utilising. The contents of this guide can be seen below.

There are several related tutorials on our website to help you become a coding magus. A great place to start would be Python Rundown and Main Windows and Python and MicroPython - Compare and Access. These will also tell you where to download Python and access online Python emulators. Python is an excellent language to utilise with Raspberry Pi Boards such as Raspberry Pi 4 Model B 8GB. These are computing devices that can easily fit on the palm of your hand.

As always if you have any questions, queries or things to add please let us know your thoughts!


Overview of Control Structures

step by step flowControl structures are a flowchart method to represent the execution flow of programming languages. The flowchart shows each execution step as boxes of various shapes. 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 top to bottom and each box is connected with arrows.

Interesting facts - flowcharts to document business processes (or any process in that matter) only came into use in the 1920s. This means the Model T car manufactured by the Ford Motor Company pre-dates flowcharts. Frank and Lilian Gilbreth introduced the Flow Process Chart to the American Society of Mechanical Engineers in 1921. Furthermore, they had 12 Children together and Cheaper by the Dozen was written by one of their children. I am sure a lot of flowcharts were employed at their residences. Frank died when 55 and Lilian outlived him by 48 years.

In control structures flowcharts there are three basic symbols. These are rectangles, diamonds, and arrows. They are given unique box shapes to make the identification easier. If you were to write out your written code as a control structure each executing line would have its own box. A description of these symbols can be seen below. Worth noting, it is common to use rectangles with rounded edges to denote the start and end of a software program.

Control Structures Legend

 
Below are the simplest examples of these Control Structures being employed in a flow chart to represent flow structures of simple code. In a sequence structure, an action, or event, leads to the next ordered action in a predetermined order. A sequence structure simply executes a sequence of statements in the order in which they occur. Next is a selection structure which can also be referred to as a decision. 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. The question usually utilises a Boolean choice. The future tutorial If, Elif and Else Keywords in Python dive into these. Finally, below there is the simplest representation of an iteration structure. An iteration structure will execute a sequence of statements over and over while a condition holds true. This 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 to the next step of code. The future guide Loops in Python deeply focus on the iteration structures and the variations of it.

Three Simple Code Flowcharts Using Control Structures


Sequence Flowchart and Code

A sequence structure executes a sequence of statements in the order in which they are written. Below is a Python code made in the Python Programming Window in which a sequence occurs. The code produces a number of different variables and then displays the variables to the Python IDLE Shell which can be seen adjacent. A representation of the code using a control structure flowchart is further below. 

Sequence of Variables being made and then printed

Control Structure Flowchart Annotated with code in a sequence


Selection Flowchart and Code

A selection structure involves asking a question and depending on the answer, the program takes one of two courses of action, after which the program moves on to the next event. Below is a Python code in which a selection occurs and also a representation of the code using a control structure flowchart. The code produces a result which it then displays in the Python IDLE Shell which can be seen adjacent to the Python Programming Window below. The selection process is done using a comparison operator. More about these can be found in the guide All the Operators in Python! Precedence too

Bouncers performing their job.

Control Structure Flowchart Annotated with code that performs a selection


Iteration Flowchart and Code

An iteration structure is one which will execute a sequence of statements repeatedly if a condition holds true or a task is incomplete. 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 to the next step of code. Below is a Python code created in the Python Programming Window in which an iteration occurs. The below code adds up all the elements inside a list and then displays the result to the Python IDLE Shell which can be seen adjacent. Further below is a representation of the code using a control structure flowchart.

The below code uses a keyword to enact an iteration which utilises a looping structure. More about keywords can be found in the guide All the Keywords in Python! The very next step on our Python journey will be a deep dive into loops. Loops are among the most basic yet also the most powerful programming capabilities. They are utilised by all modern programming languages. Loops effectively instruct a computer to repeat a process until a specified condition is reached. 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 the criteria are met or no further action is required. Each time the question is asked or the task occurs it is called an iteration.

Iteration occurring in Python code to count up all the integers in the list

Control Structure Flowchart Annotated with code that performs an iteration


The Next Step

The natural next step in our coding adventures would be to learn deeper on how to control the execution flow. An excellent starting point on this is the guide If, Elif and Else Keywords in Python.


Download the Codes

Below is a link which will let you download the code directly into your computing device. Open it up with Python IDLE Shell and run it using the Python Programming Window.  

Attachment - Control_Structures_Python_Codes.zip

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
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.