![]() |
VOOZH | about |
Loops are used to execute a block of code repeatedly until a condition is met or all items in a sequence are processed. The main types are For loops (iterating over sequences) and While loops (executing code based on a condition).
For loops is used to iterate over a sequence such as a list, tuple, string or range. It executes a block of code once for each item in the sequence.
0 1 2 3
Explanation:
Below is the flowchart of For loop:
A for loop can also iterate through sequence elements using their index values with the help of range() and len().
geeks for geeks
Explanation:
len(a) returns the total number of elements in the list. range(len(a)) generates index values from 0 to 2. a[idx] accesses list elements using their index.While loop repeatedly executes a block of code as long as the given condition remains true. When the condition becomes false, the line immediately after the loop in the program is executed.
Hello Geek Hello Geek Hello Geek
Explanation:
Below is the flowchart of While loop:
An infinite while loop runs continuously because its condition always remains true.
Explanation:
Note: It is suggested not to use this type of loop as it is a never-ending infinite loop where the condition is always true and we have to forcefully terminate the program (or loop execution).
A nested loop is a loop inside another loop. The inner loop executes completely for every iteration of the outer loop.
1 2 2 3 3 3 4 4 4 4
Explanation: