Control Structures
by Team uCertify
The values in a condition are compared by using the comparison operators. The loops are used to run a set of statements several times until a condition is met. If the condition is true, the loop is executed. If the condition becomes false, the loop is terminated and the control passes to the next statement that follows the loop block.
The control structures that are used in JavaScript are as follows:
The if statement: The if statement is a simple decision-making statement that is used to apply conditions in the program. If a condition is true, the statement in the if block is executed; otherwise, the statement following the if block is executed.
The syntax of the if statement is as follows:
if(condition)
{
//statements
}
The following example will demonstrate the use of the if statement:
function ifdemo()
{
var a=prompt("Enter a number")
var b= prompt("Enter a number")
if(a>b)
{
document.write(a)
}
}
The above code will execute only if the value of the variable a is greater than the value of the variable b, and the value of the variable a will be printed.
The if-else statement: The if-else statement is used to apply a condition in the program. If the condition is true, the statement in the if block is executed. If the condition is false, the statement in the else block is executed.
The syntax for using the if-else statement is as follows:
if(condition)
{
//statements
}
else
{
//statements
}
The following example will demonstrate the use of the if else statement:
function ifelsedemo()
{
var a=prompt("Enter a number")
var b= prompt("Enter a number")
if(a>b)
{
document.write(a)
}
else
{
document.write(b)
}
}
In the above code, if the value of the variable a is greater than the value of the variable b, the value of the variable a will be printed. If the value of the variable b is greater than the value of the variable a, the value of the variable b will be printed.
The if-else if statement: The if-else if statement is used to check several conditions. If any of the condition is true, the statement of that block is executed; otherwise, the value in the else block is executed.
The syntax of using the if-else if statement is as follows:
if(condition)
{
//statements
}
else if(condition)
{
//statements
}
else if(condition)
{
//statements
}
else
{
//statements
}
The following example will demonstrate the use of the if-else if statement:
function ifelseifdemo()
{
var a=parseFloat(prompt("Enter the marks in Math"))
var b=parseFloat(prompt("Enter the marks in Science"))
var c=parseFloat(prompt("Enter the marks in Geography"))
var d=parseFloat(prompt("Enter the marks in History"))
var e=parseFloat(prompt("Enter the marks in Commerce"))
var f=(a+b+c+d+e)*100/500
if(f=40 && f=50 && f=60 && f75)
{
alert("Excellent")
}
}
In the above code, five conditions are given. If anyone of the condition is true, the statement following the block will be executed.
The conditional statement: The conditional statement can be used in the same way as the if else statement. The conditional statement is a single line statement that can be used in place of the if-else statement.
The syntax of using the conditional statement is as follows:
variablename=condition ? value1 : value2
If the condition is true, the statement following the question mark(?) will be executed. If the condition is false, the statement after the colon(:) is executed.
The following code will demonstrate the use of the conditional expression:
function condexp()
{
var a=prompt("Enter a number")
var b=prompt("Enter a number")
var c=a>b ? a : b
document.write(c)
}
Loops: The loops are the statements that are used to run a set of statements several times until a condition is met. These statements can also be used to extract the elements of an array.
The loops that are used in JavaScript are as follows:
The for loop: The for loop is created by using the for keyword.
The syntax of using the for loop is as follows:
for(initialization; condition; increment/decrement)
{
//statements
}
In the above syntax, the initialization refers to the value that starts the loop. The condition is the conditional statement that is used to control the loop. If the condition is true, the loop continues. If the condition is false, the loop is terminated and the control passes to the next statement that follows the loop block. The increment or decrement is used to run the loop in forward or backward direction.
The following code will demonstrate the use of the for loop:
function fordemo()
{
var a
for(a=1; a
function forindemo()
{
var obj=document
var objname="window"
var result=""
var i
for(i in obj)
{
result+=objname+"."+i+"="+obj[i]+ "
"
}
document.write(result)
}
The while loop: The while loop is a statement that is used to run a statement or a set of statements several times until a condition is met. The while loop checks the condition and runs the loop until the condition is true. If the condition becomes false, the loop is terminated and the control goes to the next statement that follows.
The syntax of the while loop is as follows:
while(condition)
{
//statements
}
The following example will demonstrate the use of the while loop:
function whiledemo()
{
var num=0
while(num
More articles by Team uCertify:

