A program construct that features a looping branch of code is typically referred to as a loop. Loops allow a section of code to be executed repeatedly based on a specified condition. There are several types of loops commonly found in programming:
-
For Loop: A loop that runs a specific number of times. It often uses a counter to control the number of iterations.
for i in range(5): print(i)
-
While Loop: A loop that continues to execute as long as a specified condition remains true.
i = 0 while i < 5: print(i) i += 1
-
Do-While Loop: A variation of the while loop that executes the loop body at least once before checking the condition at the end.
do { console.log(i); i++; } while (i < 5);
-
Enhanced For Loop (or For-Each Loop): This type of loop is used to iterate over elements in a collection or array.
for (String item : list) { System.out.println(item); }
All of these constructs enable repeated execution of code, making them essential for tasks that require iteration, such as processing arrays, lists, or handling repetitive user input.