As Developer’s, it is of utmost importance that we know how to control the flow of our code. And in Python, we have operators, conditional statements, loops, switch statements, nested if statements and a lot more that help us achieve this. Therefore, we need to study and practice these statements.
OPERATORS
An operator is a symbol that tells Python to perform a certain operation. These operations could be mathematical, logical and comparison. Operators most times work on two values.
1)Mathematical operators: They are used for simple and complex calculations. There are four mathematical operators. They are:
a)Addition or Plus operator: It is used to add two or more values together. It’s symbol is “+”.
b)Subtraction or Minus operator: It is used to subtract two or more values. It’s symbol is “-“.
c)Division Operator: It is used to divide values. Its symbol is “/”.
d)Multiplication Operator: It is used to multiply two or more values. Its symbol is “*”, the asterisk symbol.
2)Logical Operators: They are used to determine a true or false outcome in Python on conditional statements. They include:
a)The “and” operator: This operator checks for all condition to be true.
b)The “or” operator: The operator checks for at least one condition to be true.
c)The “not” operator: The operator returns a false value if the result is true.
Operators are usually combined or used with conditional statements to control the flow of a program that meets specific criteria.
CONTROL FLOW
As Developer’s, it is very important to know how to use conditional statements to control the flow of your program.
What is control flow and what are conditional statements?
The order in which instructions are executed in a program is referred to as CONTROL FLOW. There is always a decision to be made in every program. As a result, the program will take different actions or directions. In Python, there are two types of control flows:
1)Conditional statements: These include if, else and elif or else if.
a)The “if” keyword states that if a condition is met or proves to be true then a function is to be performed.
b)The “else” keyword is a second option to the “if” keyword. If a condition is not met in the “if” function, then an “else” keyword states what is to happen in the situation.
c)The “elif” keyword is a way of Python saying that if the previous conditions were not met, then you can run the “elif” function.
2)Loops: These includes the for loop and the while loop.
a)The “for” loop checks for specific conditions and as long as these conditions are met, repeatedly executes a block of code.
b)The “while” loop repeats a specific block of code an unknown number of times until a condition is met.
SWITCH STATEMENTS
The Switch statement gives you the ability to test a variable against many conditions. The “if” statement or “else if” statement does something similar but can only be used to test a variable against few conditions. And as such, the switch statement is more advantageous. Using a switch statement, you can achieve cleaner, more readable code that allows all the same functionality as the “if” statement but better and more efficient. You can also combine several conditions by using the “or” operator when using a switch statement.
LOOPING CONSTRUCTS
The action of repetition is known as looping. Looping is used to iterate through a sequence and access each item in the sequence. As mentioned earlier, Python has two types of looping constructs for iterating through sequence. These are the For loop and While loop. A For loop is used to repeat a block of code a known or specified number of times WHILE a While loop repeatedly executes a block of code until the condition is satisfied. While loop is best used when one needs to perform a repeated operation but doesn’t know how many iterations is needed and as such cannot specify.
For loop
The For loop makes working with any sequence in Python easier. It is based on the size or the length of the items to iterate over.
While loop
The While loop is based upon a condition being true. If the condition becomes no longer true, then the loop stops. Therefore, the number of iterations is not known ahead of time as in a For loop.
Practicing control flow and loops
If else: There will be times where you may need to search for a particular item in a list and when the item is found, you do not need to continue looping through the other items. To achieve this, you use the IF ELSE statement. The IF is used to specify the item you want to make stand out and the keyword ELSE is used to handle the case if the item is not chosen or found by the user.
Break: The BREAK statement is used to stop the loop which in turn also stops the ELSE condition. Without this looping control statement known as the BREAK, the loop will continue even after the IF condition is satisfied.
Continue: Very similar to a BREAK statement but the difference is that it can allow you to skip over a section of loop but then continue with the rest.
Pass: This looping control statement allows you to run through the loop entirely and effectively ignore that the IF condition has been fulfilled or satisfied.
NESTED LOOPS
Loops can be nested meaning you can have one loop inside another loop which can be known as the main loop. Python nested loops can be used to solve problems that are more complex. The nested for loop is written by indentation inside the outer loop.
How does a nested loop work?
It starts with the main or outer loop, then steps into the inner loop. The inner loop will then run until its range limit is met. Once the inner loop runs to completion, it will come back to the outer loop for the next iteration and then step into the inner loop again and keeps running until the outer loop’s range limit is met.
CONCLUSION
You now know about control flows, operators, looping constructs and nested loops and how to use them. They are all vital parts to your Python code as a Developer in fact you will make use of them very often to solve problems. It is therefore important to practice often to get very familiar with them. Good luck.