Showing posts with label switch. Show all posts
Showing posts with label switch. 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, 2 February 2013

Introduction to Control Statements in Java


Control statements decide flow of a program

JAVA CONTROL STATEMENTS

if, if-else, switch, nested if, switch, for, while, do-while, break, continue and return control statements


Control statements are used in programming languages to cause the flow of control to advance and branch based on changes to the state of a program. The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.
In Java, control statements can be divided under the following three categories: