Tutorials Home   >   Programming Basics   >   Loops in Programming

Loops in Programming

1. What are Loops?

In programming, a loop is a control structure that allows a set of instructions to be repeated multiple times until a certain condition is met.

Think of a loop like a repeating task: instead of writing the same code many times, you can use a loop to automate repetition.

Example: Printing numbers 1 to 5. Without a loop, you would write:

print(1)
print(2)
print(3)
print(4)
print(5)

With a loop, it becomes:

for i in range(1, 6):
print(i)

2. Why Use Loops?

Loops are important because they:

  1. Save time and effort – Write once, repeat many times.

  2. Handle repetitive tasks efficiently – Process lists, files, or user input.

  3. Make programs dynamic – Execute code based on conditions or data size.

  4. Reduce errors – Less code duplication means fewer mistakes.


3. Types of Loops

There are two main types of loops used in most programming languages:

  1. For Loop – Repeats code a specific number of times.

  2. While Loop – Repeats code as long as a condition is true.

Some languages also have do-while loops, which execute the loop at least once before checking the condition (common in Java, C++).


4. For Loop

A for loop is used when you know how many times you want to repeat a block of code.

Python Example:

# Print numbers from 1 to 5
for i in range(1, 6):
print(i)
  • range(1, 6) generates numbers 1 to 5.

  • i takes each value from the range, one at a time.

Java Example:

// Print numbers from 1 to 5
for(int i = 1; i <= 5; i++){
System.out.println(i);
}

4a. Iterating Over Collections

For loops can also iterate over lists, arrays, or strings.

Python Example:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

Java Example:

String[] fruits = {"apple", "banana", "cherry"};
for(String fruit : fruits){
System.out.println(fruit);
}

5. While Loop

A while loop executes a block of code as long as a condition is True.

Python Example:

i = 1
while i <= 5:
print(i)
i += 1 # Increment i to avoid infinite loop

Java Example:

int i = 1;
while(i <= 5){
System.out.println(i);
i++;
}

Key point: Always update variables used in the condition to prevent infinite loops.


6. Do-While Loop (Java/C++)

A do-while loop executes the code at least once, then checks the condition.

Java Example:

int i = 1;
do {
System.out.println(i);
i++;
} while(i <= 5);

7. Controlling Loops

a) Break Statement

The break statement stops the loop immediately, even if the condition is still True.

Python Example:

for i in range(1, 10):
if i == 5:
break
print(i)

Output: 1 2 3 4

Java Example:

for(int i = 1; i < 10; i++){
if(i == 5){
break;
}
System.out.println(i);
}

b) Continue Statement

The continue statement skips the current iteration and moves to the next.

Python Example:

for i in range(1, 6):
if i == 3:
continue
print(i)

Output: 1 2 4 5

Java Example:

for(int i = 1; i <= 5; i++){
if(i == 3) continue;
System.out.println(i);
}

c) Nested Loops

Loops can be placed inside another loop. This is called nested loops.

Python Example: Multiplication Table

for i in range(1, 4):
for j in range(1, 4):
print(i, "*", j, "=", i*j)
print() # Empty line after each table

Java Example:

for(int i = 1; i <= 3; i++){
for(int j = 1; j <= 3; j++){
System.out.println(i + "*" + j + "=" + (i*j));
}
System.out.println();
}

8. Infinite Loops

A loop becomes infinite if the exit condition is never met.

Python Example:

while True:
print("This will print forever")

Java Example:

while(true){
System.out.println("Infinite loop");
}

Infinite loops are sometimes useful for servers or games, but usually need a break condition.


9. Best Practices for Loops

  1. Avoid infinite loops unless intended.

  2. Update loop variables properly.

  3. Use meaningful variable names (i, j are okay for counters, but descriptive names are better in complex loops).

  4. Break down complex loops using functions or comments.

  5. Use nested loops carefullyβ€”they can slow down programs if overused.


10. Summary

Loops are essential for repeating tasks in programming.

  • For loops: repeat a fixed number of times.

  • While loops: repeat while a condition is True.

  • Do-while loops: repeat at least once (Java/C++).

  • Break and continue: control loop execution.

  • Nested loops: loops inside loops for complex repetition.

  • Infinite loops: repeat endlessly unless a break condition is added.

Loops save time, reduce errors, and make programs dynamic, and understanding them is critical for efficient programming.