What is the “For” loop?
The “For” Loop is a commonly used control structure in programming that allows us to repeat a set of actions multiple times. The “For” loop is a compact way of writing a repetitive process, allowing us to perform a set of actions multiple times on a given data structure.
The For loop's basic syntax consists of three parts:
- Initialization: The starting value of the loop is set.
- Condition: If this is true, the loop will continue to execute.
- Increment/Decrement: The value of the loop variable is updated each time the loop iterates.
For loops can iterate over arrays, lists, and other data collections. In each iteration of the loop, the current value of the loop variable is used as the index for accessing the elements of the collection. This makes for loops an efficient way to perform operations on each element in a collection.
Suppose we want to print the numbers 2-13. In Python, we would write it like this:
#for loop program
for i in range(2, 13):
The value of “I” will be printed in each iteration of this loop's 11 iterations. The range() function returns a number sequence from the start value to the end value.
What is the “While” loop?
In the “While” loop, if the provided condition is true, a block of code is continually executed. In Python, the general syntax for a while loop is:
The code block will be executed until the condition is satisfied. If the condition is initially false, the code block will be skipped.
count = 1
while count <= 5:
count += 1
This code will print the numbers 1 to 5, as the condition count <= 5 is true, and the count is incremented by 1 in each iteration of the loop. It is important to make sure that the condition will eventually become false, otherwise, the loop will continue indefinitely, creating an infinite loop.
A while loop is a useful tool when you need to perform a task repeatedly while a certain condition is met. You can use the while loop to control the flow of the program and make decisions based on conditions.
In a while loop, the condition is evaluated before each iteration, so if the condition becomes false, the loop will stop and the control will be transferred to the next line after the loop. It's possible to use multiple conditions in the while loop, just like in the “if” statement, by using logical operators such as “and, or, and not”.
In some cases, you might need to break out of the loop early, before the condition becomes false. This is possible with the break statement.
count = 1
while count <= 10:
if count == 5:
count += 1
Difference Between For Loop and While Loop
- A for loop is used for iterating over a range or sequence of items and executing a block of code for each item. On the other hand, a while loop is used to execute a block of code repeatedly as long as a specified condition is true. The key difference is that for loop is designed to run a predetermined number of times, while the while loop will continue to run as long as the condition is met.
- When you need to repeat a specific block of code a certain number of times, for loops come in scene. They are also useful when working with lists, arrays, or other data structures where the number of iterations is known ahead of time. The syntax of a for loop typically includes an initialization statement, a condition that is evaluated before each iteration, and an increment or decrement statement that is executed after each iteration.
- The use of while loops, on the other hand, is ideal when it is impossible to predict how many iterations would be required. Typically, they are employed to repeatedly run a piece of code until a particular condition is satisfied. A while loop's syntax includes a condition that is tested before each iteration, and the loop will keep running as long as the condition is still true.