Control Structures in JS-6
Control Structures in JS-6
Control Statements in
JavaScript
- SMVS KUMAR
Control Statement
• if statement
• if..else statement
• if..else if statement
• Switch statement
if
The simple if statement is used to execute a
statement/block if the condition is true
otherwise the statement/block will be skipped.
Syntax: if (condition)
Statement/block;
eg: if (age>=18)
document,write(“eligible”));
If..else
In this type of if statement, if the condition is true then the
statement1/block1 is executed otherwise the
statement2/block2 will be executed.
Syntax: if (condition)
Statement1/block1;
else
Statement2/block2;
Eg:
if (a>b)
document.write(a);
else
document.write(a);
If..else if:
It is used to check multiple conditions. It is also
known as ladder if.
Syntax: if (condition1)
Statement1/block1;
else if (condition2)
statement2/block2;
. .............
else if (condition N)
statement/blockn;
eg: if (avg>=60)
document.write(“first class”);
else if (avg>=50)
document.write(“second class”);
else if (avg>=35)
document.write(“third class”);
switch:
It is a multi-way decision statement. It is used to select a
statement(s) out of several
Available groups.
Syntax:
switch(expression)
{
case const1: statement1/block1;break;
case const2: statement2/block2;break;
.................................................
case constn: statement n/blockn;break;
[default: default statement;]
Eg:
switch(ch)
{
case 1: document.write(“red”);break;
case 2: document.write (“blue”);break;
case 3: document.write(“green”);break;
}
Looping Statements
while loop
do-while loop
for loop
for in loop
while
Increment/decrement;
………… .
}
}
Do..while