Wednesday, January 25, 2017

Learn Java for Beginner easily ! part 10 : Multiple Conditional

So in the tutorial before ( Part 9 ) , I have thaught you guys how to make an "if" statement with one conditional inside it. In programming world, there will be a moment where you want to make a bunch of keyword's to do (after declarating "if" statement) by giving it more than one condition to be required so that this program can be excecuted. Well, Actually you can use the "if" statement with more than one conditional inside it. I'm going to show you guys how to do it by examples, just like how as I usualy do it. In this case for example, imagine that you are making a simple dating website with the age of gender as the indicator of agreement and disagreement or simply called match or not match.Wanna know more about it ? Go check the codes below :





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


     public class dating   //line 1
  {
public static void main (String args [ ])
{
int boy = 19;
       int girl =  70;
if (boy < 25 && girl <23)                            //  Line 8
     {
System.out.println("You are match");
        System.out.println("Congratulations !");
      }

      
else                                             // Line 15
System.out.println("Sorry, you are not gonna date an old cougar ");

}

     }

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

So as you guys can see on the codes above. I wrote two different conditional with two different variables in one "if" statement. What I'm gonna explain to you guys is the logical operator that written on the Line 8. Take a look at Line 8, you guys are going to found a "&&" signs. In a daily conversation we read it as "and" sign . Actually, in this programming languange, the meaning is not really different. The "&&" sign that you guys found as the separator of  first condition and the second condition means that the keywords contained in this conditional will be excecuted if those two first and second conditional are fully required. Otherwise, if even one of the conditional is not required, then the bunch of keywords inside the "if" statement will not be excecuted.

On the program above,because the boy is 19 and the girl is 70, errr actually we don't call 70 y/o woman with "girl" anymore, I hope it is not happening to me knowing the fact that I'm 19 y/o.But it is okay considering it as only as an example.It means that one of the condition in the "if" statement is NOT required, so the keywords inside it would not be excecuted. This is the result after you run the program above.



Then there is one more logical operator that I want to show to you guys.It is the "||" signs. This is the sign that you want to use as the separator between two different condition of your "if" statement in case if you want the keywords to be excecuted even though there is only one of the condition that are required. I am gonna show you guys the example of it on the codes below.

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


     public class dating   //line 1
  {
public static void main (String args [ ])
{
int boy = 19;
       int girl =  70;
if (boy < 25 || girl <23)                            //  Line 8
     {
System.out.println("You are match");
        System.out.println("Congratulations !");
      }

      
else                                             // Line 15
System.out.println("Sorry, you are not gonna date an old cougar ");

}

     }

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


Then this is the result after you run the codes above :



See ? eventhough there is only one of the conditional that are required, the Java still excecuted the "if" statement's keyword. Now you guys must have known the different between the "&&" operator and the "||" operator. So 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