Skip to main content

Control Statements in Java (Part 2)

Welcome back to my blog!!! In my previous post you learned about if, nested ifs and else-if-else in Java. In this post you will learn about switch.
What is a switch? In Java, a switch is a multiway branch statement.

General form of a switch:

switch(expression) {
case value1: statement;
                     break;
case value2: statement;
                     break;
.
.
.
.
.
.
default: statement;
              break;
}


The expression must be the type of int, byte, short or char. Duplicate case values are not allowed.
The switch statement works like this: the value of the expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following the case statement is executed. If none of the constants matches the value of the expressions, then the default statement is executed. however, the default statement is optional. If no case matches and no default is present, then no further action is taken.
The break statement is used inside the switch to terminate a statement sequence. When a break statement is encountered, execution branch is to the first line of the code that follows the entire switch statement. This has the effect of "jumping out" of the switch. Given below is a simple example of switch: 


class switch {
public static void main(String[] args) {
     for(int i = 0; i<6; i++)
     {
             case 0: System.out.println(" i is zero");
             break;

             case 1: System.out.println("i is one");
             break;

             case 2: System.out.println("i is two");
             break;

             case 3: System.out.println("i is three");
             break;

             default: System.out.println("i is greater than 3");
             break;
                     }
                }
         }


Nested switch statements:

You can use a switch as part of the statement sequence of an outer switch. This is called a nested switch. Since a switch statement defines its own block, no conflicts arise between the case constants in the inner switch and in the outer switch. For example, the following fragment is perfectly valid:


switch(count) {
case 1:
switch(target) {
case 0:
           System.out.println("Target is zero");
             break;
case 1:
           System.out.println("Target is one");
             break;
}
break;
case 2: . . . . . .
}



Thank you for coming to my blog. Now you have learned about switch in Java. If you find this post useful, please share it with your friends and colleagues.

FEEL LIKE A PROGRAMMER!!!

Comments

Popular posts from this blog

VLOOKUP in MS Excel

VLOOKUP in MS Excel H ello and welcome back to Learn Java. It's been very long since I wrote my last post. This post will give you a short and simple tutorial on using VLOOKUP in MS Excel. Verticle Lookup or VLOOKUP is a function in MS Excel that helps a user look for a specific value in a column   based on another particular value in the first column. A simple example: Your boss asks you to find the price of a food item based on its name. Syntax: VLOOKUP(the value you want to look up, where you want to look for it(array/column/table), the column number the value is in, return an approximate or exact match) Let's try to understand it more clearly: Here's a little data set containing a table of my favourite food items.  Step 1: Select a specific cell/text value you would like to search for in the given table/data set. Here, we are looking for the price of Pizza, so we use cell value "Pizza" which is also cell reference A3. We are making sure to use a cell value/re

Simple Java program

J ava is a high level programming language which is used almost everywhere- in Android mobile phones, set top boxes, in Google Maps, video games, etc. Today let's see how to make the simplest Java program- "Hello, world" in Eclipse. Step #1. Select your file location where you want to save your work. Step #2. Select File>New>Project as shown in the picture below: Step #3. Select Java>Java Project>Next as shown below: Step #4. Enter the project name in Project name as shown below: Step #5. Select Finish. Step #6. Select Yes. Step #7. Double click one the project name. You will see a folder named as "src" under the the project HelloWorld. Step #8. Right click on the folder "src">New>Class. Step #9. Enter the name of the class. Then click on Finish. Step #10. You will see a screen like this: Step #11. Write the following code: public class Hello { public static void main(String[] args) { System.out.println("Hello, world!!"); }