Updated April 5, 2023
Introduction to Perl while Loop
Perl while loop is basically used for iterate the part of program or execute the statements of the program multiple times. In while loop we have given the condition before the code statement, condition is given before the statement of code to be executed in the program. In while loop when program execution will start the compiler first checks the given code condition is true or false if the given condition is true then loop statement will execute. If the given condition is false while loop will terminate from the loop.
Syntax:
Given below is the syntax of while loop in perl:
While (Condition) -- Condition which was used in while loop.
{
# Program or code to be executed using while loop in perl.
}
While (Condition) -- Condition which was used in while loop.
{
Statements; - - Statement to be executed using while loop in perl.
}
Below is the parameter description syntax of perl while loop:
- While: While loop is very important and useful in perl to iterate the statement of the code multiple times. While loop will repeatedly execute the statement in while condition. It will repeatedly execute the statement until the condition is not get false.
- Condition: Condition is the important parameter of while loop in perl. Condition is defined as which condition we have used to execute the loop. We can also use true or false condition in while loop. If we have used true condition then while loop executes the statement which we have used in program code and if we have used false condition then loop will terminate from the loop.
- Statement: Statement is used to execute the code under the while statement. Statement is nothing but the code under the while loop.
Flowchart
- Flowchart is nothing but a step by step execution of program code.
- Below figure shows flowchart of while loop in perl.
- Flowchart is very important to find the step by step execution of while loop that how it is work in program statements.
- Above figure shows the pictorial representation or flow of while loop in program code. How while loop is working its shows in flowchart.
- Flowchart of while loop starts with keyword name as start and end with the name as stop or we can also end the flowchart with name as end keyword.
- After starting of the flowchart, compiler will execute the block of while loop condition. If the condition is true then while loop executes the statement which we have used in program code and if we have used false condition then while loop will terminate from the loop.
- If the condition is true then compiler will execute the statement or block of code again and again until while loop condition is not getting failed.
- If the condition is fail then loop is terminated from the condition and executes the next statement of the program.
- Flowchart is very important to display the pictorial flow of any program or code in perl language.
How while Loop works in Perl?
- While loop will repeatedly execute the statement in while condition. It will repeatedly execute the statement until the condition is not false.
- While loop is very important and useful in perl to iterate the statement of the code multiple times.
- If we are using true keyword in condition then while loop will go into the infinite loop.
Below example show that loop goes into the infinite loop while using true keyword in loop condition.
Example:
Code:
while(true) ## Loop goes into the infinite condition.
{
printf"Using true\n";
}
Output:
- If we are using false keyword in condition then while loop will go into the infinite loop.
Below example show that loop goes into the infinite loop while using false keyword in loop condition.
Example:
Code:
while(false) ## Loop goes into the infinite condition.
{
printf"Using false\n";
}
Output:
- In while loop we have given the condition before the code statement, condition is given before the statement of code to be executed in the program.
- In while loop when program execution will start the compiler first checks the given code condition is true or false if the given condition is true loop or loop statement will execute.
- If the given loop condition is false then while loop will terminate from the loop.
- Perl while loop is basically used for iterate the part of program or execute the statements of the program multiple times.
Examples of Perl while Loop
Given below are the examples mentioned:
Example #1: while Loop
Below is the example of while loop in perl, in below example we are printing the number from 1 to 100.
Code:
$i = 1;
# while loop execution start from 1
while( $i<= 100 ){
printf "$i\t";
$i++; ## loop will execute until the number is not reached 100.
}
printf "\n\nAfter loop condition is false it is terminated from while loop.";
Output:
Example #2: Nested while Loop
Nested while loop is nothing but a one while loop used inside another while loop. The execution of loop start from outer while loop and ends with outer while loop.
Code:
$i = 1;
# while loop execution start from 1.
while( $i<= 5 ){ ## First while loop define value of i is 5
$j = 1;
while( $j <= 5 ){ ## second while loop define value of j is 5
printf "$i $j\n";
$j++;
} ## End of second while loop.
$i++;
} ## End of first while loop.
Output:
Conclusion
while loop is very important and useful in perl to iterate the statement of the code multiple times. In while loop when program execution will start the compiler first checks the given code condition is true or false if the given condition is true then loop statement will execute.
Recommended Articles
We hope that this EDUCBA information on “Perl while Loop” was beneficial to you. You can view EDUCBA’s recommended articles for more information.