Broad Network


Boolean Logic and Java Conditions

Java Basics – Part 7

Forward: In this part of the series I apply Boolean logic to Java conditions.

By: Chrysanthus Date Published: 2 Sep 2012

Introduction

This is part 7 of my series, Java Basics. In this part of the series I apply Boolean logic to Java conditions. For this part of the series, let us assume that a tall man has a height of 20dm and a short man has a height of 10dm. I have exaggerated the numbers for emphasis.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Single Expression Example
Consider the following code:

class SingleExp
    {
        public static void main(String[] args)
            {
                //tall means 20
                int me = 20;

                if (me == 20)
                    {
                        System.out.println("I am tall");
                    }
            }
    }

Read and try the code. In the condition, (parentheses of if) there is only one expression, which is, (me == 20). Since this expression results in true, the if-block is executed. The above if-construct is equivalent to

    if (true)
        {
            System.out.println("I am tall");
        }

For this second if-construct to be executed, you do not need the creation of the variable and its assignment. Read and try the following code:

class SingleExp2
    {
        public static void main(String[] args)
            {
                if (true)
                    {
                        System.out.println("I am tall");
                    }
            }
    }

Let us look at a case where the condition results in false. Consider the following code:

class SingleExpFalse
    {
        public static void main(String[] args)
            {
                //short means 10
                int me = 10;


                if (me == 20)
                    {
                        System.out.println("I am tall");
                    }
            }
    }

The if-block (curly brackets) in the above code will not be executed, because the condition results in false, since the value of the variable, me, is 10 for “short” and not 20 for “tall”. The above if-construct is equivalent to:

if (false)
    {
        System.out.println("I am tall");
    }

An if-block can only be executed if the condition is true. In this last case it is not executed, since the condition is false.

More than One Expression in Condition
You can have more than one expression in a condition. In this part of the series, I consider a maximum of two expressions in a condition. Each of the expressions results in true or false. The expressions are combined with the AND, OR or NOT operators. The AND operator is typed as, &&. The OR operator is typed as, || . The NOT Operator is typed as ! . &&, || , and ! are called logical operators. With logical operators, the rules in the previous part of the series can be rewritten as:

AND
(false) && (false) = false
(false) && (true) = false
(true) && (false) = false
(true) && (true) = true

OR
(false) || (false) = false
(false) || (true) = true
(true) || (false) = true
(true) || (true) = true

NOT
!(false) = true
!(true) = false

Double-Expression Examples
The if-block will not be executed in the following code:

class DoubleFalse
    {
        public static void main(String[] args)
            {
                if ((false)&&(true))
                    {
                        System.out.println("I am tall");
                    }
            }
    }

A practical example for the above code is:

class DoubleFalsePract
    {
        public static void main(String[] args)
            {
                //Tall means 20 and short means 10
                int you = 20;
                int me = 20;

                if ((you == 10)&&(me == 20))
                    {
                        System.out.println("I am tall");
                    }
            }
    }

20 is assigned to the variable, you, and also to the variable, me. The first expression in the condition results in false and the second one results in true. (false)&&(true) gives false as the effective boolean value for the condition. So the block is not executed.

The if-block will be executed in the following code:

class DoubleTrue
    {
        public static void main(String[] args)
            {

                if ((false)||(true))
                    {
                        System.out.println("I am tall");
                    }
            }
    }

A practical example for the above code is:

class DoubleTruePract
    {
        public static void main(String[] args)
            {
                //Tall means 20 and short means 10
                int you = 20;
                int me = 20;

                if ((you == 10)||(me == 20))
                    {
                        System.out.println("I am tall");
                    }
            }
    }

Read the above code. Try it. The first condition expression results in false; the second one results in true. The effective condition is true, since (false)||(true) gives true.

NOT Examples
The if-block will be executed in the following code:

class Opposite
    {
        public static void main(String[] args)
            {
                if (!(false))
                    {
                        System.out.println("I am tall");
                    }
            }
    }

The if-block is executed, if the condition is true. Now !(false) gives true.

A practical example for the above code is:

class OppositePract
    {
        public static void main(String[] args)
            {
                //Tall means 20 and short means 10
                int me = 20;

                if (!(me == 10))
                    {
                        System.out.println("I am tall");
                    }
            }
    }

else if and else
You can still add the else-if and else sub constructs to the above code samples, following what I taught in one of the previous parts of the series.

A tall man in your area may actually have the height of 1.8m and not 2m as given above. Also, a short man in your area may actually have a height of 1.4m and not 1m as given above. You may now ask the question, why did I not use 1.8 for tall and 1.4 for short and then use the double type instead of the int type for the above fields (variables). Java may not handle the double field type in a straightforward way (see later).

Let us stop here and continue in the next part of the series.

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message