Unit I PPT CSS 2
Unit I PPT CSS 2
1
• Create interactive web pages
CO1 using program flow control
structure
2
TEACHING AND EXAMINATION SCHEME
3
CONTENTS
1. if statement
2. if…else statement
3. if…else..if statement
4. nested if statement
5.
Switch…case statement
4
Department of Information Technology,Government
Polytechnic Awasari(kh)
IF STATEMENT
• if is a predefined keyword of JavaScript.
• It is a selection statement/decision making statement.
• If condition is true then it will execute the body of if statement.
• If condition is false then it will not execute the body of if statement.
Syntax:
if(condition)
{
//body of if
}
Example:
<html>
<body>
<script type = "text/javascript">
var age = 20;
5
Department of Information Technology,Government
Polytechnic Awasari(kh)
IF-ELSE STATEMENT
• if and else both are predefined keyword of JavaScript.
• It is a selection statement/decision making statement.
• If condition is true then it will execute the body of if statement.
• If condition is false then it will execute the body of else statement.
Syntax:
if(condition)
{
//body of if
}
else
{
//body of else
}
Example:
<html>
<body>
<script type = "text/javascript">
}
</script>
</body> </html>
6
Department of Information Technology,Government
Polytechnic Awasari(kh)
IF…ELSE…IF STATEMENT
• if and else both are predefined keyword of JavaScript.
• It is a selection statement/decision making 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.
• It is a chain of if-else.
Syntax:
if(condition-1)
{
//statement-1
}
else if(condition-2)
{
//statement-2
}
else if(condition-3)
{
//statement-3
}
else
{
//statement-4
}
7
Department of Information Technology,Government
Polytechnic Awasari(kh)
IF…ELSE…IF STATEMENT- EXAMPLE
<html>
<body>
<script type = "text/javascript">
</script>
</body>
</html>
8
Department of Information Technology,Government
Polytechnic Awasari(kh)
NESTED IF…ELSE STATEMENT
• if and else both are predefined keyword of JavaScript.
• It is a selection statement/decision making statement.
• If we use one if-else within another if-else statement then it is called as
nested if-else statement.
• This statement is used to take correct decision out of several conditions.
Syntax:
if(condition-1)
{
if(condition-2)
{
//statements
}
else
{
//statements
}
}
else
{
if(condition-3)
{
//statements
}
else
{
//statements
}
9
Department of Information Technology,Government
}
Polytechnic Awasari(kh)
NESTED IF…ELSE STATEMENT- EXAMPLE
<html>
<body>
<script type = "text/javascript">
if(a>b)
{
if(a>c)
{
document.write("Greatest Number="+a);
}
else
{
document.write("Greatest Number="+c);
}
}
else
{
if(b>c)
{
document.write("Greatest Number="+b);
}
else
{
document.write("Greatest Number="+c);
}
</script>
</body> </html>
10
Department of Information Technology,Government
Polytechnic Awasari(kh)
SWITCH CASE STATEMENT
• Switch statement is used to execute one of the statements from many
blocks of statements.
• It is a selection statement.
• There are four keywords are used i.e switch, case, break, default.
• Switch case expression value compared with case value and if it is match
then corresponding block will be executed . If none of case values are matched
then default block is executed.
Syntax:
switch(Expression)
{
case value1: //statement;
break;
11
Polytechnic Awasari(kh)
SWITCH CASE STATEMENT- EXAMPLE
<html>
<body>
<script type = "text/javascript">
var grade = 'M';
switch (grade)
{
case 'A': document.write("Good job");
break;
12
Department of Information Technology,Government
Polytechnic Awasari(kh)
QUIZ TIME
Q1. What will be the output of the
following JavaScript code? Q2. Which statement is used to test a
specific condition?
var grade='A';
var result;
switch(grade) a) Select
{ b) If
case 'A': c) Switch
result+="10";
case 'B':
d) For
result+=" 9";
case 'C':
result+=" 8";
default:
result+=" 0";
► Ans.
} document. Write(result); ► Ans. b. If
a) 10
b) 27
c) 8
d) 0
►Ans. b. 27
13
Department of Information Technology,Government
Polytechnic Awasari(kh)
QUIZ TIME
► Ans.c .1
14
Department of Information Technology,Government
Polytechnic Awasari(kh)
THANK YOU
Thank You
15
Polytechnic Awasari(kh)