Introduction to Perl for loop
Perl for loop is the loop that execute the program several times, mainly used in the cases where we need to run or execute certain tasks several times, suitable for small number of iterations where number of iterations is limited and fixed. Perl for loop also known for c style for loop. For loop has two parts, header and body where header is used to indicate the iterations and body will execute the statement based on the iteration.
The statement Perl for loop enables us to loop over a list of elements. You can process each item of the list separately in each iteration. Due to this, the statement for loop is sometimes considered foreach loop. The for and foreach loop in Perl is interchangeable, so you can use the foreach keyword instead of for keyword.
The syntax of per for loop is as follows
for(initialization; condition; increment/decrement)
Statements to be executed;
Parameters of Perl for loop
- Initialization: for loop first executes the initialization part. This section is initialize first and executes only once. It is used to initialize the variable which is used in for loop.
- Condition: This part defines the condition, the execution of the for loop is based on the condition, if the condition is true the loop will execute else will stop the program. That means the loop will iterate untill the condition matches.
- Increment/decrement: The increment and decrement will based on the requirements of the condition and the program. It simply increment or decrement the variable.
Flowchart of Perl for Loop
The flow chart of perl for loop is as follows
How for Loop Works in Perl?
Perl uses for as the keyword to execute the loop. to execute the loop the certain parameters are used like we have discussed in the syntax part – initialization, condition and increment and decrement. First flow control is transferred to initialization to initialize the variable as declared then it transfer control to the condition if the condition is true it will execute the loop else will stop the program. Increment and decrement will used based on the requirements. first loop iterates the statement s then again it will check the condition, if true then again the loop will be executed, the same procedure is followed out untill the condition becomes false.
If user don’t assign the loop with an specific iterator, Perl will use a special variable called the default variable named $ as the iterator. In every iteration, Perl assigns each element of the array @a to the default variable $_.
Examples of Perl for loop
Examples to implement the for loop in perl
Program To print number from 1 to 15
print "List of Numbers from 1 to 15:\n";
for($i = 1; $i <= 15; $i++)
Explanation of the code
Here we have written a program to print the numbers from 1 to 15 using for loop in perl. As we need to print the numbers from 1 to 15 first we have initialized the variable i to 1. The last number is 15. so we have mentioned the condition i <= 15. Here we can also used condition i < 16 which is the same. Here we have initialized i to 1 so to print the values till 15, we need to increase the value, so here we have used increment.
Program to print the input string
print "Please enter a string: ";
my $value = <STDIN>;
for($i = 1; $i <= 5; $i++)
Explanation of the above code
Here we have written a program to get an input string from the user and then print it 5 times. The first program will execute the first statement and will ask user “Please enter a string”. STDIN is used to get a value as a string from the user. In other words, we can say that it enables the user to enter the string according to their need. Here variable value is used to store the value entered by the user. Then the for loop will be executed and the string will be printed. Here we need to print the string 5 times, so the condition is defined accordingly.
Program to print infinite loop
print "Welcome to eduCBA";
Explanation of the above code
Here we have written a program to implement an infinite loop. The double semicolon (;;) is used to implement the infinite loop in Perl. The loop will be executed for infinite times. If you want to stop this infinite loop, you need to use ctrl + C.
This is a guide to Perl for Loop. Here we have discuss the Perl for loop statement to loop over elements of a list and its programming examples. You may also have a look at the following articles to learn more –