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;
}
}
}
{
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 :
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