Advertisements
Advertisements
प्रश्न
Explain switch case statement with example.
Advertisements
उत्तर
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)
{
` case label1:
statements1;
break;
case label2:
statements2;
break;
case labeln;
statements - N;
break;
default:
statements;
}
Working of switch statement:
The switch statement begins by evaluating an expression placed between parenthesis, much like the if statement. The result compared to labels associated with case structure that follow the switch statement. If the result is equal to a label, the statements in the corresponding case structure are executed.
The default structure is can be at the end of a switch structure if the result of the expression that do not match any of the case labels. The break statement is also used commonly within switch to exit the statement once the appropriate choice is found.
Example:
HTML CODE
<Html>
<Head>
<Title>Program - To test witch command in JavaScript </Title>
</Head>
<Body>
<script language="javascript" type="text/javascript">
var grade=0;
var marks=prompt(“Please enter your marks/100:”,”0”);
if(marks>90)
{grade=1;}
else if(marks>70)&&(marks<=90)
{grade=2;}
else if(marks>50)&&(marks<=70)
{grade=3;}
else if(marks>40v)&&(marks<=50)
{grade=4;}
else
{grade=5;}
switch(grade)
{
case 1:
document.write("Your Grade is Outstanding..");
break;
case 2:
document.write("Your Grade is Excellent..");
break;
case 3:
document.write("Your Grade is Good..");
break;
case 4:
document.write("Your Grade is Satisfectory..");
break;
default:
document.write("Your Grade Poor and have to re-appear Exam..");
}
</script>
</Body>
</Html>
The output will be



APPEARS IN
संबंधित प्रश्न
Which conditional statement is used to transfer control from current statement to another statement?
In which loop the condition is evaluated, before executing a statement?
<script type=“text/javascript”>
x = 6 + “3”;
document.write(x);
</script>
what will be the output?
List out the various branching statements in JavaScript?
Differentiate the break and continue statement.
Write the syntax for else-if statement.
What message will be displayed, if the input for age is given as 20, for the following snippet?
Write the output for the following program.
<Html>
<Head>
<Title> for statement</title>
<Head>
<Body>
<script language= "java Script" type = "text / javaScript")
var no1= prompt ("please enter table you want:", "0" );
document write ("<h2> multiplication for your need </h2>")
for (Var no2= 0; no2<=10; no2++)
{
document write (no1+ "x" + no2+ "=" + no1+no2+ "<br>);
}
</script>
</body>
</Html>Rewrite the following code using the if-else statement:
int m = 400;
double cl). = (m>300) ? (m/10.0) * 2; (m/20.0) - 2;Give the output of the following program segment:
int n = 4279; int d;
while(n>0)
(d=n%10;
System.out.println(d);
n=n/100;
}