In this article we will discuss implementing switch statement. The switch statement is used to perform different actions based on different condition. In order to improve the readability of a program multiple if else statements can be replaced with a switch statement.
Syntax:
switch(expression) {
case n:
// code block
break;
case n:
// code block
break;
default:
default // code block
}
Example:
<script type="text/javascript">
var input = Number(prompt("Please enter a number", ""));
switch (input)
{
case 1:
alert("You have entered One");
break;
case 2:
alert("You have entered Two");
break;
case 3:
alert("You have entered Three");
break;
default:
alert("Your have to enter number between 1 and 3");
break;
}
</script>
Output:
Post your comments / questions
Recent Article
- How to decode HTML character code in c#?
- How to save the xml file to a particular location?
- How to use if else statement in c++?
- How to use godaddy domain name with another godaddy hosting account?
- Restore of database 'DATABASE' failed. (Microsoft.SqlServer.Management.RelationalEngineTasks)
- How to programmatically modifying the AppSetting value in web.config using c#?
- TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
- How to calculate the age from jQuery ui datepicker using c#?
Related Article