IF -ELSE
Now, discuss about If-Else in JavaScript, this is most important chapter for every programmer. while writing a program, there may be a situation when you need to adopt one out of a given set of paths. In such cases, you need to use conditional statements that allow your program to make correct decision and perform right actions.
JavaScript supports conditional statments which are used to perform diffrent actions based on diffrent conditions. Here we will explain the if..else.
Flow Chart of if-else:
- The following flow chart shows how the if-else statments works:
- JavaScript supports the following forms of el...else statements:
- If Statement
- If....else stement
- if..else If... Statement
If Statement:
The if statement is the fundamental control statement that allows JavaScript to make decisions and execute statements conditionally
Syntax:
The syntax for a basic if statement is a follows:
if(expression){
Statement(s) to be executed if expression is true.
}
- Here a JavaScript expression is evaluted. If the resulting value is true, the given statement(s) are executed. If the expression is false, then no statement would be not executed. Most of the times, you will use comparison operators while making decisons.
- Here a javaScript expression is evaluted. If the resulting value is true, the given statement(s) are executed. Most of the times, you will use comparison operators while making decisions.
Example :
Try the following example to understand how the if statment works:
<script>
var age = 20;
if(age > 18){
document.write("<b> Qulifies for driving </br>");
}
</script>
OUTPUT:
Oualifies for driving
You can use diifrent value on variable
if....else stements:
- The if...else statement is the next form of control statment that allows javascript to execute statments in a more controlled way.
Syntax:
- The syntax of an if else statement is a follows:
if(expression){
statement(s) to be executed if expression is true.
}else{
statement(s) to be executed expression is true.
}
- Here JavaScript expression is evaluated. If the resulting value is true, the given statement(s) in the ‘if’ block, are executed. If the expression is false, then the given statement(s) in the else block are executed.
Example:
- Try the following code to learn to implement an if statement in JavaScipt:
var age = 10;
if(age > 18){
document.write("<b> Qulifies for driving </br>");
}else{
document.write("<b> Not Qulifies for driving </br>");
}
OUTPUT:
Not, Oualifies for driving:
if...else if...stements
- The if...else statement is an advanced from if...else that allows javascript to make a correct decision out of several conditions.
Syntax:
- The syntax of an if else if statement is as follows:
Syntax:
<p>
if(expression 1){
statement(s) to be executed if expression1 is true.
}else if(expression 2){
statement(s) to be executed of expression 2 is true.
}else if(expression 3){
statement(s) to be executed if expression 3 is true..
}
else{
statement(s) to be executed expression is true.
}
</p>
- There is nothing special about this code. It is just a series of if statements, where each if is a part of the else clause of the previous statement. Statement(s) are executed based on the true condition, if none of the conditions is true, then the else block is executed.
- Try the code for the topics:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Conditional Statement </title>
</head>
<body>
<script>
var book= "maths";
if (book == "history"){
document.write("<b> History Book </b>");
}else if(book == "maths" ){
document.write("</b> Maths Book </b>");
}else if(book =="english"){
document.write("</b> English Book </b>");
}else{
document.write("</b> Sorry!!!, Unknown Books");
}
</script>
</body>
</html>
OUTPUT:
Not, Oualifies for driving:
I hope this is helpful for you.
Comments