Control structures are an essential aspect of any programming language. They determine the control and the flow of the code so it can transform into logic.
Control structures are responsible for deciding the flow and lead the program into a successive path to generate a desired output. In this article, we will learn three control structures in Python.
Computer programs are said to shape into the control structures or the flow control that are nothing but a block of decisions that analyze the flow through instructions.
All the decisions are made based on two categories, the data and instructions to provide a particular response.
When you deal with data, you try to understand the nature of it (data type), and when you deal with instructions you need to understand on what basis are, they are being made on. Control structures, defines the instructions to be taken through appropriate statements
There are three types of control structures in python. Selection, Sequential, and repetition (iteration).
Flow of a program that executes in an order, without skipping, jumping or switching to another block of code.
You cannot execute the second instruction before executing the instruction above it. Also, you cannot divide a sum which has not been calculated.
The best examples of selection control structures in Python are if-else statements and dictionary mapping, switcher.
This is where the program takes decisions on a particular criteria. In the above example, if the age is greater than 17 then you can vote or else you can’t vote. The control is given to the code which passes the condition set.
This is when a certain chunk of code needs to be repeated until a given number of times. For example, you need to print the record of all the students on their mark sheet.
Now it’s would be easy to repeat this activity for all students through repetitive or iterative control structure. The most common examples in python are for and while loops.
Similarly, printing student records through a for loop
You can use the control structures individually, nest one flows into another, or combine multiple flows. As the logic becomes more complicated, the structure can get messy, but with a steady trace of the flow can find an execution path.