While a loop in python is a loop where we check the condition and then execute the set of code and the most important event comes is increments or decrements of the variable where the condition is checked, Most of the initiate loop runs when there are no increments or decrements.
Loops in Pythons
Loops in pythons reference to the algorithm where a set of fixed lines of code need to execute multiple times until the given/provided condition is true.
Types Of Loops
There are three types of loop statements
It is a loop where first we check the condition and then execute the body. However, all three things are written in a single line
fruits = ["apple", "banana", "cherry"] for x in fruits: print(x)
here for loop will execute till the last element of the tuple.
While loop is also similar to for loop where first we check the condition then execute the body of the loop and then increment.
i = 1 while i < 6: print(i) i += 1
here we can say that here first check the condition and then execute the body of the loop if the condition is false then there will not be any execution of the body of the loop as the condition check is the first step to satisfy.
It is the same as for the loop where the first thing to do is check the condition provided and then execute the body of the loop and then update the variable to execute and check the condition again.
1. We need to keep the most important thing in mind which is an increment of variables because without increment the loop will execute for infinite times. And could lead to the failure of the program as it will run for an infinite time. 2. Another condition when the loop will run for infinite times is the condition given for check will always be true in that case also the program will run for infinite times 3. Another case of the infinite loop could be the checking of the condition is different from the increment or decrement that for checking the condition is in a different manner and updating is in a different direction. And due to that the condition will always be true.
Do While Loop
Do while is also a type of loop with a different quality in this loop the first thing that happens is entering the loop body and executing after that the condition check happens.
So we can conclude that if the condition provided is false or true at least once the body of the loop definitely be executed. That is used at certain conditions where at least once the code needs to execute whatever the condition is.
Do while loop also needs to update the variable as per the given situation otherwise, the loop will execute for infinite times which is again a threatening condition for the program to execute.
Learn More about Loops: Some Basic List Programs-1
Learn More about While loop While Loop