0% found this document useful (0 votes)
97 views

Control Structures in JS-6

The document discusses different control statements in JavaScript including decision statements like if, if-else, if-else if and switch statements as well as looping statements like while, do-while and for loops. It provides the syntax and examples of each statement type to specify program flow and repeat instructions in JavaScript.

Uploaded by

smvskumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Control Structures in JS-6

The document discusses different control statements in JavaScript including decision statements like if, if-else, if-else if and switch statements as well as looping statements like while, do-while and for loops. It provides the syntax and examples of each statement type to specify program flow and repeat instructions in JavaScript.

Uploaded by

smvskumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIT –III

Control Statements in
JavaScript
- SMVS KUMAR
Control Statement

• The control statement specifies the flow of data.


• In JavaScript control statements are classified into two
types. They are
• Decision statements
• Looping Statements
Decision-Making Statements

• 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

• It is an entry controlled loop statement


which is used to repeat the instructions until
condition is satisfied.
• It enters into loop by checking the condition
at first.
• It enters into loop minimum zero times if the
condition is not satisfied.
Syntax:
initial value; eg: i=1;
while (condition) while(i<=10)
{ {
Statements; …………..

Increment/decrement;
………… .
}
}
Do..while

• It is an exit–controlled loop statement which is


used to repeat the instructions until
condition is satisfied.
• In this the condition is checked at last .
• It enters into loop minimum 1 time even if the
condition is not satisfied.
for

It repeats the loop fixed no of times.It checks the condition


and enter into loop.
 
Syntax: for(intialvalue;condition;increment/decrement)
Statements;
Eg: for(i=0;i<5;i++)
printf(“%d”,i);

You might also like