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
Selection statement The syntax of if is
If(condition) Statement if the condition is true
The syntax of ifelse is
If(condition) Statement if the condition is true else Statement if the condition is false
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
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
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)
[Link]("<h3> a is largest number </h3>"); else
[Link]("<h3> c is largest number </h3>"); } else { if(b>c) [Link]("<h3> b is largest number </h3>"); else [Link]("<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) [Link]("You are Failed"); else if(marks>=40 && marks<50) [Link]("You are Passed");
If else if statement
else if(marks=50 && marks<60) [Link]("You have got second class"); else if(marks>=60 && marks<66) [Link]("You have got first class"); else [Link]("You are Distinction Holder"); </script> </body> </html>
Display Even or Odd
<html> <head> <title> Odd or Even Demo </title> <script type="text/javascript"> function fun(str) { var num=Number(str); if(num%2==0) alert("Even number!!!"); else alert("Odd number!!!"); } </script> </head> <body> <script type="text/javascript"> var input_str=prompt("Enter some number",""); fun(input_str); </script> </body> </html>
Display the Message Using Time Condition <html>
<head> <title> Display Message </title> <script type="text/javascript"> function GreetMsg() { var today=new Date(); var h=[Link](); var m=[Link](); var s=[Link](); if(h<10)
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) { [Link]("<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
{ [Link]("This statement number:"+counter); [Link]("<br>"); counter++; }while(counter<=5); </script> </body> </html>
For loop statement
Syntax:
For(initial condition; terminating condition; stepping condition)
<html> <head> <title> for-loop Demo </title> </head> <body> <table border=1 align="center"> <th>Number</th><th>Square< /th> <script type="text/javascript">
for(i=1;i<=10;i++) { [Link]("<tr><td>"+i+" </td><td>"+(i*i)+"</td></tr> "); } </script> </table> </body> </html>
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=[Link](); switch(ch) { Case 1: [Link]("January"); Break; Case 2: [Link]("February"); Break; Case 3: [Link]("March"); Break; Case 4: [Link]("April"); Break; Case 5:
[Link]("May"); Break; Case 6: [Link]("june"); Break; Case 7: [Link]("july"); Break; Case 8: [Link]("august"); Break; Case 9: [Link]("september"); Break; Case 10: [Link]("october"); Break; Case 11: [Link]("november"); Break; Case 12: [Link]("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; } [Link]("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; } [Link](i); [Link]("<br>"); } [Link]("The Number "+x+" is missing in above list"); </script> </body> </html>