Loops in Python Programming || Iterators in Python || Python

What are Loops?

 Loops are the programming structure that repeats a set of   instruction till a particular condition is met.

Loop play a vital role in programming and there are numerous of programs which are completely based on loops like Sum, Factorial, Palindrome, Armstrong Number and many more. There are many logics which are baseless without loops.


As you can see, the above Flowchart depicts some basic of the working of loop:

It shows that when the program reaches the loop statements then the loop syntax will not allow the program to execute further till the Requirements are met or satisfied.


How it is different from Conditional Statements ?

In conditional statements either the condition is true or false the program will proceed further or it will print or  execute some statements.

But in Loops till the condition is not fulfilled the program will not proceed further it will be in a continuous cycle inside the syntax.

There are different types of Loops in Programming Language :

- for Loop 

- while Loop

- do-while Loop

- Nested Loops


for Loop:

For loop is a loop which will repeat a block of code till n number of times where n is the range of the loop which can be either pre-decided or during run time. It requires a iterator which is used to initialize the loop.

Syntax:-


for iterator in sequence:
	# loop body	
    
# OR

for iterator in range(range_variable or constant):
	# loop body
    

Example:-

# Program to print a character 5 times
c = str(input("Enter a Character:"))

for i in range(5):
    print(c)

while Loop:

While loop is a loop in which the execution is decided based on boolean condition. It is also known as repeating If statement.

Syntax:-

 while condition:
 	# loop block

Example:-

// Printing Hello World 5 times

c = "Hello World"
n = 5

while n:
    print(c)
    n -= 1


Nested Loop:

Nested loop means a loop inside another loop. In this loop the condition of both the loop has to be satisfied for the further execution. Nested loop can be of different types like "A for loop inside another for loop", "A for loop inside a while loop " and vice-versa," A for loop inside a do-while loop" and vice-versa.

Syntax:-

for iterator in range(range_variable or constant):
	for iterator in range(range_variable or constant):
		# loop body
 

Example:-

n = int(input("Enter the number of rows: "))

for i in range(n):
    for j in range(n):
        print(i+1,end="")
    print()


So, this was all about the loop, different type of loops and their syntaxes and the examples of all the loops mentioned above will be provided in further posts with the help of pattern series and other projects the reason behind this is the better understanding of using loops and their working.


#ENJOY CODING


Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post