Custom Search

Sunday, May 22, 2011

Intro to logic operators

In this post we will be covering logic operators, what they are, how to use them, and what they are most commonly used for. A logic operator is a statement that helps the computer decide what to do and when to do it. Logic operators can be used in a variety of things but are commonly used in if else statements.



When using an if else statements you have to put a condition in the parameters of the statement like so:



 public class Main {



public static void main(String[] args){

            int x=1;

            if(x==1){ System.out.println("x is 1");

                       

            }else{

                        System.out.println("x is not 1");

            }

}

}

Here you are asking the computer what x is and depending on what x is do something, but what if you have two conditions that need to be true? We can write two if statements to make something happen for both as seen below, but what if we only want it to print out “Hello World x + y” if x=1 and y=2?







public class Main {



public static void main(String[] args){

            int x=1;

            int y=2;

            if(x==1){

                        System.out.println("Hello World,x");

            }else{

                        System.out.println("Conditions are not true");

            }

           

            if(y==2){

                        System.out.println("Hello World ,y");

                       

            }else{

                        System.out.println("Conditions are not true");

            }

}

}

Well, that’s where logic operators come into play! With the power of a logic operator we can type something like this:





public class Main {



public static void main(String[] args){

            int x=1;

            int y=2;

            if(x==1 && y==2){

                        System.out.println("Hello World x + y");

            }else{

                        System.out.println("Conditions are not true");

            }

           

           

}

}



We can use a double & sign to only do something if both sides of the condition are true(x is 1 and y is 2).  Another great part is that we can add in as many double & signs as we want so if we wanted to make an example like this we could:



public class Party {



public static void main(String[] args){

            boolean tim = true;

            boolean bob = true;

            boolean jim = false;

            boolean john = true;

            boolean James = true;

            boolean Parker = false;

            if(tim == true && bob == true && jim == true && john == true && James == true && Parker == true){

                        System.out.println("Full Party!!");

            }else{

                        System.out.println("Some could not make it to the best party ever.....");

            }

           

           

}

}



Here we make a simple party application to test if everybody showed up.  We declare a bunch of boolean(true of false) names.  True if they showed up, false if they didn’t.  Some of the names are set to true some to false.  We don’t need to declare the ones that are false because if we don’t declare them true booleans have a default value of false, but we set some to false just because.  The end result will be “Some could not make it to the best party ever…….” Because some didn’t come.  How do we use logic operators some only some have to come? Well lets say that you only want your best buds to come over, but they all invited people also so really its fine if only James and Tim come over(because they are your best buds!), but its ok with you if some other friends to come over. To make a logic operator that only needs James and Tim to come over, but will accept other friends would look like this:







public class Party {



public static void main(String[] args){

            boolean tim = true;

            boolean bob = true;

            boolean jim = false;

            boolean john = true;

            boolean James = true;

            boolean Parker = false;

            if(tim == true && James == true || bob == true && jim == true && john == true){

                        System.out.println("Great party");

            }else{

                        System.out.println("Man.... hardly anyone showed up....    ): ");

            }

           

           

}





The computer will print out great party if only tim and James comes over OR if bob, jim , and john come over.  Otherwise hardly anyone came to your party.  The double pipe keys ( | ) are right below your backspace key on most keyboards and indicate an or statement.  So if this side of the argument is true do it, if the other side is true do it,  if only one side is true do it, if both are true do it.  If both sides are false don’t do it, but with the double & signs both sides must be true to continue the action.  Remember, one = sign is assigning a value two = signs are checking the value!

If you have any questions or ideas for future posts feel free to leave a comment below!  Well, that wraps it up for this post, see you in the next!

No comments:

Post a Comment