Java & J2EE Conditionals, Expressions and Other Java Operators |
As I recall, by some fluke, you had ended up with more muscles than me (*cough* steroids *cough*) and I put a call into my buddy at the Steroid Usage is a No No Board. To determine if you were a liar, they would have to ask you a series of Boolean questions: True or False -- did you do steroids? Yes or No -- Are you Barry Bonds? Booleans are black and white; for them, there is no in between. It's either true or false, yes or no, on or off. We will get into how to use them in programming a bit later on. For now take comfort in the fact that they exist. Conditional Operators Sometimes you need to know if more than one criteria has been met, or if one of the two criteria has been met. To do that, use conditional operators. class IamBetter { public static void main(String[] args) { int my_muscles = 10; int your_muscles = 10; int my_looks = 10; int your_looks =10; if((my_muscle > your_muscle) && (my_looks > your_looks)) System.out.println("I really AM better than you in every way!"); In the above example, we are asking the computer if my looks and muscles are better than yours. As you can see by the values, they are. Because both criteria are met, the program will print to the screen: I really AM better than you in every way! If only one criteria had been met, it would have printed nothing. Let's say again, that because of your heavy steroid use and because my Pilates exercise builds lean muscles instead of those stupid looking ones that you have, that you appear to have more muscles than me. It wouldn't matter; I can make the program say that I am still better than you. class IamBetter { public static void main(String[] args) { int my_muscles = 10; int your_muscles = 11; int my_looks = 10; int your_looks =10; if((my_muscle > your_muscle) || (my_looks > your_looks)) System.out.println("I really AM better than you in every way!"); Now that I have changed the criteria so that only one match will suffice, it will still print that I really AM better than you in every way! Again, if neither criteria was true, it would have printed nothing.
blog comments powered by Disqus |
|
|
|
|
|
|
|