Web Technologies
(COURSE CODE: B18CS3102)
Control Statements
Smt.M.Jeevana Sujitha
Assistant Professor
Department of Computer Science and Engineering
SRKR Engineering College, Bhimavaram, A.P. - 534204
OBJECTIVES
The Objective of this lecture is
Control Statements
Conditional Statements:
used to based on
different conditions.
Control Statements
If statement: The if statement executes some code if one
condition is true.
Syntax:
if(condition)
{
Code to be executed if condition is true
}
Example:
<?php
$age=19; if($age==18||$age>18) echo “eligible for voting”;
?>
Control Statements
if-else statement: The if-else statement executes some code if a condition is true and another code if that 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
$age=19; if($age==18||$age>18) echo “eligible for voting”; else
echo “Not eligible for voting”;
?>
Control Statements
else if ladder: The else if ladder statement executes different
codes for more than two conditions.
Syntax:
if(condition)
{
code to be executed if this condition is true;
}
else if(condition)
{
code to be executed if first condition is false and this condition is
true;
}
else
{
code to be executed if all conditions are false;
}
Control Statements
Switch Statement: The switch statement is used to perform different actions based on different conditions.
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;
}
Control Statements
Looping Statements: If we want to do the same task for required number of times then we go for looping concept.
of code again and again, as long as a certain condition is true
Control Statements
while loop: The while loop executes a block of code as long as the specified condition is true.
Syntax: while(condition)
{
Code to be executed;
}
Example:
<?php
$x = 1;
while($x <= 10)
{
echo “srinu”;
$x++;
}
?>
Control Statements
do-while loop: The do-while loop always executes a block of code once, it
will then check the condition and repeat the while loop as long as the
specified condition is true.
Syntax:
do
{
code to be executed;
} while (condition is true);
Example:
<?php
$x = 1;
do
{
echo “srinu";
$x++;
}while($x<=10);
?>
Control Statements
for loop: The for loop is executed a block of code a specified
number of times.
Syntax:
for (initialization; condition; increment/decrement)
{
code to be executed for each iteration;
}
Example:
<?php
for ($x = 1; $x <= 10; $x++)
{
echo “srinu";
}
?>
Control 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)
{
code to be executed;
}
assigned to $value and the array pointer is moved by one, until it
reaches the last array element.
Example:
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
Control Statements
Jumping Statements:
Break: The break statement can be used to jump out of a
loop or switch statement.
Example:
<?php
for ($x = 1; $x <= 10; $x++)
{
if ($x == 5)
{
break;
}
echo "The number is: $x <br>";
}
?>
Control Statements
Jumping Statements:
Continue: The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
Example:
<?php
for ($x = 1; $x <= 10; $x++)
{
if ($x == 5)
{
continue;
}
echo "The number is: $x <br>";
}
?>
THANK YOU