Take a decision !!!

Not knowing which decision to take can sometimes be the most painful...

In the world of programming, this is followed strictly, cause computer don't run by emotions, they run by logic. So, decision making is important in programming.

OK, I agree, its important to take decisions. I will take decisions but what is the point here to make me take decisions. This was supposed to be a Java discussion.
Wait dear, its not the end of the story. Let me get through it. Well, take an example of having sunglasses. You have pair of glasses, now you have to decide if you are going to wear them or not. Simply I can tell you, if its sunny outside, you will wear glasses else you are not going to wear them.

Hey man, its too much, any kid can tell that. Why are you telling me something that I already know ???
Hey, just go through the last part again, you can find if and else. Note that, they are in italics.
So, here it goes, Java can also take decisions for you using if-else.

Is this the way you talk to your friends and teach them ?
Unfortunately yes...
Either you hate me or love me, I don't care...

OK, go on, I think, I have got to know some of your attitude. I can manage !!!

Thanks a lot dear. Let's get our hands dirty. OK, let me show you a picture how it works in Java,
This is the simplest form of decision making,
Simplest if statement

When you want anything to be done only on a condition and don't do anything if this is not there in place, then use this construct.
For example, you are going out and check that outside is cloudy, you would like to carry your umbrella else you will carry on without bothering of umbrella.
Let's put that in a code and see how it looks like in Java,

 public class Main {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           if(weather.isCloudy()){  
                carryUmbrella();  
           }  
      }  
 }  

But it is not everywhere in your life, you are going to face situations where only a single condition would suffice. Now, let's take this to broader environment, You are going out and its sunny outside, you would love to wear your brand new Sun glass but if its cloudy outside, you are most likely to carry your umbrella. So, you can take two decisions depending on the condition.
So, its a high time we draw the second level of decision making,
Decision making using if-else
Now, put this in a code,

 public class Main {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           if(weather.isCloudy()){  
                carryUmbrella();  
           } else {  
                wearSunglass();  
           }  
      }  
 }  

Weather is not always sunny or cloudy, it may be a beautiful evening and you would love to take a walk. Now, you are in a situation where condition and consecutive workflow is not bound to two. Here you can take advantage of if-else-if ladder. Again, first go through the diagram how it works,
More than one condition
This is represented in code something like below,

 public class Main {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           if(weather.isCloudy()){  
                carryUmbrella();  
           } else if(weather.isSunny()) {  
                wearSunglass();  
           } else {  
                walkDownTheRoad();  
           }  
      }  
 }  

Ohh god, what are they ? What is isCloudy(), isSunny()?
Wait, I will let you know, they are nothing but boolean expressions, which evaluates to true/false.

The format for if-else construct is as follows,

           if(<boolean expression 1>){  
                //perform suitable task for condition 1  
           } else if(<boolean expression 2>) {  
                //perform suitable task for condition 2  
           } else {  
                //perform suitable task for other conditions  
           }  

And this can have any number of if-else-if ladder in it. the last else is also optional. So, basically you can end up with something like this too,


         if(<boolean expression 1>){  
                //perform suitable task for condition 1  
           } else if(<boolean expression 2>) {  
                //perform suitable task for condition 2  
           } else if(<boolean expression 3>) {  
                //perform suitable task for other condition 3  
           } else if(<boolean expression 4>) {  
                //perform suitable task for other condition 4  
           }  

So, what will happen if nothing comes under within the expression list?
It will do nothing. If any of the boolean expression matches, then only the corresponding code block will run, otherwise, it will simply ignore the code block and move on.

OK, I got a clear picture, each if block is for a special situation to be handled. Right ?

Yes dear reader, you are exactly correct. Each if block handles a special situation and when no special situation arises in your program, it will simply execute the general else (if provided) or will resume normal code flow. 

OK, I get it but what are boolean expressions?
Boolean expressions are anything that can have only two results, either true or false, like
  • if a number is greater that 0
  • if the length of the road is less than 1 km
  • if the answer is 'yes'
  • if the volume of the jar is 1 litre.
  • And many more....
OK I got it, if else can handle boolean expressions. My next question is, how to create a boolean expression?
Hmm, brilliant question, I would love to show my next article on this. Till then, find some real life scenarios, where you can use boolean expressions and put them in use with if-else.


Prev     Next
Palash Kanti Kundu

No comments:

Post a Comment