Latest posts

10/recent/ticker-posts

Looping statements in Python Programming | What is an looping statements | Types of Loops in Python | Control statements of Loops


So now, after the data structures we are going to learn about looping statements in python programming. So before starting, we need to know what is a looping statement and how to use it?

What is a Looping Statement ?

So basically, looping statements are used to iterate a block of code according to the condition or for a fixed amount of time.
It will reduce the rewrite work during the programming.

Looping statements are an essential feature of any programming language, and Python offers several looping statements that can be used to execute a block of code repeatedly. In this blog post, we will explore the different types of looping statements available in Python, how they work, and some of the ways in which they can be used.

Types of Loops available in Python :

In python there 3 types of looping statements are available,

1. for loop
2. while loop
3. Nested loops

1. for loop :

So basically, a for loop is used to iterate over a sequence of elements, such as a list or tuple.
It is totally different than other for-loops of other programming languages. In other programming languages, we can provide a starting point, a condition to be checked, and an increment or decrement operator to iterate the block of code.

But in the case of python's for loop, we can simply provide a range or a sequence to iterate the for loop in either a range or a sequence.

Syntax : 
for variable in sequence:
    # code block to be executed

So here is the basic syntax of python's for-loop.
In this syntax, the "variable" represents the variable that is used to store each element in the sequence as the loop iterates. The "sequence" represents the sequence of elements over which the loop will iterate.
In the next session, we are going to learn the for-loop in detail.

2. while loop :

Similar to the for-loop, while loop is also used to iterate the block of code.
But, the working and the syntax of the while loop are totally different than the for-loop.
So, a while loop follows a condition and iterates a block of code until the given condition becomes false.

It means we don't need to provide any range or any sequence to iterate the block of the while loop.

Syntax :
while condition:
    # code block to be executed

In this syntax, the "condition" represents the expression that is evaluated before each iteration of the loop. If the condition is true, the code block is executed. Otherwise, the loop is terminated.

So the basic working of a while loop depends on the given condition.
At every iteration it will check the condition when the condition is met with the result, then it will go for the next iteration until the condition gets false.

In the next sessions, we are going to learn the while loop in detail.

3. Nested Loops :

A nested loop is a loop inside another loop. This type of loop is useful when you need to iterate over a sequence of elements within another sequence.
Basically, at every iteration of the outer loop, it will iterate the whole inner loop and then goes for the next iteration of the outer loop.

Example :
for i in range(1, 11):
    for j in range(1, 11):
        print(i * j, end='\t')
    print()


So this is a simple example of a nested loop, it will return the tables from 1 to 10.
In this example, the outer loop iterates over the values from 1 to 10, and the inner loop iterates over the values from 1 to 10 for each iteration of the outer loop. The "print" statement then prints the product of the two values.

Loop Control Statements : 

Python also provides several loop control statements that can be used to control the flow of a loop. The three most commonly used loop control statements are:

There are 3 control statements available in Python : 

1. break
2. continue
3. pass

Break: terminates the loop when a certain condition is met.
Continue: skips the current iteration of the loop when a certain condition is met.
Pass: used when a statement is required syntactically but does nothing.

Here is an example of a for loop in Python that uses the break statement to terminate the loop when a certain condition is met:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
    if num == 3:
        break
    print(num)



So in this post, we learned about looping statements in python programming.

I hope it will help you to learn python programming more...

Thank You...!!!




Also Visit


Post a Comment

0 Comments