Home
Posts
Article
Encyclopedia
Garden
Set
English

Control Structures: If, Else, and Loops

养花风水
23 Views
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.

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

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
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:

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
Let us say for example, you want to print numbers from 1 to 5, you can use a while loop as shown below:

While Loop Example

  count = 1
while count <= 5:
print(count)
count += 1
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.
0
0
Article
comment
😀 😁 😂 😄 😆 😉 😊 😋 😎 😍 😘 🙂 😐 😏 😣 😯 😪 😫 😌 😜 😒 😔 😖 😤 😭 😱 😳 😵 😠
* Only support image type .JPG .JPEG .PNG .GIF
* Image can't small than 300*300px
Be the first comment
Just Reply
Elite Article
FeedBack

You have any problems or suggestions, please leave us a message.

Please enter content
Set
VIP
Sign out
Share

Share good articles, GFinger floral assistant witness your growth.

Please go to the computer terminal operation

Please go to the computer terminal operation

Forward
Insert topic
Remind friend
Post
/
Submit success Submit fail Picture's max size Success Oops! Something wrong~ Transmit successfully Report Forward Show More Article Help Time line Just Reply Let's chat! Expression Add Picture comment Only support image type .JPG .JPEG .PNG .GIF Image can't small than 300*300px At least one picture Please enter content