Wednesday, January 25, 2017

Learn Java for Beginner easily ! part 9 : If Conditional


"if" statement  is one of the feature in programming languange where you can have many statements to do according to a condition that you have set before. So this fuction will be useful when you want to make a program which contains more than one choices. Also, this program will always be a partner of "else" statement. "else" statement is a condition when there is none of your "if" condition that match with the condition of the variable.As an example, take a look on the code below.


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


     public class indonesia
  {
public static void main (String args [ ])
{
int test = 6;
if (test == 9)                            //  Line 7
     {  
System.out.println("It is 9");
        System.out.println("Try again");
      }

       if (test == 10)                             //Line 13
       System.out.println("It is 10");

else                                             // Line 16
System.out.println("It is not 9");

}

     }

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

First, take a look at Line 7. Inside the brackets after the "if" text is the condition that need to be required if you want to run the keyword inside the brackets. Remember that when you want to compare a variable to something else like integer number, you will have to use two equal sign (==) just like the code above. If you see a couple of curly brackets after the "if" conditional, that stand for the limit range of keywords that have to be excecuted if the "if" condition is fully required, but if there is only one line of keyword that have to be excecuted if the condition required, then you don't need to put a couple of brackets to limitate it just like what I did in the "if" statement on the Line 13. Also it is okay if you still want to put the brackets, it will cause nothing.

Now, take a look at Line 16,you will find the "else" statement there. The question is WHY don't you find the conditional that need to be required to run the keywords inside it. The Answer is because the function of "else" statement is as the default programs that will be excecuted when there is none of your "if" statement that required the condition. Then after knowing the different between "if" and "else" statement, I'm sure you all guys already know which statement that this program will run.

Yup, it is the "else" statement just like the picture below :


Also, in writing the conditional inside the brackets, you can use the greater than (>) and the smaller than (<) signs too. This will cause the choices in your program have so much variety. So that's it guys for now , I hope you are enjoying reading my blog. If you guys have any questions to be asked, leave a comment below and Thank you very much .

Greetings from Programmer !

No comments:

Post a Comment