Jump to content

3.3.2 Looping Constructs (for, while, do-while)

From Computer Science Knowledge Base
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

3.3.2 Looping Constructs (for, while, do-while)

Imagine you need to count from 1 to 100, or you need to send an email to everyone on a list. Doing this manually, one by one, would be super boring and take forever!

Looping Constructs (also called loops) are programming instructions that allow your program to repeat a block of code multiple times. They are incredibly powerful for automating repetitive tasks.

There are different types of loops, each useful for slightly different situations:

  • for loop:
    • This loop is best when you know exactly how many times you want to repeat something.
    • It's like saying: "Repeat this action 10 times."

Example: "Count from 1 to 5."

FOR (number from 1 to 5)

   Print the number.

In code (Python example):

for i in range(1, 6): # range(1, 6) means numbers 1, 2, 3, 4, 5

   print(i)

# Output:

# 1

# 2

# 3

# 4

# 5

  • for loops are also great for going through each item in a list (like a list of names or numbers).
  • while loop:
    • This loop is best when you want to repeat something AS LONG AS a certain condition is true. You don't necessarily know exactly how many times it will run beforehand; it just keeps going until the condition becomes false.
    • It's like saying: "WHILE the light is red, WAIT. Once it's green, GO."

Example: "Keep asking for a password until the correct one is entered."

WHILE (password is NOT correct)

   Ask for password again.

In code (Python example):

password = ""

while password != "secret":

   password = input("Enter the password: ")

print("Access granted!")

  • Caution: Be careful with while loops! If the condition inside the while loop never becomes false, the loop will run forever, causing your program to get stuck (this is called an "infinite loop").
  • do-while loop (in languages like Java, C++, C#):
    • This loop is similar to a while loop, but with one key difference: it ALWAYS runs at least once, and then it checks the condition.
    • It's like saying: "DO this action AT LEAST ONCE. THEN, WHILE the condition is true, keep doing it."

Example (Java concept):

int count = 0;

do {

   System.out.println("Count is: " + count);

   count++;

} while (count < 3); // Condition checked AFTER the first run

// Output:

// Count is: 0

// Count is: 1

// Count is: 2

  • (Note: Python does not have a direct do-while loop, but you can achieve similar logic using a while True loop with a break statement inside.)

Loops are fundamental for making programs efficient and able to handle large amounts of data or repeat tasks without needing you to write the same line of code over and over again.

Bibliography:

  • W3Schools - Python While Loops: https://www.w3schools.com/python/python_while_loops.asp
  • GeeksforGeeks - Loops in C/C++: https://www.geeksforgeeks.org/loops-in-c-cpp/
  • Wikipedia - Loop (computing): https://en.wikipedia.org/wiki/Loop_(computing)