Conditional Statement & Loops
-
Upload
kabilan-siranjivi -
Category
Documents
-
view
41 -
download
0
description
Transcript of Conditional Statement & Loops
-
5/27/2018 Conditional Statement & Loops
1/34
Conditional Statement & Loops Control structure is the essential part of programming
language.Commonly used control structures are:
Selection statement
If
IfelseIfelseif
Nested if
While
Do-whileFor
Switch case
Break
Continue
-
5/27/2018 Conditional Statement & Loops
2/34
Selection statement
The syntax of if is
If(condition)
Statement if the condition is true
The syntax of ifelse isIf(condition)
Statement if the condition is true
else
Statement if the condition is false
-
5/27/2018 Conditional Statement & Loops
3/34
The syntax of ifelse if is
If(condition)
Statement if the condition is true
else If(condition)
Statement if the another condition is false
else If(condition)
Statement if the another condition is false
else
Statement
-
5/27/2018 Conditional Statement & Loops
4/34
Nested if
If(condition)
Statement if the condition is true
If(condition)
Statement if the condition is true
else
Statement if the condition is false
-
5/27/2018 Conditional Statement & Loops
5/34
If else statement
if else Demo
var a,b,c;
a=10;b=20;c=30;
if(a>b)
{
if(a>c)
document.write(" a is largest
number ");
else
document.write(" c is largest
number ");}
else
{
if(b>c)
document.write(" b is largestnumber ");
else
document.write(" c is largestnumber ");
}
-
5/27/2018 Conditional Statement & Loops
6/34
-
5/27/2018 Conditional Statement & Loops
7/34
If else if statement
if else if Demo
var marks;
marks=80;
if(marks=40 && marks=60 &&marks
-
5/27/2018 Conditional Statement & Loops
8/34
-
5/27/2018 Conditional Statement & Loops
9/34
-
5/27/2018 Conditional Statement & Loops
10/34
-
5/27/2018 Conditional Statement & Loops
11/34
-
5/27/2018 Conditional Statement & Loops
12/34
-
5/27/2018 Conditional Statement & Loops
13/34
Display Even or Odd
Odd or Even Demo
function fun(str)
{var num=Number(str);
if(num%2==0)
alert("Even number!!!");
elsealert("Odd number!!!");
}
var input_str=prompt("Entersome number","");
fun(input_str);
-
5/27/2018 Conditional Statement & Loops
14/34
-
5/27/2018 Conditional Statement & Loops
15/34
-
5/27/2018 Conditional Statement & Loops
16/34
-
5/27/2018 Conditional Statement & Loops
17/34
-
5/27/2018 Conditional Statement & Loops
18/34
Display the Message Using Time
Condition
Display Message
function GreetMsg()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
if(h
-
5/27/2018 Conditional Statement & Loops
19/34
-
5/27/2018 Conditional Statement & Loops
20/34
While statement
Used to implement the iterative logic of theprogram.
Syntax:
Some initial condition;
While(terminating condition)
{
Some statements;
Stepping condition;
}
-
5/27/2018 Conditional Statement & Loops
21/34
While Demo
i=1;
while(i
-
5/27/2018 Conditional Statement & Loops
22/34
-
5/27/2018 Conditional Statement & Loops
23/34
Do While statement
Syntax:Do
{
}while(condition)
-
5/27/2018 Conditional Statement & Loops
24/34
Do-While Demo
counter=1;
do
{
document.write("Thisstatement
number:"+counter);
document.write("
");counter++;
}while(counter
-
5/27/2018 Conditional Statement & Loops
25/34
-
5/27/2018 Conditional Statement & Loops
26/34
For loop statement
Syntax:
For(initial condition; terminating condition;
stepping condition)
-
5/27/2018 Conditional Statement & Loops
27/34
for-loop Demo
NumberSquare
for(i=1;i
-
5/27/2018 Conditional Statement & Loops
28/34
-
5/27/2018 Conditional Statement & Loops
29/34
Switch statement
Syntax:Switch(ch)
{
Case 0: statement1;
Break;Case 0: statement2;
Break;
Case 0: statement3;Break;
}
-
5/27/2018 Conditional Statement & Loops
30/34
document.write("May");
Break;
Case 6:
document.write("june");
Break;
Case 7:
document.write("july");Break;
Case 8:
document.write("august");
Break;
Case 9:
document.write("september");
Break;Case 10:
document.write("october");
Break;
Case 11:
document.write("november");
Break;
Case 12:document.write("december");
Break;
}
switch case Demo
d=new Date();
ch=d.getMonth();
switch(ch)
{
Case 1:
document.write("January");
Break;
Case 2:
document.write("February");
Break;
Case 3:
document.write("March");
Break;
Case 4:
document.write("April");
Break;
Case 5:
-
5/27/2018 Conditional Statement & Loops
31/34
Break statement
Break Demo
for(i=10;i>=0;i--)
{
if(i==5)
break;
}
document.write("My
Lucky Number is"+i);
-
5/27/2018 Conditional Statement & Loops
32/34
-
5/27/2018 Conditional Statement & Loops
33/34
Break statement
Continue Demo
for(i=10;i>=0;i--)
{
if(i==5)
{
x=i;continue;
}
document.write(i);
document.write("
");}document.write("The Number"+x+" is missing in abovelist");
-
5/27/2018 Conditional Statement & Loops
34/34