Home | | Internet Programming | Control Structures

Chapter: Web or internet Programming : Scripting Elements

Control Structures

In JavaScript we have the following conditional statements:

Control Structures

 

Conditional Statements

In JavaScript we have the following conditional statements:

       if statement - use this statement to execute some code only if a specified condition is true

 

       if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false

 

       if...else if....else statement - use this statement to select one of many blocks of code to be executed

 

      switch statement - use this statement to select one of many blocks of code to be executed

 

Example:

 

<script type="text/javascript"> var d = new Date()

 

var time = d.getHours() if (time<10)

{

document.write("<b>Good morning</b>");

 

}

else if (time>10 && time<16)

 

{

document.write("<b>Good day</b>");

 

}

else

 

{

document.write("<b>Hello World!</b>");

 

}

</script>

 

Switch Statement

Use the switch statement to select one of many blocks of code to be executed.

 

Example:

 

<script type="text/javascript">

 

//You will receive a different greeting based

//on what day it is. Note that Sunday=0,

//Monday=1, Tuesday=2, etc.

var d=new Date(); theDay=d.getDay(); switch (theDay)

 

{

case 5:

 

document.write("Finally Friday"); break;

case 6:

 

document.write("Super Saturday"); break;

case 0:

 

document.write("Sleepy Sunday"); break;

default:

document.write("I'm looking forward to this weekend!");

 

}

</script>

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : Scripting Elements : Control Structures |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.