Every single program consists of control structures that allow the user to specify the sequence in which different parts of that program are executed. They permit the usage of codes in different sections, based on certain requirements. Some of the simplest forms of control structures include if statements, else statements, and, most notably, loops. These three combine to allow programmers to make decisions and repeat instructions, which makes the code more efficient than it would have been had these features not existed. In this article, we will explore the role of these control structures in Python and how they can help you manage the flow of your programs.
The regular style of "IF" statement is as follows:
This means, the condition can be any statement such as a boolean. In an event that the condition yields a "true" value, the statement or code block in which the "if" statement is will run, but in the event that "False" is given, that portion of the code or code block is been avoided or skipped
As an example, you can explain the age restriction to vote where you can say the age must be 18 or 21 and if so they are then allowed to cast their vote.
This loop is generally used whenever you need to iterate exactly the definite number of times or iterate or work on a set which contains a certain number of elements. If a case arises such as needing to print all items in a list then the loop will be executed as thus:
Let us say for example, you want to print numbers from 1 to 5, you can use a while loop as shown below:
This loop will as long as the variable `count` is less than or equal to 5. With every iteration of the loop, `count` is made to be one more than its previous value. When the condition turns `False`, the loop ends.
Comprehending the "If" Statement
One of the most important program control structures in Python is the if statement. It is designed to make a decision in your code. An if statement works on the logic that there is a certain code block that will run only if a condition that is explicitly stated is met. This is advantageous as it means you can easily make decisions without hardcoding them into the program, but rather using the data or the context of the situation to make the decision.The regular style of "IF" statement is as follows:
Basic If Statement Syntax
if condition:
//code block to be executed if that condition turns to be True
As an example, you can explain the age restriction to vote where you can say the age must be 18 or 21 and if so they are then allowed to cast their vote.
Voting Age Example
age = 20;
if age > = 18:
print("You are eligible to vote")
The "Else" Statement
This value would alternate depending on whichever way you specified. Basically, while the if statement helps you execute some piece of code when a condition becomes true, then the else statement provides such an option which is an opposite to the code of conditions since it allows you to specify a section of code to be executed when the if statement IS n. On the other hand, the presence of the current case double negatives suggests that they are used when both boolean possibilities are required to be specified, that is, this statement is also true when the condition becomes false.Number Check Example
number = -5
if number > = 0:
print("the number is positive")
else:
print("the number is negative")
Grade Evaluation Example
score = 85
if score > = 90:
print("Grade: A")
elif score > = 80:
print("Grade: B")
elif score > = 70:
print("Grade: C")
else:
print("Grade: F")
The Role of Loops
The first two statements rely on conditions but the latter two do the opposite. In other words, the if and its partner the else allow us to branch depending on conditions while the loops are control structures that allow us to repeat a particular block of code. There are two loops that are generally used when programming in Python i.e. loops and while loops. The functions of the two kinds of loops differ but they are all meant to prevent redundancy in carrying out tasks.The "For" Loop
A for loop in Python's programming can be defined as general iteration over sequences like a string or range. It is a common operation to execute a block of code for each element in case there is a certain condition that requires a sequence of execution. The following is its syntax:For Loop Syntax
for item in sequence:
Code block to execute for each item
For Loop Example
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
The "While" Loop
As opposed to the for loops however, the while loops don't necessarily have to have a precondition of running a set number of times or iterating over a certain element. A whole condition solely depends on the satisfaction of the `True` condition stipulated by the user.While Loop Syntax
while expression:
code which is carried out provided the expression evaluates to True
While Loop Example
count = 1
while count <= 5:
print(count)
count += 1
Article
Be the first comment