CSCI 201 Introduction to Algorithm Design
home | homework index | labs index 
FALL 2006  

Conditional Execution and Relational Operators

[an error occurred while processing this directive]

This lab will test your ability to use Java's conditional statements and relational operators while you draw some flags.

The if-statement

One of the simplest form of conditional statement is the if statement. This statement directly translates into: if a test is true, then do this, else do that - decisions that you make each and every day of your life. Some examples:

The last statement above is very close to what computers do, here is that statement translated into Java:

  if ( X > 5 ) {                 // IF X is greater than 5
      System.out.println( X );   // THEN Output X
  } else {                       // ELSE
      X = X + 1;                 // Add 1 to X
  }

Notice, first comes the word if, then a test within parenthesis, then the true, or then, block of code enclosed in curly braces ({}),  then the false, or else, block of code enclosed in curly braces. The else portion of the statement can be omitted, in which case, nothing is done if the condition is false.

As it turns out, in those cases in which either the true or else block is a single statement long, the curly braces can be omitted. This allows the conditional statement shown above to be re-written as follows:

  if ( X > 5 )                   // IF X is greater than 5
      System.out.println( X );   // THEN Output X
  else                           // ELSE
      X = X + 1;                 // Add 1 to X

Unfortunately, even experienced programs can make mistakes when they avoid the braces, so we suggest you try to use them, especially when writing your first few programs.

Programming with style

It is very important that you indent, with a consistent number of spaces, the then and else blocks of your code. Condition expressions with messy indentations are extremely difficult to comprehend and are always the sign of a poorly designed program. Turning in a programming assignment with poor indentation is certain to lower your grade.

if ( X > 5 ) {
  System.out.println( X ); 
}
else {
        X = X + 1;
}

Stack N' Heap

Step through the following example to see a couple of conditional statements in action.

Code
int test = 15;

if (test > 30) {
    System.out.println("one");
}

if (test == 15) {
    System.out.println("two");
} else {
    System.out.println("Not two");
}
Stack
Heap
Temporary

Relational operators

What makes conditional statements so powerful is that almost any code can go in the then or else blocks. But to use an if statement, you must know how to correctly specify a test. The test, which is always placed in parenthesis, must be an expression that evaluates to either a boolean values, that is, true or false. Relational operators are binary operators that compare two values and generate a boolean result. The following table contains the relational operators of Java.

x < y x is less than y
x <= y x is less than or equal to y
x > y x is greater than y
x >= y x is greater than or equal to y
x == y x is equal to y
x != y x is not equal to y

Logical operators

But that's not all. You can also use logical operators to create conditions. They allow you to execute blocks under more complex conditions, such as when Income is greater than 15000 and Income is less than 40000. Java pretty much sticks with the the same three operators defined by George Boole by 1854 in "An Investigation Into the Laws of Thought, on Which are founded the Mathematical Thories of Logic and Probabilities". However, Java requires these operators to be written in a most peculiar fashion.

P && Q True only when both P and Q are true
P || Q True if at least one of P and Q are true
! P True only if P is false

Because && and || have a lower level of precedence than the relational operators, you can write Java tests like "Income > 15000 && Income < 4000" without using parenthesis. Following the convention of decades of logicians, && has a higher precedence than ||.

Downloading the project framework

Download Flag.zip, a ZIP file containing a NetBeans project named Flag and unZIP this project into your csci/201 directory. Try to make your Projects panel look something like the following picture before continuing.
Initial Flag Projects Panel

Checkpoint

Answer the questions below. You have to answer all questions correctly to see the rest of the lab.

Which of the following codes compares a variable x with 0 in Java?
x = 0
x != 0
x = = 0
x == 0

Which one of the following is a valid boolean expression that is true if x and y are both between (and not equal to) 15 and 25?
15 < x < 25 && 15 < y < 25
x > 15 && x < 25 && y > 15 && y < 25
x > 15 && x < 25 || y > 15 && y < 25

Select a correct if-statement from the following:

(if x > 0) {
   ++x;
}

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

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