Loops
and
Conditional Statements
Definition of Loop
 In computer programming, a loop is a sequence of instruction(s) that is
continually repeated until a certain condition is reached
OR
 Loops execute a block of code while the specified condition is
true.
Topic: Loops & Conditional Statements
Loop Types
 for - loops through a block of code a specified number of times
 while - loops through a block of code as long as the specified condition is
true
 do...while - loops through a block of code once, and then repeats the
loop as long as the specified condition is true
 foreach - loops through a block of code for each element in an array
Topic: Loops & Conditional Statements
Basic Operations for Loop
 Initialization: - this step is a first execute in for loop .initialization step is
allows declaration of variable. This step is starting point of for loop
statement in php.
 Condition: - second step of for loop is condition, if condition is true then
body part is execute .otherwise body part is not execute in php.
 increment/decrements: - this step through declare variable increment
and decrements in php(it means declare variable update to the increment
and decrements).
 Statements: - this part is body part. All three step are execute then body
part is executed .in this part display any type of message in web page.
Topic: Loops & Conditional Statements
For loop
 The for statement is used when you know how many times you want to
execute a statement or a block of statements.
Syntax
 for ( initialization; condition; increment/decrements )
{
statement(s);
}
Topic: Loops & Conditional Statements
For loop (Example)
Example
$a = 0;
$b = 0;
for( $i=0; $i<5; $i++ )
{
$a += 10;
$b += 5;
echo $a. “<br>";
echo $b.“<br>";
}
echo ("At the end of the loop a=$a and b=$b" );
Output
10
5
20
10
30
15
40
20
50
25
At the end of the loop a=50 and b=25
Topic: Loops & Conditional Statements
While loop
 The while looping statement will execute a block of code if and as long as
a test expression is a true.
SYNTAX
 while(condition)
{
statement(s);
increment/decrements;
}
Topic: Loops & Conditional Statements
While loop (Example)
Example
$i = 0;
$num = 50;
while( $i < 10)
{
echo $i.“<br>";
echo $num."<br>";
$num--;
$i++;
}
echo ("Loop stopped at i = $i and num = $num" );
0
50
1
49
2
48
3
47
4
46
5
45
6
44
7
43
8
42
9
41
Loop stopped at i = 10 and num = 40
Output
Topic: Loops & Conditional Statements
Do while loop
 The do...while loop will always execute the block of code once, it will then
check the condition, and repeat the loop while the specified condition is
true.
SYNTAX
 do
{
statement(s);
increment/decrements;
} while( condition );
Topic: Loops & Conditional Statements
Do while loop (Example)
Example
$i = 0;
do{
echo $i.“<br>";
$i++;
}while( $i < 10 );
echo ("Loop stopped at i = $i" );
0
1
2
3
4
5
6
7
8
9
Loop stopped at i = 10
Output
Topic: Loops & Conditional Statements
foreach loop
 The foreach loop works only on arrays, and is used to loop through each
key/value pair in an array.
SYNTAX
 foreach (array as value)
{
Statement;
}
Topic: Loops & Conditional Statements
foreach loop (Example)
Example
$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value )
{
echo "Value is $value.“<br>”;
}
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
Output
Topic: Loops & Conditional Statements
Decision Making | Conditional Statements
The if, else if ...else and switch statements are used to take decision based on
the different condition. You can use conditional statements in your code to
make your decisions. PHP supports following three decision making
statements:
 if...else statement - use this statement if you want to execute a set of code
when a condition is true and another if the condition is not true
 else if statement - is used with the if...else statement to execute a set of
code if one of several condition are true
 switch statement - is used if you want to select one of many blocks of code
to be executed, use the Switch statement. The switch statement is used to
avoid long blocks of if..else if..else code.
Topic: Loops & Conditional Statements
The if Statement
 The if statement is used to execute some code only if a specified condition is
true
SYNTAX
 if (condition) {
code to be executed if condition is true;
}
Example
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
Topic: Loops & Conditional Statements
The if...else Statement
 Use the if....else statement to execute some code if a condition is true and another code if the condition is false.
SYNTAX
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
Example
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
} else {
echo "Have a good night!";
}
?>
Topic: Loops & Conditional Statements
The if...else if....else Statement
 Use the if....elseif...else statement to specify a new condition to test, if the first condition is
false.
SYNTAX
if (condition) {
code to be executed if condition is true;
} else if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
Topic: Loops & Conditional Statements
The if...else if....else Statement(Example)
Example
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} else if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
Topic: Loops & Conditional Statements
Switch Statement
 Use the switch statement to select one of many blocks of code to be executed.
SYNTAX
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default: code to be executed if n is different from all labels;
} Topic: Loops & Conditional Statements
Switch Statement
EXAMPLES
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
} Topic: Loops & Conditional Statements
Difference Between if & switch
IF Statement:
Checks the value of data is less than or greater than. (In ranges).
Example:
can tell whether an input age is more than 18 and less than 60.
Switch Case:
Checks the value of data that is pre-specified. Only equal to.
Example:
Can only generate output if the value matches. When the age is 18 or when the age is
60. No comparison of data based on greater than or smaller than. Compares data based on
equality
Topic: Loops & Conditional Statements
Topic: Loops & Conditional Statements