Python Basics: Intro to Loops
Published: 2021-01-02 20:17:11

In general Computer, Programs are executed sequentially. It will execute the first line then the second line than 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 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. And 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 Languages **For Loops** are used for sequential traversal. In **Python**, we use *For 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 word loop inside a loop. We can put any loop inside any loop for example we can put **For Loop** inside **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 also 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* which 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* which just skips the execution of the code which 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 in near future.

#### Syntax:-
#### An empty loop
```
for letter in 'coderaman07':
	pass
print(f'Last Letter :{letter}')
```

#### Output:-
```
Last Letter : 7
```

<br /><br /><br />

#### So wrapping this Lesson up we had a brief insight of For loop, While loops, and also 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 to ask questions on any topics just Tag me on Twitter or Instagram or Facebook with my username [coderaman07](https://linktr.ee/coderaman07). I will be happy to help you out.

For Development Related Stuff Hit on to my Website for Open Source Software's [RushX](https://rushx.pythonanywhere.com)

Also, join my email list for weekly updates [coderaman07](https://coderaman07.ck.page)

Also, you can visit my Portfolio website for any Full-time Job as I am going to Graduate this coming April. You can go to my Portfolio website by clicking here [https://amanojha.me](https://amanojha.me)