For, While, do While Loops in PHP and Javascript (JS)
Since Loop is always same in general Programing Languages Like C,C++,Js,PHP
Here We Get The Use Of For Loop For Beginners in Programing Languages.
What is Loop?
Sometime we need to use the task again and again and any calculation we use loops.
Loops are of 3 types for,While,Do While .
1. For Loop
code :
In PHP
<?php
for($i=0;$i<50;$i++)
{
echo $i.”<br>”; //print numbers 1 to 49
}
?>
In Javascript (Js)
<html>
<head>
</head>
<body>
<h1>For Loop In Javascript Please Open Your Console To See The Ouput By Pressing F12 Key</h1>
</body>
<script>
for(var i=0;i<50;i++)
{
console.log(i); //print numbers 1 to 49 in your console
}
</script>
</html>
Here 1st parameter in for loop is intialization (initial value of i for starting of loop)
2nd is for condition checking ( check current value of i is i<50 .if condition satisfy then body of loop run).
3rd is for iteration (Increasing value i=i+1).
Flow Chart Of For Loop
2.While Loop
Code :
In PHP
<?php
$i=0; //initialization
while($i!=50) //Condition Checking
{
echo $i.”<br>”; //print numbers 1 to 49
$i++; //iteration
}
?>
In Javascript (Js)
<html>
<head>
</head>
<body>
<h1>While Loop In Javascript Please Open Your Console To See The Ouput By Pressing F12 Key</h1>
</body>
<script>
var i=0;
while(i!=50)
{ //bodyof loop
console.log(i); //print numbers 1 to 49 in your console
i++; //iteration
}
</script>
</html>
Here First We Check Condition If the condition is satisfied then the body of loop will executed until the condition not getting true.
Flow Chart Of While Loop
2.Do While Loop
Code :
In PHP
<?php
$i=0; //initialization
do //Body of loop execute
{
echo $i.”<br>”; //print numbers 1 to 49
$i++; //iteration
}
while($i!=50); //Condition Checking
?>
In Javascript (Js)
<html>
<head>
</head>
<body>
<h1>Do While Loop In Javascript Please Open Your Console To See The Ouput By Pressing F12 Key</h1>
</body>
<script>
var i=0;
do //body of loop
{
console.log(i); //print numbers 1 to 49 in your console
i++; //iteration
}
while(i!=50);
</script>
</html>
Here First Time loop will be executed then check If the condition is satisfied then the body of loop will execcuted again.
Flow Chat Of Do While Loop
Difference Between For, While and Do while loop
While loop and For Loop Excutes body of loop only if the condition get satisfied .Since Do While loop execute body of loop once then check the condition.
Thank you.
No comments:
Post a Comment