Chapter 5 - Loop Types
In programming, loops are a fundamental construct used to repeat a block of code multiple times, either for a fixed number of iterations or while a specific condition holds true. Loops are essential for automating repetitive tasks, iterating over collections of data, and implementing algorithms that require repeated processing. By reducing the need for manual repetition, loops help improve code efficiency, readability, and maintainability.
Below is a concise description of the loop types available in Python, PHP, C++, Zig, and Go.
Python
Python provides three primary types of loops:
for
Loop:Iterates over a sequence (like a list, tuple, string, or range).
Example:
for i in range(5): print(i)
while
Loop:Continues execution as long as a given condition is
True
.Example:
i = 0 while i < 5: print(i) i += 1
Nested Loops:
Loops within loops.
Example:
for i in range(3): for j in range(2): print(f"i: {i}, j: {j}")
Python also provides constructs like break
, continue
, and else
(executed when the loop finishes without being interrupted by break
).
PHP
PHP supports several loop types:
for
Loop:Iterates using a counter.
Example:
for ($i = 0; $i < 5; $i++) { echo $i . "\n"; }
while
Loop:Executes while a condition is
true
.Example:
$i = 0; while ($i < 5) { echo $i . "\n"; $i++; }
do-while
Loop:Executes the block at least once before checking the condition.
Example:
$i = 0; do { echo $i . "\n"; $i++; } while ($i < 5);
foreach
Loop:Iterates over arrays or objects.
Example:
$arr = ["a", "b", "c"]; foreach ($arr as $value) { echo $value . "\n"; }
PHP also provides break
and continue
for controlling loop flow.
C++
C++ offers a range of looping constructs:
for
Loop:Standard counter-based iteration.
Example:
for (int i = 0; i < 5; i++) { std::cout << i << std::endl; }
while
Loop:Repeats while a condition is true.
Example:
int i = 0; while (i < 5) { std::cout << i << std::endl; i++; }
do-while
Loop:Executes at least once.
Example:
int i = 0; do { std::cout << i << std::endl; i++; } while (i < 5);
Range-Based
for
Loop:Simplified iteration over containers (introduced in C++11).
Example:
std::vector<int> nums = {1, 2, 3}; for (int num : nums) { std::cout << num << std::endl; }
Nested Loops:
Loops inside loops.
Example:
for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { std::cout << "i: " << i << ", j: " << j << std::endl; } }
C++ also supports break
and continue
for loop control.
Zig
Zig offers loops with a slightly different syntax and approach:
while
Loop:Executes as long as a condition is true.
Example:
var i: i32 = 0; while (i < 5) : (i += 1) { std.debug.print("{d}\n", .{i}); }
for
Loop:Iterates over collections or ranges.
Example:
const items = [_]i32{1, 2, 3}; for (items) |item| { std.debug.print("{d}\n", .{item}); }
inline for
Loop:Compact loop syntax often used in compile-time or single expressions.
Example:
inline for (std.range(0, 5)) |i| { std.debug.print("{d}\n", .{i}); }
Labeled Loops:
Supports labeling for breaking out of nested loops.
Example:
var i: i32 = 0; outer: while (i < 5) : (i += 1) { if (i == 3) break :outer; std.debug.print("{d}\n", .{i}); }
Go
Go provides several looping constructs, primarily centered around the for
loop, which can be adapted to cover the functionality of other common loop types. Go does not have a while
or do-while
loop, but these can be implemented using for
.
for
Loop:The standard
for
loop in Go is similar to other languages, with initialization, condition, and post-expression.Example:
package main import "fmt" func main() { for i := 0; i < 5; i++ { fmt.Println(i) } }
while
-like Loop:Go uses the
for
loop without an initialization or post-expression to mimic awhile
loop.Example:
package main import "fmt" func main() { i := 0 for i < 5 { fmt.Println(i) i++ } }
Infinite Loop:
Omitting all three components of the
for
loop results in an infinite loop.Example:
package main import "fmt" func main() { i := 0 for { if i >= 5 { break } fmt.Println(i) i++ } }
Range-based
for
Loop:Iterates over elements of arrays, slices, maps, or strings.
Example:
package main import "fmt" func main() { nums := []int{1, 2, 3} for index, value := range nums { fmt.Printf("Index: %d, Value: %d\n", index, value) } }
Go also supports break
and continue
for controlling loop flow.
Each language offers flexibility and functionality through these loops, and understanding their syntax and applications is crucial for efficient programming.