0% found this document useful (0 votes)
46 views

PHP Loop Statement

The document discusses different types of PHP loops including for, while, and do while loops. It provides syntax examples and explanations of how each loop works.

Uploaded by

scarletdain63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

PHP Loop Statement

The document discusses different types of PHP loops including for, while, and do while loops. It provides syntax examples and explanations of how each loop works.

Uploaded by

scarletdain63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

TRINIDAD MUNICIPAL COLLEGE

COLLEGE OF COMPUTER STUDIES

LOOP STATEMENT
Looping statement or loop is also one of the
most important in programming. Loop is used to
repeat a certain part of your source based on the
perimeters.
Loop becomes active as long as the result of
the condition is meet and exit if it didn’t meet the
condition became, just like a conditional statement.
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

PHP Looping statement:


for() loop
while() loop
Do while() loop

PHP looping statement also used comparison


operators to create a condition and logical
operator to add another set of condition(s).
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

PHP for() Loop


PHP for() loop execute a block of code in a
specified number of time. Also, for loop is used
when you know in advance how many times the
script should run.

SYNTAX:
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

for() Loop parameters:

• initialization – initialize the value of a counter


• condition – check the value of a counter in each loop. If
the condition is TRUE, the loop will continue and if not
the loop will end.
• steps – increases or decreases the value of the counter.
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

flowchart
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

Example 1: Output:
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

Example 2: Output:
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

Exercises
1. Create a loop that runs from 0 to
9.
2. Write a program to calculate
and print the factorial of a number
using a for loop. The factorial of a
number is the product of all integers
up to and including that number, so the
factorial of 4 is 4*3*2*1= 24.
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

PHP while() Loop


The first and simplest loop to learn
in PHP is the so-called while() loop.
With this loop type, so long as the
conditional expression specified
evaluates to true, the loop will
continue to execute. When the
condition becomes false, the loop will
be broken and the statements
following it will be executed.
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

Syntax:

<?php
while (condition is true)
{
do this;
}
?>
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

Flowchart:
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

Example:

<?php
$n=1;
while($n<=10){
echo "$n<br/>";
$n++;
}
?>
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

Activity:

1. Create a PHP program using


while statement to sum the
whole numbers from 1 to 10.
2. Create a program that identity
if the number is “EVEN or ODD.
Starting Number: 1
Ending Number: 10
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

Activity:

1. Create a loop that runs from 0 to 9.


2. Write a program to calculate and print the factorial of a
number using a for loop. The factorial of a number is
the product of all integers up to and including that
number, so the factorial of 4 is 4*3*2*1= 24.

3. Create a PHP program using while statement to sum


the whole numbers from 1 to 10.
4. Create a program that identity if the number is “EVEN
or ODD.
Starting Number: 1
Ending Number: 10
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

PHP do while() Loop

The do while() loop will always


execute the block of code once and
check the condition, it will repeat the
loop while the specified condition is
<?php
true.
do {
Syntax:
code to be execute;
} while (condition);
?>
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

FLOWCHART:
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

Example: Output:
<?php
$i = 0;
do {
echo $i .
"<br>";
$i+=2;
} while ($i<=16);
?>
TRINIDAD MUNICIPAL COLLEGE
COLLEGE OF COMPUTER STUDIES

Example:

1. Create a PHP program that uses a do-while loop to


print all even numbers from 2 to 20.
2. Create a PHP program that uses a do-while loop to
print all odd numbers from 1 to 15.
3. Create a PHP program that counts backward from
10 to 1 using a do-while loop and prints each
number.
4. Create a PHP program that calculates the factorial
of a positive integer using a do-while loop.

You might also like