I wanna decide what to do !!!

Taking the correct decision is important in times, where you have to take either of the ways. Correct decisions make life easier. But what controls a decision?
The situation.
Based on different situations, people take decisions. Your brain takes action based on the situation. Such is true for Java as well. Based on situation Java can take decisions too.

What ? I am confused.
Most of your articles tell me that, 'Its only you who knows what to do and how to do. Then what Java is doing here ? Don't you feel that you are contradicting yourself ?
Yes, its absolutely true that Java do not know what the flow it should be or what it should look. On the other hand it is also true that based on situations, Java can take decisions.

Ohh god, who are you ? Why are you again confusing me ? I will leave !
Well, keep calm. Close your eyes, take a deep breath.

Now again repeat after me, 'Java does not know how the flow looks like or what should be the next course of action but it can take decisions on situations. It is only you who is responsible for the whole flow'.

Now, let me unveil the story, from my previous article you know, using Java, you can control the flow of your program and take different course of action based on different situations. Now, when your program is executing on computer, you can not define the input to change the flow of it.

Can you ???
No obviously.

So, we need help of a special kind of expression, which is known as boolean expression. Let's find a simple definition of what it is.

Boolean expression: Expressions that evaluate to a boolean value are known as Boolean Expressions. Now, you know from this article, that boolean value can either be true or false. So, in its simple form if anything that has a value of true or false is a boolean expression.

OK, so let me think, the following are part of boolean expression,

  • Do I have a car ?
  • Do I have a house of my own ?
  • Do I live in India ?
  • Have I taken my lunch ?
Aren't they ?
Absolutely correct, all these evaluate to boolean value of either true or false. Its great to have a reader who thinks in real life scenarios.
(I want to thank one of my readers who asked me this question in the middle. Hope the person is reading this article now)

Well, there are millions of real life examples to perfectly fit with a boolean expression. You just start counting.

In the world of Java, we create boolean expressions by comparison of values or directly using the boolean literals or by operating multiple boolean expressions or using methods that returns boolean value.

Lets take all these one by one.

  1. Boolean expression by comparison of values: We can have situations where we need to take action based on the value of something. Something like, if you have scored more than 80% you are Grade A, if you are less than 80%, you are Grade B. So, basically we are comparing your score with a predefined Grade Scale to get the Grade.
    To form boolean expressions by comparing values, we can use four operators, <, >, =, !
    These operators can be arranged differently to have different type of comparison, Value of the left is compared with the value of right.

    < means 'Less than'
    > means 'Greater than'
    == mean 'equal to'
    <= means 'Less than equal to'
    >= means 'Greater than equal to'
    != means 'not equal to'

    Lets put everything in code,
 public class BooleanEvaluation {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           // Compare if 5 is less than 8  
           System.out.println("5 is less than 8: " + (5 < 8));  
           // Compare if 5 is greater than 8  
           System.out.println("5 is greater than 8: " + (5 > 8));  
           // Compare if 9 is equal to 8  
           System.out.println("9 is equal to 8: " + (9 == 8));  
           // Compare if 9 is equal to 9  
           System.out.println("9 is equal to 9: " + (9 == 9));  
           // Compare if 9 is less than equal to 8  
           System.out.println("9 is less than equal to 8: " + (9 <= 8));  
           // Compare if 9 is less than equal to 9  
           System.out.println("9 is less than equal to 9: " + (9 <= 9));  
           // Compare if 9 is less than equal to 10  
           System.out.println("9 is less than equal to 10: " + (9 <= 10));  
           // Compare if 9 is greater than equal to 10  
           System.out.println("9 is greater than equal to 10: " + (9 >= 10));  
           // Compare if 9 is greater than equal to 10  
           System.out.println("9 is greater than equal to 10: " + (9 >= 10));  
           // Compare if 9 is greater than equal to 8  
           System.out.println("9 is greater than equal to 8: " + (9 >= 8));  
           // Compare if 9 is not equal to 8  
           System.out.println("9 is not equal to 8: " + (9 != 8));  
           // Compare if 9 is not equal to 9  
           System.out.println("9 is not equal to 9: " + (9 != 9));  
      }  
 }  
The output is as follows,

 5 is less than 8: true  
 5 is greater than 8: false  
 9 is equal to 8: false  
 9 is equal to 9: true  
 9 is less than equal to 8: false  
 9 is less than equal to 9: true  
 9 is less than equal to 10: true  
 9 is greater than equal to 10: false  
 9 is greater than equal to 10: false  
 9 is greater than equal to 8: true  
 9 is not equal to 8: true  
 9 is not equal to 9: false  

          2. Boolean expression by literals: Its just using the true or false value directly in code                         without evaluating any boolean expression.

          3. Boolean expression by operating multiple boolean expressions: if we have multiple                       boolean expressions and we want the final result to be a single boolean value by operating.
              There are four operations supported on boolean expressions, 'and', 'or', 'exclusive or' and                   'not'. We will discuss this one in great detail in a new article. Please hold on.
           4. Boolean expression using methods that returns boolean value: In cases, it can happen                  that, your method returns a boolean value and you can directly use that method to get a                      boolean expression. If you recall from this article, we have to define a return type for every                method, Now, if you have a method whose return type is boolean, then this method                            invocation itself is a boolean expression.

Well, for now keep remembering this and try to find different boolean expressions by process 1 and process 2 discussed above. Very soon, I will discuss how to achieve  Boolean expression by operating multiple boolean expressions.

Till then, enjoy writing your own boolean expressions !!!



Prev     Next
Palash Kanti Kundu

No comments:

Post a Comment