Conditional Statement & Loops
Conditional Statement & Loops
Conditional Statement & Loops
Control structure is the essential part of programming language. Commonly used control structures are:
Selection statement If Ifelse Ifelse if Nested if While Do-while For Switch case Break Continue
Nested if
If(condition) Statement if the condition is true If(condition) Statement if the condition is true
If else statement
<html> <head> <title> if else Demo </title> </head> <body> <script type="text/javascript"> var a,b,c; a=10;b=20;c=30;
if(a>b)
{
if(a>c)
document.write("<h3> a is largest number </h3>"); else
document.write("<h3> c is largest number </h3>"); } else { if(b>c) document.write("<h3> b is largest number </h3>"); else document.write("<h3> c is largest number </h3>"); } </script> </body> </html>
<html> <head> <title> if else if Demo </title> </head> <body> <script type="text/javascript"> var marks; marks=80; if(marks<40) document.write("You are Failed"); else if(marks>=40 && marks<50) document.write("You are Passed");
If else if statement
else if(marks=50 && marks<60) document.write("You have got second class"); else if(marks>=60 && marks<66) document.write("You have got first class"); else document.write("You are Distinction Holder"); </script> </body> </html>
alert("Good Morning!!!(Time"+h+":"+m +":"+s+")"); else alert("Have A Good Day!!!(Time"+h+":"+m+":"+ s+")"); } </script> </head> <body onload="GreetMsg()"> </body> </html>
While statement
Used to implement the iterative logic of the program. Syntax: Some initial condition; While(terminating condition) { Some statements; Stepping condition; }
<html> <head> <title> While Demo </title> </head> <body> <table border=1 align="center"> <script type="text/javascript"> i=1; while(i<=10)
while(i<=10) { document.write("<tre><td>" +i+"</td><td>"+(i*i)+"</t d></tr>"); i++; } </script> </head> <body onload="GreetMsg()"> </body> </html>
Do While statement
Syntax:
Do { }while(condition)
<html> <head> <title> Do-While Demo </title> </head> <body> <script type="text/javascript"> counter=1; do
<html> <head> <title> for-loop Demo </title> </head> <body> <table border=1 align="center"> <th>Number</th><th>Square< /th> <script type="text/javascript">
Switch statement
Syntax: Switch(ch) { Case 0: statement1; Break; Case 0: statement2; Break; Case 0: statement3; Break; }
<html> <head> <title> switch case Demo </title> </head> <body> <script type="text/javascript"> 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:
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; } </script> </body> </html>
Break statement
<html> <head> <title> Break Demo </title> </head> <body> <script type="text/javascript"> for(i=10;i>=0;i--) { if(i==5) break; } document.write("My Lucky Number is"+i); </script> </body> </html>
Break statement
<html> <head> <title> Continue Demo </title> </head> <body> <script type="text/javascript"> for(i=10;i>=0;i--) { if(i==5) { x=i; continue; } document.write(i); document.write("<br>"); } document.write("The Number "+x+" is missing in above list"); </script> </body> </html>