Loops are fundamental constructs in programming that allow you to execute a block of code repeatedly based on a condition. In Perl, there are several types of loops, each suited for different scenarios. This section will cover the following types of loops:

  1. while loop
  2. until loop
  3. for loop
  4. foreach loop
  5. Loop control statements (next, last, redo)

  1. while Loop

The while loop executes a block of code as long as the specified condition is true.

Syntax

while (condition) {
    # code to be executed
}

Example

my $count = 0;

while ($count < 5) {
    print "Count is: $count\n";
    $count++;
}

Explanation

  • The loop starts by checking the condition $count < 5.
  • If the condition is true, the code inside the loop is executed.
  • The variable $count is incremented by 1 in each iteration.
  • The loop continues until $count is no longer less than 5.

  1. until Loop

The until loop is the opposite of the while loop. It executes a block of code as long as the specified condition is false.

Syntax

until (condition) {
    # code to be executed
}

Example

my $count = 0;

until ($count >= 5) {
    print "Count is: $count\n";
    $count++;
}

Explanation

  • The loop starts by checking the condition $count >= 5.
  • If the condition is false, the code inside the loop is executed.
  • The variable $count is incremented by 1 in each iteration.
  • The loop continues until $count is greater than or equal to 5.

  1. for Loop

The for loop is used when you know in advance how many times you want to execute a statement or a block of statements.

Syntax

for (initialization; condition; increment) {
    # code to be executed
}

Example

for (my $count = 0; $count < 5; $count++) {
    print "Count is: $count\n";
}

Explanation

  • The loop starts with the initialization my $count = 0.
  • It checks the condition $count < 5.
  • If the condition is true, the code inside the loop is executed.
  • The variable $count is incremented by 1 in each iteration.
  • The loop continues until $count is no longer less than 5.

  1. foreach Loop

The foreach loop is used to iterate over a list of values.

Syntax

foreach my $item (@array) {
    # code to be executed
}

Example

my @numbers = (1, 2, 3, 4, 5);

foreach my $num (@numbers) {
    print "Number is: $num\n";
}

Explanation

  • The loop iterates over each element in the array @numbers.
  • For each iteration, the current element is assigned to the variable $num.
  • The code inside the loop is executed for each element in the array.

  1. Loop Control Statements

Perl provides several control statements to alter the flow of loops:

  • next: Skips the rest of the current iteration and moves to the next iteration.
  • last: Exits the loop immediately.
  • redo: Restarts the current iteration without evaluating the loop condition.

Examples

next

for (my $count = 0; $count < 5; $count++) {
    next if $count == 2;
    print "Count is: $count\n";
}

Output:

Count is: 0
Count is: 1
Count is: 3
Count is: 4

last

for (my $count = 0; $count < 5; $count++) {
    last if $count == 2;
    print "Count is: $count\n";
}

Output:

Count is: 0
Count is: 1

redo

my $count = 0;

while ($count < 5) {
    $count++;
    redo if $count == 2;
    print "Count is: $count\n";
}

Output:

Count is: 1
Count is: 3
Count is: 4
Count is: 5

Practical Exercises

Exercise 1: Sum of Numbers

Write a Perl script that calculates the sum of numbers from 1 to 10 using a for loop.

Solution

my $sum = 0;

for (my $i = 1; $i <= 10; $i++) {
    $sum += $i;
}

print "The sum is: $sum\n";

Exercise 2: Print Even Numbers

Write a Perl script that prints all even numbers between 1 and 10 using a while loop.

Solution

my $num = 1;

while ($num <= 10) {
    if ($num % 2 == 0) {
        print "$num\n";
    }
    $num++;
}

Exercise 3: Iterate Over an Array

Write a Perl script that iterates over an array of strings and prints each string using a foreach loop.

Solution

my @words = ("apple", "banana", "cherry");

foreach my $word (@words) {
    print "$word\n";
}

Common Mistakes and Tips

  • Infinite Loops: Ensure that the loop condition will eventually become false. For example, forgetting to increment the loop variable can lead to an infinite loop.
  • Off-by-One Errors: Be careful with loop boundaries. For example, using < instead of <= can result in one less iteration than intended.
  • Using next, last, and redo: These control statements can make loops more flexible but also more complex. Use them judiciously to maintain readability.

Conclusion

In this section, you learned about different types of loops in Perl, including while, until, for, and foreach loops. You also explored loop control statements like next, last, and redo. Understanding and using loops effectively is crucial for writing efficient and readable Perl programs. In the next module, we will delve into subroutines, which allow you to encapsulate and reuse code.

© Copyright 2024. All rights reserved