Showing posts with label switch statement. Show all posts
Showing posts with label switch statement. Show all posts

Monday, 4 April 2016

Javascript - Switch statement


The JavaScript switch statement is used to execute one code from multiple expressions. It is just like else if statement that we have learned in previous page.

Syntax for javascript switch statement

switch(expression){
case value1:
 code to be executed;
 break;
case value2:
 code to be executed;
 break;
......

default:
 code to be executed if above values are not matched;
}  



Example:
<script>
var color='C';
var result;
switch(color){
case 'A':
result="one";
break;
case 'B':
result="two";
break;
case 'C':
result="three";
break;
default:
result="No Color";
}
document.write(result);
</script>

Output: three  

Here the default keyword specifies the code to run if there is no case match

Saturday, 4 January 2014

Switch Statement in Java

How to use Java Switch: Case: Default: 

Using switch statement

Swich:

The switch statement is Java’s multiway branch statement.It provides an easy way to dispatch execution to different parts of your code based on the value of an expression.It is an alternatives of else-if ladder. Switch allows you to choose a block of statements to run from a selection of code, based on the return value of an expression. The expression used in the switch statement must return an int, a String, or an enumerated value.