Thursday, December 4, 2014

Switch the context !!!

Decision making is an important feature of any application. Everything works in algorithm, as I mentioned in the article on operators. Algorithms also deal with logic and decision making. 

Wait, Java already has it in built and we know about them What's the catch then ? 
Well, Java provides if-else for decision making feature. But we've another way to do it in Java. 

Again a new way ? 
What is the need ? 
Well, it provides a more performance boost while working with logical structures. Also provides a better readability of the code. 

How can you tell about the performance ? 
I will discuss this one later and if you are an advanced reader, you can read it here. 

Well, I agree. Let's check the structure. 
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. 

The syntax of switch is: 
    case value : 
       break; //optional 
    case value : 
       break; //optional 
    //You can have any number of case statements. 
    default : //Optional 

  • The variable used in a switch statement can only be a byte, short, int, or char. Recently Java added support for Strings as well in switch statements. I have written something on it here. 
  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon. 
  • The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal. 
  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. 
  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. 
  • Not every case needs to contain a break. If no break appears, the flow of control will fall through  to subsequent cases until a break is reached. 
  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case. 

Let's put everything in code,
  * Copyright 2014-2015 Palash Kanti Kundu.  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Mar 5, 2015  
 public class SwitchTest {  
       * @param args  
      public static void main(String[] args) {  
           int i = 1;  
           switch (i) {  
           case 1:  
           case 2:  
           case 3:  
                System.out.println("Invalid number");  

Determining the output is a task for you. Go, get your hands dirty with Java.

With this we are almost done with handling the logic and looping. Almost all programming languages deal with these basic features. The difference is only in the syntax. These are pretty much required knowledge for any programming. We are leaving with two keywords continue and break.
After that we are going to deep dive into OOP features of Java. 

Stay tuned, next levels are way more interesting than this one. You will feel the real power of Java in the next sections. 

Prev     Next
Palash Kanti Kundu

No comments:

Post a Comment