Control structures are fundamental in any programming language as they allow you to control the flow of your program. In Perl, control structures include conditional statements and loops. This section will cover the following topics:
- Conditional Statements
- Looping Constructs
- Practical Examples
- Exercises
- Conditional Statements
Conditional statements allow you to execute code based on certain conditions. Perl supports several types of conditional statements:
if Statement
The if
statement executes a block of code if a specified condition is true.
else Statement
The else
statement executes a block of code if the condition in the if
statement is false.
my $number = 3; if ($number > 5) { print "The number is greater than 5\n"; } else { print "The number is not greater than 5\n"; }
elsif Statement
The elsif
statement allows you to check multiple conditions.
my $number = 5; if ($number > 5) { print "The number is greater than 5\n"; } elsif ($number == 5) { print "The number is equal to 5\n"; } else { print "The number is less than 5\n"; }
unless Statement
The unless
statement is the opposite of the if
statement. It executes a block of code if the condition is false.
- Looping Constructs
Loops allow you to execute a block of code multiple times. Perl supports several types of loops:
while Loop
The while
loop executes a block of code as long as the condition is true.
until Loop
The until
loop is the opposite of the while
loop. It executes a block of code as long as the condition is false.
for Loop
The for
loop is used to execute a block of code a specific number of times.
foreach Loop
The foreach
loop is used to iterate over a list of values.
- Practical Examples
Example 1: Using if-else with User Input
print "Enter a number: "; my $input = <STDIN>; chomp($input); if ($input > 10) { print "The number is greater than 10\n"; } else { print "The number is 10 or less\n"; }
Example 2: Looping through an Array
my @fruits = ('apple', 'banana', 'cherry'); foreach my $fruit (@fruits) { print "I like $fruit\n"; }
- Exercises
Exercise 1: Simple if-else
Write a Perl script that asks the user for their age and prints whether they are a minor (under 18), an adult (18-64), or a senior (65 and older).
Solution:
print "Enter your age: "; my $age = <STDIN>; chomp($age); if ($age < 18) { print "You are a minor.\n"; } elsif ($age <= 64) { print "You are an adult.\n"; } else { print "You are a senior.\n"; }
Exercise 2: Loop with Condition
Write a Perl script that prints the numbers from 1 to 10, but skips the number 5.
Solution:
Exercise 3: Nested Loops
Write a Perl script that prints a multiplication table for numbers 1 through 5.
Solution:
for (my $i = 1; $i <= 5; $i++) { for (my $j = 1; $j <= 5; $j++) { print $i * $j, "\t"; } print "\n"; }
Conclusion
In this section, you learned about control structures in Perl, including conditional statements and loops. These constructs are essential for controlling the flow of your programs. Make sure to practice the exercises to reinforce your understanding. In the next module, we will dive into variables and data types in Perl.