JAVA SCRIPT DAY 5 (Control statement)

DAY -5

Control statement

When we write code for a particular program, we sometimes takes various decisions for executing different action. These can be done through conditional / control statement.
JavaScript has the following control statement:
1.      If statement
2.      Switch statement

JavaScript - if...else Statement

Flow Chart of if-else

The following flow chart shows how the if-else statement works.
JavaScript supports the following forms of if..else statement −
·if statement
·if...else statement
·if...else if... statement.

 

if statement

Ø    The syntax for a basic if statement is as follows −
if(expression){
Statement(s) to be executed if expression istrue
}
Ø    The expression is evaluated. If the resulting value is true, the given statement(s) are executed. If the expression is false, then no statement would be not executed.


 

Example

<html>
<body>
<scripttype="text/javascript">
<!--
var age =20;
if( age >18){
document.write("<b>Qualifies for driving</b>");
}
//-->
</script>

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

Output

Qualifies for driving

 

if...else statement:

Ø    Syntax

if(expression){
Statement(s) to be executed if expression istrue
}
else{
Statement(s) to be executed if expression isfalse
}
Ø    The 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

<html>
<body>

<scripttype="text/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>

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

Output

Does not qualify for driving

 

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.
Ø    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.

Ø    Syntax

if(expression 1){
Statement(s) to be executed if expression 1istrue
}

elseif(expression 2){
Statement(s) to be executed if expression 2istrue
}

elseif(expression 3){
Statement(s) to be executed if expression 3istrue
}

else{
Statement(s) to be executed ifno expression istrue
}

Ø    Example

<html>
<body>

<scripttype="text/javascript">
<!--
var book ="maths";
if( book =="history"){
document.write("<b>History Book</b>");
}

elseif( book =="maths"){
document.write("<b>Maths Book</b>");
}

elseif( book =="economics"){
document.write("<b>Economics Book</b>");
}

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

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

Output

Maths Book

No comments:

Post a Comment

Give your valuable feedback

Topic :Software & Types, Subject: Computer Fundamental Notes for CSJM University Kanpur(for different courses like BBA, BCA, etc..)

Software Software refers to the programs, data, and instructions that enable a computer or other digital device to perform specific tasks or...