Licence.. ! OK Chapter Pages . .
. else if Statement: The if ... else statement evaluates an expression and executes one of two pieces of code, depending on the outcome. The else if statement to specify a new condition if the first condition is false.
if (n == ) { // Execute code block # } else if (n == ) { // Execute code block # } else if (n == ) { // Execute code block # } else { // If all else fails, execute block # } <Html> <Head> <Title>Program - To test else ..if command in JavaScript </Title> </Head> <Body> <script language="javascript" type="text/javascript"> var marks = prompt("Please enter your Marks/ :", " "); if(marks> ) { document.write("Your Grade is Outstanding.."); } else if((marks> ) && (marks<= )) { document.write("Your Grade is Excellent.."); } else if((marks> ) && (marks<= )) { document.write("Your Grade is Good.."); } else if((marks> ) && (marks<= )) { document.write("Your Grade is Satisfectory.."); } else { document.write("Your Grade Poor and have to re-appear Exam.."); } </script> </Body> </Html> Illustration . Using Logical Operators and else if Statement Chapter Pages The output will be There is nothing special about this code. It is just a series of if statements, where each following if is a part of the else clause of the previous statement. Using the else if idiom is preferable to, and more legible than, writing these statements out in their syntactically equivalent, fully nested form: if (n == ) { // Execute code block # } else { if (n == ) { // Execute code block # } else { if (n == ) { // Execute code block # } else { // If all else fails, execute block # } } } Chapter Pages .
. . switch case Statement: JavaScripts offers the switch statement as an alternate to using if...else structure. The switch statement is especially useful when testing all the possible results of an expression.
The syntax of a switch structure as the following: switch(expression) {