PHP
and Javascript (JS) if,if else,if elseif...else
What
is If Statement in Programing ?
In
programing we need to check the condition if condition is true then
we perform task 1 else we perform task 2
then
we can check this conditions by if statement
Simple
Voting Example
If
Our Age is Greater then 18 we are eligible for vote else we are not
eligible.
if
(we are greater than 18)
{
eligible
}
else
{
we
are not eligible
}
Condition
Statements Are Of Four Types
1.
if statement - Execute Condition if true
2.
if...else statement - Execute Task 1 if condition true otherwise
Perform Task 2
3.
if...elseif....else statement - Executes Task More Than One Condition
if Condition Satisfy Perform The Task Else Perform Default else Task
4.
switch statement - Perform Task If Condition Satify in any Case and
Perform The Result.
if
syntax
if
(condition) {
if
condition true
}
Example
1
if
Condition in php
<?php
$a=1;
if($a%2==0)
//checking the condition
{
echo
"even no"; //print even no if
condition is true but here we will get nothing because 1
is an odd number
}
?>
if
Condition in Javascript (Js)
<html>
<head>
</head>
<body>
<h1>If
Statement in Javascript</h1>
</body>
<script>
var
a=1;
if(a%2==0)
//checking the condition
{
console.log("even
no"); //we get the output nothing in
our console by clicking on F12
}
</script>
</html>
Flow
Chart Of if Statement
if
else syntax
if
(condition) {
if
condition true
}
else
{
else
condition true
}
Example
1
if
else Condition in php
<?php
$a=1;
if($a%2==0)
//checking the condition
{
echo
"even no"; //print even no
if condition is true but here we will get nothing because 1 is an odd
number
}
else{
echo
"odd no"; //print odd no. we
know that 1 is a odd number
}
?>
if
else Condition in Javascript (Js)
<html>
<head>
</head>
<body>
<h1>If
else Statement in Javascript</h1>
</body>
<script>
var
a=1;
if(a%2==0)
//checking the condition
{
console.log("even
no"); //we get the output nothing in
our console by clicking on F12
}
else
{
console.log('odd
no'); //we get output odd no
}
</script>
</html>
Flow
Chart Of if else Statement
The
if...elseif....else
if
(condition) {
if
condition true
}
elseif
{
else
condition second
}
else
{
else
third condition
}
Example
1
if
elseif.. if Condition in php
<?php
$a=1;
if($a<0)
//checking the condition
{
echo
"a is smaller then zero";
}
else
if($a>1) //second
condition
{
echo
"a is smaller then 1";
}
else //else
last condition
{
echo
“a is equal to 1”;
}
?>
if
else Condition in Javascript (Js)
<html>
<head>
</head>
<body>
<h1>If
elseif... else Statement in Javascript</h1>
</body>
<script>
var
a=1;
if(a<0)
//checking
the condition
{
console.log(
"a is smaller then zero");
}
else
if(a>1) //second
condition
{
console.log("a
is smaller then 1");
}
else //else
last condition
{
console.log(
“a is equal to 1”);
}
</script>
</html>
Flow
Chart Of if elseif....else Statement
No comments:
Post a Comment