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 create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article