Thursday, January 26, 2017

Learn Java for Beginner easily ! part 11 : Switch Statement

In this tutorial I'm gonna teach you guys about "Swich" Statement. If you guys wondering what is Switch statement, it is one of keyword that tests a variable depending on the value pretty much which it can give you multiple choices. Instead of using the "if" statement, using the "switch" statement will be a lot of easier when you want to serve a lot of choices depending on the value of a variable. So before I explain about it further, I want to give you guys an example of a program that uses the switch statement below.

code :
========================================================================


     public class banana   //line 1,
  {
      public static void main (String args [ ])
    {
       int boy ;
       boy = 18;

       switch (boy){                        //line 8
       case 16 :                                //line 9
      System.out.println("You are not allowed to drive a car");
      break;                                // line 11
   
       case 17 :
      System.out.println("You are allowed to submit a driving license");
      break;
   
       case 18 :
      System.out.println("You are allowed to watch something");
      break;

       default:               //Line 21
       System.out.println("your age is not the list");
        break;

       }
   
     }
  }

========================================================================

So after you guys have typed the code above (remember to always type it, not copied it because sometimes it will cause undefined syntax error) , I want you guys to look at the result in the console box under your program (I hope you are using Eclipse). It will look exactly like this :

So the value inside the variable that the "Switch" statement identify is 18.That is the reason why is the keyword inside the "case 18" that being run by Java. As you guys can see inside the program, the keyword inside "case 18" is to print out "You are allowed to watch something" , the same text in the console box which is showing the program's result.

After you guys have run the program, I want to explain each line of the program above.I am going to start it at Line 8. Take a look at Line 8. In that line you will find a variable inside the Switch's parameters which is written as switch (boy) . The switch (boy) means that you want to run "switch" statement on variable "boy". So it's like you tell Java that you want to identify a variable using "switch" statement named "boy".

The next line I would like to explain is Line 9, now take a look at Line 9. The case 16 : means that you want to declared what to do in case if the variable's value is 16. Remember one thing that after you guys have typed case 16 , it will have to be followed by the regular colon ( : ) instead of semi colon ( ; ) that we usually use after a keyword. You also can put a couple of brackets after it, but it is fine even if you don't want to use brackets.

Next, take a look at line 11.There you will find break; . This code tells Java that the case have to be stopped there (the place where you typed break;) . Actually it kinda looks the same as the function of brackets, as a separator between each case.

The last line that I'd like to explain is Line 21. You will find default :  keyword in there. It has the same function as "else" statement that I have explain in Part 9 . So the default's function is in case there is none of your case that matches the variable's value, this default's statement will be excecuted by Java.

So that's it guys for now. Thank you guys for visiting my blog, hope you guys enjoy it and see you on the next tutorial. If you guys got something to be asked, please leave a comment below and thank you very much.

Greetings from Programmer !

No comments:

Post a Comment