Unit 3: Making Decisions and Repeating Yourself

4th semester

 

Conditional Statements

Conditional statements are used to perform different actions based on different conditions.
In PHP we have the following conditional statements:

  • if statement – use this statement to execute some code only if a specified condition is true
  • if..else statement – use this statement to execute some code if a condition is true and another code if the condition is false
  • if..elseif…. else statement – use this statement to select one of several blocks of code to be executed
  • switch statement – use this statement to select one of many blocks of code to be executed

i. The if Statement
Use the if statement to execute some code only if a specified condition is true.
Synatx:-
if (condition) code to be executed if condition is true;
The following example will output “Have a nice weekend!” if the current day is Friday:

ii. if…else Statement
 
Use the if… else statement to execute some code if a condition is true and another code if a condition is false.
syntax
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Example
The following example will output “Have a nice weekend!” if the current day is Friday, otherwise it will output “Have a nice day!”:

output :– 10 is even number

iii.The if…elseif…. else Statement
 Use the if….elseif…. else statement to select one of several blocks of code to be executed.
Syntax
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Example
 The following example will output “Have a nice weekend!” if the current day is Friday, and “Have a nice Sunday!” if the current day is Sunday. Otherwise it will output “Have a nice day!”:

Conditional statements are used to perform different actions based on different conditions.
iv. PHP Switch Statement
In php switch statement is used to perform different actions based on different conditions. In others word PHP switch statement is used to execute one statement from multiple conditions. It works like PHP if-else-if statement.
Syntax

switch(expression){
case value1:
//code to be executed
break;
case value2:
//code to be executed
break;
……
default:
code to be executed if all cases are not matched;
}

Example

For Loop

The for loop is used when you know in advance how many times the script should run. In php for loops execute a block of code specified number of times.
Syntax
for (initilation; condition; increment/decrement)
{
code to be executed;
}
Example

Output
0 1 2 3 4 5 6 7 8 9 10

While Loop

The while loop is used when you don’t know how many times the script should run. while loops execute a block of code while the specified condition is true same like for loop.
Syntax
while(condition)
{
//code to be executed
}
Example

Output
0 1 2 3 4 5 6 7 8 9 10

do..while Loop

do..while loop is used where you need to execute code at least once. 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
{
//code to be executed
}
while(condition);
Example

Output
0 1 2 3 4 5 6 7 8 9 10