Skip to main content

Conditional Statements

If I give you a number and asked you to find whether it is even or odd. 

You can easily answer the question as we know how to check whether the number is even or odd.


There are several techniques for that :

* Method 1 : If the last digit of the given number is [0 or 2 or 4 or 6 or 8], then it is an even number, otherwise it is an odd number.

* Method 2 : If the given number is exactly divisible by 2, then it is even number, otherwise it is an odd number. We can check whether the number is exactly divisible by another number by using the "%" (remainder after division) operator. If the remainder is 0, then the first number is exactly divisible by the second number. 

* Method 3 : The simplest way is, find the bitwise AND of the given number with 1. If the result is 1, then the given number is odd, if the result is 0 it is even.

Let me explain, how this works,

Ex : Given number 6

Here we are considering only 8 bits as the numbers are small. Internally an integer value is stored in 32-bits.

00000110 (binary representation for number 6)

00000001 (binary representation for number 1)

If we perform the bitwise AND (&) between those two numbers, we will get the result,

00000000 (binary format)  => 0 (decimal format)

As the result is 0, the number is even. Try the same for any odd number and you will get the result 1.

If you don't know how to perform bitwise AND, do search in Google about that as I don't want to move out of the topic.


If you observe any of the methods above, it is like taking decision. If the given number satisfies the condition for even number, we display it as even number, otherwise we will display it as odd number.



IF - ELSE STATEMENT :


In java, you can take decision using "if" and "else" statement.

Syntax : 

if (boolean value){

    // statements

}

else

if (boolean value){

    //statements

}

.

.

.

else{

    //statements

}


If condition takes a boolean value or any expression which returns a boolean value. For example comparision operators return boolean values.

The last "else" statement is not mandatory.

If there is only one statement inside the "if" or "else" block, you can remove the flower braces.



We can simplify the above program as :




Now, we will solve the same problem using the first approach as discussed in the beginning.

If the last digit of the number is 0 or 2 or 4 or 6 or 8, then it is even number, otherwise it is odd number.

Fine ! But how to find the last digit of a number ?

We can get the last digit of any number by applying "%" (remainder) operator by 10.

For example, the number is 123

123 % 10  => returns the remainder after dividing 123 by 10. The remainder is 3 which is the last digit of that number.

We got it !!! Let's apply that concept.

This method is not better than the method 2. But we are doing this just to gain knowledge over conditional statements.




We can use the logical operators to simplify the above program. If the image is not clear, do click on that and zoom it !




CONDITIONAL OPERATOR (? :)

In the operators concept, we didn't discuss this operator. 

The conditional operator is same as the "if" and "else" with slight variation.


Syntax :

variable  = boolean value ?  value1 :  value2 ;

The operator, checks the boolean value. If it is true, value1 will be stored in variable, otherwise value2 will be stored in the variable.






Comments