Monday 6 July 2020

conditional statements in javascript - Online Help

Conditional Statements in javascript:

Conditional statements in javascript are the set of commands used to perform different action s based on different conditions.  the conditional construct will either return a True or false depending upon how the condition is evaluated. There are two conditional statements in JavaScript:
  • if-else statement
  • switch statement

if-else statement :

Use this statement if you want to execute s set of code when a condition is true. If the condition is true the true statement is executed, otherwise, the false statement is executed.

The general format of the if-else statement is 

if (condition)
{
statement 1;                         
}           
else             
{             
statement 2;
}

The else clause allows you to specify a statement or group of statements that will be executed if the expression does not evaluate to true. Only one else clause is allowed in any if statement. Again, the statement can be used with or without the curly brackets.


Example:

<html>
<head>
<title> java script </title>
</head>
<body>
<b>RESULT</b>
<p> OPEN SOURCE SOFTWARE:
<script type=”text/java script”>
var m= 70;
if (m>=35 && m<=100)
{
document. write (“<span style=’ color: blue ‘> PASS</span>”);
}
else
{
document. write(“<span style=’color : red’>FAIL</span>”);
}
</script>
</body>
</html>


switch statement in javascript:

The switch statement evaluates an expression and compares the value to one or more case clauses. If you need to compare a variable against more than two or three values, the switch statement is the most efficient way.

The general format of the switch statement is

 switch (expression)
{
case labe11: block1
break;
case labe12: block2 
break;
 ……………
default:     def block;
}

Evaluate an expression and attempt to match the expression’s value to a case label. If a match is found, the program executes the associated statement.

 

Example:

<html>
<head>
<title>java script</title>
</head>
<body>
<h2><font Color=”red”>SUBJECT&CODE</h2></font>
<script type=”text/java script”>
var subject code=”UCA61”;
switch (subject code)
{
case”UCA61”: subject=”Open Source Software”;
break;
case”UCA62”: subject=”Multimedia”;
break;
case”UPCA66”: subject=Open Source Laboratory”;
break;
default: subject=” UNKNOWN SUBJECT”;
}
document. write(“<h2>code=& nbsp” +subject code+”
<br> subject =&nbsp” +</h2>”);tement
</script>
</body>
</htm1>

I hope you guys understand what is a conditional statement in javascript. If you find any mistake or having any queries then please comment down below.



No comments:

Post a Comment