JavaScript Resources
 
 
Categories
Misc

beginners php
programmershelp
programmingsite
software directory
Low Cost domains
Mobile phone business
Free stuff


 

Stores

 

 
Conditional Statements

Conditional statements are used to make decisions based on a condition using a variety of operators

if

This is the most common conditional statement that you will use . the syntax of the if statement is as follows

if (expression)
    statement ;

If the expression in the parentheses evaluates to true then the statement is executed , otherwise the statement is skipped . If you have two or more lines in the statement you should use the curly braces { } to surround the statements .

You can add the else keyword to provide various alternatives should the initial statement fail . Here is the syntax of the if...else version .

if (expression)
    statement 1 ;
else
    statement 2;

If the expression is true statement 1 executes otherwise , statement 2 executes .

Here is an example

<script language="JavaScript">
<!--
var age ;
age = prompt("what is your age");
if (age <=16)
	document.write("you are under age , please leave");
else
	document.write("welcome to the site");
		
//-->
</script>

else...if

This is used to replace nested if....else structures and makes your code more readable . Each else...if phrase is followed by an expression enclosed in parentheses . Use as many else...if statements as required . You use a final else statement to execute code when all the other conditionals are false .

Here is an example .

<script language="JavaScript">
<!--
var grade;
grade = prompt("Please enter your percentage", 50 );
if (grade > 90 )
	document.write("great performance");
else if (grade > 60 )
	document.write("good effort");
else
	document.write("poor show");		
//-->
</script>

switch

JavaScript has a switch statement as an alternative to using if...else statements . the syntax of the switch statement is as follows

switch (expression)
{
    case label1 :
        statement 1;
        break;
    case label2 :
        statement 2;
        break;
    default :
        statement 3;
}

The switch statement evaluates an expression placed between parentheses . The result is compared to the labels , the statement in the corresponding case structure are executed . A default structure can be used at the end of a switch structure to catch results that do not match any of the case labels . Here is an example .

<script language="JavaScript">
<!--
var grade;
grade = prompt("Please enter your grade" , "a");
switch (grade)
{
	case "a":
		document.write("great stuff");
		break;
	case "b" :
		document.write("good effort");
		break;
	case "d"  :
		document.write("poor stuff");
		break;
	default :
		document.write("invalid response");
}
//-->
</script>

Here are some things to note about the switch structure . You need a colon ( : ) after the label . You use curly brackets {} to enclose the switch structure . The keyword break is used to break out the entire switch statement once a match is found , thus preventing the default structure from being executed accidentally .

 

 

SPONSORS

© 2003 onwards programmershelp