Intro To Loops

Intro To Loops

Introduction to Python Loops and it's logic how it works

ยท

4 min read

In general Computer, Programs are executed sequentially. It will execute the first line then the second line then the third line and so on.

Programming Languages nowadays also provide various control statements that allow us to control the execution of a Program.

There may be sometimes you want to execute a particular amount of code multiple times ( Fixed Number of times ) or maybe a Dynamic number of times.

and to solve the problem of executing the limited number of lines of code multiple numbers of times Python Programming Language provides multiple Loops to solve the problem of executing multiple lines multiple times.

There are majorly 3 kinds of Loops in Python:-

  • While Loops

  • For Loops

  • Nested Loops

1. While Loops

In Python, while loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false the loop is terminated and the program continues to execute code after the while loop.

Format of While Loop:-

while expression:
    statement(s)

For Example:-

Python program to illustrate while loop
count = 0
while (count < 3):
count = count + 1
print("Hello Geek")

2. For Loops

In any Programming Language For Loops are used for sequential traversal. In Python, we use each loop just like we have in another programming language, unlike C or C++.

Syntax:-

for iterator_var in sequence:
    statements(s)

For Example:-

n = 4
for i in range(0, n):
    print(i)

3. Nested Loops

Nested Loops are termed to those Loops which are used inside other loops in other words loop inside a loop. We can put any loop inside any loop for example we can put For Loop inside the while loop.

Syntax

for iterator_var in sequence:
    for iterator_var in sequence:
        statements(s)
        statements(s)

In any Programming Language, we have loop control statements to control the execution of the loop as we want to

We have 3 types of Loop Control Statements

  • break

  • continue

  • pass

1. Break

It is a simple keyword or token that brings the control out of the loop and executes the rest of the program code.

Syntax:-

# Code to display the use case of break statements
# this code will break out of the for loop as soon it finds 'e' or 'r'

for letter in 'coderaman07':
    if letter == 'e' or letter == 'r':
        break

print(f'Current Letter :{letter}')

Output:-

Current Letter : e

2. Continue

It is a keyword or token that just skips the execution of the code that was meant to be executed for all.

Syntax:-

Prints all letters except 'e' and 's'
for letter in 'coderaman07':
    if letter == 'e' or letter == 'r':
        continue
    print(f'Current Letter :{letter}')
    var = 10

Output:-

Current Letter :c
Current Letter :o
Current Letter :d
Current Letter :a
Current Letter :m
Current Letter :a
Current Letter :n
Current Letter :0
Current Letter :7

3. Pass

It is an empty Control statement that is often used to describe that the code here has to be written here shortly.

Syntax:-

An empty loop

for letter in 'coderaman07':
    pass
print(f'Last Letter :{letter}')

Output:-

Last Letter : 7

So wrapping this Lesson up we had a brief insight into For loop, While loops, and Nested Loops with 3 control statements.

Hope to see you in the next Blog Post for more Python Stuff and a codebase to code something best out of these blog posts.

If you feel like asking questions on any topics just Tag me on Twitter Instagram or Facebook with my username coderaman07. I will be happy to help you out.

For Development Related Stuff Hit on to my Website for Open Source Software's RushX

This article was originally written back when I was starting but as a private article, I was afraid of building in Public but now Let's do this.

So my Web App which I am rebuilding is available on justaman045.vercel.app you can visit it now also but it's currently under heavy maintenance and the look and feel will change in about 2 or 3 weeks.

Promotion

Currently, I am also focused on building the Community mentioned below:

  1. Anime Community:- Instagram/otakuanime69

  2. Motivational Theme Page:- Instagram/glorymotivation7

also, I'm currently focused on building my Brand named Otaku Outfits currently it's in the approval stage on Etsy but once it is live I'll be sharing it with you all by making sure to subscribe to my Newsletter from the Newsletter tab.

Also, it'll be helpful if you can just follow this link to Medium and give me a like and follow there too.

Contact

To contact you can join my Discord Server where I intend to keep the community in one place and also serve the community
Discord Link:- https://discord.gg/ZfAKPZvT ( It's brand new and I'm okay if you help me out on setting up this ).

Thanks for Reading
Happy Hacking

ย