Web Based Application Development with PHP
(22619) Answers
Write a PHP program to demonstrate the use of Looping structures using- a) While statement b) Do-while statement c) For statement d) Foreach statement
* Program Code
1) Write a program to print first 30 even numbers. (Using for, while, do..while)
Ans=>
Using For Loop
using while loop
- <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>First 30 Even Numbers</title></head><body> <?php $i=1; while($i<=60) { if($i%2==0) { echo $i,"<br>"; } } ?> </body></html>
using do while loop<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>First 30 Even Numbers</title></head><body> <?php $i=1; do{ if($i%2==0) { echo $i,"<br>"; $i++; } } while($i<=60); ?> </body></html>
* Exercise.
- <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>First 30 Even Numbers</title></head><body><?php$i=1;while($i<=60){if($i%2==0){echo $i,"<br>";}}?></body></html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>First 30 Even Numbers</title>
</head>
<body>
<?php
$i=1;
do{
if($i%2==0)
{
echo $i,"<br>";
$i++;
}
} while($i<=60);
?>
</body>
</html>
1)Write any program using if condition with for loop.
<?php
for($i=0;$i<=60;$i++)
{
if($i%2==0){
echo $i,"<br>";
}
}
?>
2)Write a program to display pyramids of star/patterns using increment/decrement.
* Practical Related Questions.
1)When for loop will be terminated?
Ans => 1) When the break statement is encountered inside a loop, the loop is immediately terminated, and the program control resumes at the next statement following the loop
2) Difference between for and for-each loop
Ans=> for for each
1) The iteration is clearly visible The iteration is hidden.
2) Good Performance Better Performance
3) Stop condition is specify stop condition has to be explicitly specify
.
.
.
.
Keep Growing ❤
0 Comments