0% found this document useful (0 votes)
2 views5 pages

Conditional Statements in Javascript

The document explains conditional statements in JavaScript, including 'if', 'if...else', 'if...else if', and 'switch' statements. Each type of statement is described with syntax and examples demonstrating how to execute different blocks of code based on varying conditions. The importance of break statements in switch-case statements is also highlighted to prevent fall-through behavior.

Uploaded by

vparshant839
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Conditional Statements in Javascript

The document explains conditional statements in JavaScript, including 'if', 'if...else', 'if...else if', and 'switch' statements. Each type of statement is described with syntax and examples demonstrating how to execute different blocks of code based on varying conditions. The importance of break statements in switch-case statements is also highlighted to prevent fall-through behavior.

Uploaded by

vparshant839
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Trade:-COPA Unit:-

Javascipt

Topic: Conditional Statements in Javascipt

Conditional Statements :Conditional statements are used to perform different actions based on
different conditions. Very often when we write code, we want to perform different actions for different decisions.
we can use conditional statements in our code to do this.

In JavaScript we have the following conditional statements:

 Use if to specify a block of code to be executed, if a specified condition is true


 Use if else to specify a block of code to be executed, if the same condition is false
 Use else if to specify a new condition to test, if the first condition is false
 Use switch to specify many alternative blocks of code to be executed

The if Statement:
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.

Syntax
if (condition) {
// block of code to be executed if the condition is true
}

Note that if is in lowercase letters. Uppercase letters (If or IF) will generate a JavaScript error.

Example 1:
Make a "Good day" greeting if the hour is less than 18:00:

if (hour < 18) {


greeting = "Good day";
}

The result of greeting will be:

Good day

Example2:
<html>
<body>
<script type = "javascript">

var age = 20;

if( age > 18 ) {


document.write("<b>Qualifies for driving</b>");
}
1
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>

OutPut:
Qualifies for driving
Set the variable to different value and then try...

if...else statement
The 'if...else' statement is the next form of control statement that allows JavaScript to execute statements in a more
controlled way.

Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
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 1:
If the hour is less than 18, create a "Good day" greeting, otherwise "Good evening":

if (hour < 18) {


greeting = "Good day";
} else {
greeting = "Good evening";
}

The result of greeting will be:


Good day

Example2:
<html>
<body>
<script type = "javascript">

var age = 15;

if( age > 18 ) {


document.write("<b>Qualifies for driving</b>");
} else {
document.write("<b>Does not qualify for driving</b>");
}

</script>

2
<p>Set the variable to different value and then try...</p>
</body>
</html>

Output
Does not qualify for driving
Set the variable to different value and then try...

if...else if... statement


The if...else if... statement is an advanced form of if…else that allows JavaScript to make a correct decision out of
several conditions.

Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
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.

Example1:
If time is less than 10:00, create a "Good morning" greeting, if not, but time is less than 20:00, create a "Good
day" greeting, otherwise a "Good evening":

if (time < 10) {


greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}

The result of greeting will be:


Good morning

Exmple 2:
<html>
<body>
<script type = "text/javascript">

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 == "economics" ) {
document.write("<b>Economics Book</b>");

3
} else {
document.write("<b>Unknown Book</b>");
}

</script>
<p>Set the variable to different value and then try...</p>
</body>
<html>

Output
Maths Book
Set the variable to different value and then try...

JavaScript Switch Statement:

Switch Statement :The switch statement is used to perform different actions based
on different conditions.The objective of a switch statement is to give an expression to evaluate and several different
statements to execute based on the value of the expression. The interpreter checks each case against the value of the
expression until a match is found. If nothing matches, a default condition will be used.

Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

This is how it works:


 The switch expression is evaluated once.
 The value of the expression is compared with the values of each case.
 If there is a match, the associated block of code is executed.
 If there is no match, the default code block is executed.

Note: The break statements indicate the end of a particular case. If they were omitted, the interpreter would
continue executing each statement in each of the following cases.
<html>
<body>
<script type = "text/javascript">

var grade = 'A';


document.write("Entering switch block<br />");
switch (grade) {
case 'A': document.write("Good job<br />");
break;

case 'B': document.write("Pretty good<br />");


break;

4
case 'C': document.write("Passed<br />");
break;

case 'D': document.write("Not so good<br />");


break;

case 'F': document.write("Failed<br />");


break;

default: document.write("Unknown grade<br />")


}
document.write("Exiting switch block");

</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>

Output
Entering switch block
Good job
Exiting switch block
Set the variable to different value and then try...

Break statements play a major role in switch-case statements. Try the following code that uses switch-case statement
without any break statement.
<html>
<body>
<script type = "text/javascript">

var grade = 'A';


document.write("Entering switch block<br />");
switch (grade) {
case 'A': document.write("Good job<br />");
case 'B': document.write("Pretty good<br />");
case 'C': document.write("Passed<br />");
case 'D': document.write("Not so good<br />");
case 'F': document.write("Failed<br />");
default: document.write("Unknown grade<br />")
}
document.write("Exiting switch block");

</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>

Output
Entering switch block
Good job
Pretty good
Passed
Not so good
Failed
Unknown grade
Exiting switch block
Set the variable to different value and then try...

You might also like