Programming Fundamentals: Conditional Statements (IF..THEN..ELSE)

In the prior sections, expressions combining Boolean operators and comparison operators were described. In this section, we demonstrate how such expressions are written and evaluated in computer programming languages.
Control statements “control” which sections of code in a program are executed. For example, we might want a set of statements to execute only if certain conditions are met, otherwise, the statements would not be executed. There are three general types of control statements:
  1. Sequential – The default ordering of execution
  2. Decision (Conditional) – controls which block of code within several alternatives is executed.
  3. Iterative – controls how many times a block of code is executed.
Decision or Conditional statements are a type of Control statement and are often referred to as IF..THEN..ELSE statements as can be seen in these examples:
Programming LanguageConditional Code
“C”, C++, Java, Perl
if (condition) {
   statements
} else {
    else_statements
}
Pascal
if (condition) then
begin
   statements
end;
Oracle PL/SQL
if (condition) then
   statements
else
   statements
end if;
Basic
If condition Then
   statements
Else
   statements
End If
In each of the languages, the condition portion can be an arbitrarily complex boolean expression (using variables declared in the program) that will evaluate to either True or False. Ifcondition evaluates to True then the statements will be executed. If condition evaluates to False, then the else_statements will be executed if any are given.
In the following examples, a variable department is defined and a value is assigned to it. A conditional statement then determines which message should be displayed.
LanguageExample program
“C” or C++
#include <stdio.h> 
main() { 
      int department; 
      department = 5; 
      if (department == 5) { 
         printf("Employees should be paid more than 25,000"); 
      } else { 
         printf("Employees should be paid exactly 40,000"); 
      } 
}

Perl
$department = 5; 
if ($department == 5) { 
   print "Employees should be paid more than 25,000"; 
} else { 
   print "Employees should be paid exactly 40,000"; 
}
Oracle PL/SQL
BEGIN 
   DECLARE 
     department INTEGER; 
     department = 5; 
     IF (department = 5) THEN 
        DBMS_OUTPUT.PUT_LINE('Employees should be paid more than 25,000'); 
     ELSE 
        DBMS_OUTPUT.PUT_LINE('Employees should be paid exactly 40,000'); 
     END IF; 
   END;
Java
class myexample { 
   public static void main (String args []) { 
      int department = 5; 
      if (department == 5) { 
         System.out.println ("Employees should be paid more than 25,000"); 
      } else { 
         System.out.println ("Employees should be paid exactly 40,000"); 
      } 
   } 
}
SQL
SELECT salary FROM employees_table WHERE department = 5;
As an exercise, determine what the output should be from the following Java programs:
ExampleJava Program
1
class myexample1 { 
   public static void main (String args []) { 
      int bill_department = 4; 
      int bill_salary = 40000; 
      int alice_department = 5; 
      int alice_salary = 51000; 
      if ( (bill_department == 4) & (alice_department == 5) ) { 
          System.out.println ("This statement is True!"); 
      } else { 
          System.out.println ("This statement is False!"); 
      } 
   } 
}
2
class myexample2 { 
   public static void main (String args []) { 
      int bill_department = 4; 
      int bill_salary = 40000; 
      int alice_department = 5; 
      int alice_salary = 51000; 
      if ( (bill_salary < 40000) || (alice_department >= 5) ) {
         System.out.println ("This statement is True!"); 
      } else { 
         System.out.println ("This statement is False!"); 
      } 
   } 
}
3
class myexample3 { 
     public static void main (String args []) { 
        int bill_department = 4; 
        int bill_salary = 40000; 
        int alice_department = 5; 
        int alice_salary = 51000; 
        if ( (alice_department == 5) || (alice_salary < 25000) || (bill_salary >= 40000) ) { 
           System.out.println ("This statement is True!"); 
        } else { 
           System.out.println ("This statement is False!"); 
        } 
   } 
}