Perl WHILE Loop Function

What is While loop function in Perl programming?

Explanation

The While loop function in Perl is used to execute statements till the condition becomes false.

Syntax:


While (expr1)
{
Statement to be executed
}

In the above syntax the code segment is executed till the "expr1" becomes false, here the iteration never stops even the condition becomes false until it checks the "expr1".

Example :


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$c=0;
while($c<=5)
{
print "This is line".$c."<br>";
$c++;
}
Result :

This is line0
This is line1
This is line2
This is line3
This is line4
This is line5

In the above example when the value of $c becomes 6, the iteration continues till the "expr1" is checked and as the condition is false the code execution is stopped.

Ask Questions

Ask Question