where we have execute a particular statement or group of statements when a particular condition is satisfied. This is called decision making.
Decision making in Java Script and many other high level langues is achieved with help of if and if else statements.
For example consider the following code
var marks = prompt("Enter Your Marks:");
if (marks==80) {
alert("Congratulations"); }
if (marks==40) {
alert("Need improvement");}
This script asks for marks from the user. if marks of user are equal to 80 he will receive congratulation message. if marks are equal to 40 he will receive the Needs Improvement message.
In above example value of marks decides which statement to be executed as shown in below figure. If marks are equal to 80 then first alert is displayed.
if marks are equal to 40 second alert is displayed.
The Symbols == are called comparison operator and this == is called equality operator
Comparison
Operator Symbols
|
Meaning
|
Examples
|
==
|
Equality
|
if
(a==b)
|
<
|
Less
Than
|
if
(a<b)
|
>
|
Greater
Than
|
if
(a>b)
|
<=
|
Less
than or equal to
|
if
(a<=)
|
>=
|
Greater
than Equal to
|
if
(a<=)
|
!=
|
Not
Equal to
|
if
(a!=b)
|
More complex conditions can be achieved by using logical operator like &&, OR etc.
If else statement
With the help of if else statement we can achieve that if a particular condition is satisfied than if part is executed otherwise else part of
the statement is executed.
For examples
var marks = prompt("Enter Your Marks:");
if (marks>==50) { alert (" Your have passed the exam, Congratulations");}
else {alert ("You are failed, Regretting");}