Decision Making:
Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.Types of Decision Making in JavaScript:
1. if statement
2. if...else statement
3. if...else if... statement.
4. Switch Statement.
1. if statement:
The if statement is the fundamental control statement that allows JavaScript to make decisions and execute statements conditionally.
Syntax:
if (expression)
{
Statement(s) to be executed if expression is true
}
2. 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 (expression)
{
Statement(s) to be executed if expression is true
} else {
Statement(s) to be executed if expression is false
}
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.
3. 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 (expression 1)
{
Statement(s) to be executed if expression 1 is true
} else if (expression 2) {
Statement(s) to be executed if expression 2 is true
} else if (expression 3) {
Statement(s) to be executed if expression 3 is true
} else {
Statement(s) to be executed if no expression is true
}
4. Switch Statement:
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 condition 1: statement(s)
break; case condition 2: statement(s)
break;
...
case condition n: statement(s)
break; default: statement(s)
}
Looping Statements:
The JavaScript loops are used to iterate the piece of code using for, while, do while or for-in loops. It makes the code compact. It is mostly used in array.
There are four types of loops in JavaScript:
1. for loop
2. for-in loop
3. while loop
4. do-while loop
1. for loop:
The 'for' loop is the most compact form of looping. It includes the following three important parts −
1. The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins.
2. The test statement which will test if a given condition is true or not. If the condition is true, then the code given inside the loop will be executed, otherwise the control will come out of the loop.
3. The iteration statement where you can increase or decrease your counter. Syntax:
for (initialization; test condition; iteration statement)
{
Statement(s) to be executed if test condition is true
}
2. for-in loop:
The for...in loop is used to loop through an object's properties. Syntax:
for (variable name in object)
{
statement or block to execute
}
In each iteration, one property from object is assigned to variable name and this loop continues till all the properties of the object are exhausted.
3. while loop:
The purpose of a while loop is to execute a statement or code block repeatedly as long as an expression is true. Once the expression becomes false, the loop terminates.
Syntax:
while (expression)
{
Statement(s) to be executed if expression is true
}
4. do-while loop:
The do...while loop is similar to the while loop except that the condition check happens at the end of the loop.
This means that the loop will always be executed at least once, even if the condition is false. Syntax:
do {
Statement(s) to be executed;
} while (expression);
Conclusion: We understand that how to use decision-making & looping statements in JavaScript.
Develop JavaScript to use decision making & looping statements.
1. Decision Making: Find whether is number is EVEN or ODD.
<html>
<head>
<script type="text/javascript"> var x=24;
if(x%2==0)
{
document.write("<h3>The Given Number is Even Number</h3>");
}
else
{
document.write("<h3>The Given Number is Odd Number</h3>");
}
</script>
</head>
<body>
</body>
</html>
------------------------------------------------------------------------------------------------
Output: The Given Number is Even Number
2. Looping Statements: JavaScript for find whether is number is EVEN between 1 to 20.
<html>
<head>
<script type="text/javascript">
document.write("<b>Using do...while loops </b><br />"); var i = 2;
document.write("Even numbers less than 20<br />"); do
{
document.write(i + "<br />"); i = i + 2;
}while(i<20)
</script>
</head>
<body>
</body>
</html>
------------------------------------------------------------------------------------------------
Output:
Using do...while loops Even numbers less than 20 2
4
6
8
10
12
14
16
18
3.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Practical No_2</title>
</head>
<body>
<h3>Find Even and Odd numbers between 20</h3>
<script>
document.write("<b>Using for loop and if </b><br />");
document.write("<b>Even numbers from 1 to 20</b><br>");
for(var i=2;i<=20;i++){
if(i%2==0){
document.write("Even number is:" +i+ "<br>");
}
}
document.write("<br>");
document.write("<b>Odd numbers from 1 to 20</b><br>");
for(var i=2;i<=20;i++){
if(i%2!=0){
document.write("Odd number is:" +i+ "<br>");
}
}
</script>
</body>
</html>
Output: Find Even and Odd numbers between 20
Using for loop and ifEven numbers from 1 to 20
Even number is:2
Even number is:4
Even number is:6
Even number is:8
Even number is:10
Even number is:12
Even number is:14
Even number is:16
Even number is:18
Even number is:20
Odd numbers from 1 to 20
Odd number is:3
Odd number is:5
Odd number is:7
Odd number is:9
Odd number is:11
Odd number is:13
Odd number is:15
Odd number is:17
Odd number is:19
Comments
Post a Comment