The If-Else Construct

Home Up Search Java 2 API C++ Resources

The if-else construct is one of the fundamental ways in which logical decisions are translated into behaviors (polymorphism is the other way),     The backbone of procedural programming, the if-else statement is avoided in pure OOP because it represents a executed logic approach to correct behavior that relies on proper coding of logic rather than a structural approach that uses polymorphism to create behavior because objects already "are" the proper type that already know how to do the proper thing.

Nonetheless, the if-else construct is a very handy tool when it would be too much trouble and too little risk to create an architected solution.    If-else constructs are also implemented directly in the machine code of the computer's processor, so they are generally quite fast, though not so much faster than a polymorphically dispatched call as people may think. 

Syntax:

if ( [boolean value])
{
       [statements to execute if true go here]
}
else
{
      [statements to execute if false go here]
}

The if-else relies on the "boolean" primitive data type, which has the values of either true or false (both Java keywords).

The boolean value can be returned by a method, or by executing one of the following primitive comparison operations:

           >, <, >=, <=, ==, !

between two primitives (ints, floats, doubles, chars, booleans).

The meanins are as they seem, though "==" means logical equals, and "!" means the negation, i.e. turns true into false and vice versa.

Note that the logical equals "==" is not the same as the assignment equals "="!!!!

The root Object class contains a method  "equals" which takes another object and returns a boolean value.   Classes derived from Object, as all are, should override the equals() method with their own implentations to correctly determine if two objects are indeed equal.

For classes supplied by Java, this has already been done.

If there is no code needed for the "else" portion of the construct, then it can be omitted.   Typically this is done if something only needs to be done if the boolean value is true and nothing is done if the value is false.

It is also true that if only one line of code is needed, the curly braces can be eliminated, though this is highly not recommended practice as it can lead to errors when the code is modified.

Examples:

if(x>3)
{
    x = 0;
}
else
{
    x++;
}

if(myObject.equals(yourObject))
{
    doSomethingNice();
}
else
{
   complainAboutIt();
}

if(3.1415926 == pi)
{
   errorString = "not enough digits!";
}

if(jRadioButton.isSelected())
{
   jRadioButton.setSelected(false);   
   
System.out.println("You should have done this with polymorphism!";
}

Programming Tip:

If your are comparing two values for equality and one is a constant, put the constant on the left side of the boolean statement.    This will prevent accidental assigning of the constant value to the other variable if a single "=" is typed instead of the intended "==".    The compilers often do not catch this error (C++ compilers never do) and it can lead to very strange problems that are very difficult to track down.

if(x=true)    // assigns the value true to the variable x.    The true clause is always executed -- not what was intended.

if(true=x)    // Always generates a compiler error like "attempt to assign value to a constant" -- error caught right away.