Paul Baumgarten

Computer Scientist, teacher, freelance programmer and self confessed geek

Java: Selection ("if")

Having determined how to have Java compare values one against the other, we can now build on that by using if statements to selectively execute code based on the result of the comparison.

The overall syntax is:

if ( condition ) {
   doSomething;
   doSomething;
} else if ( condition ) {
   doSomething;
   doSomething;
} else {
   doSomething;
   doSomething;
}

Let's make a simple example:

boolean result;
java.util.Scanner keyb = new java.util.Scanner(System.in);
System.out.print("a: ");
double a = keyb.nextDouble();
System.out.print("b: ");
double b = keyb.nextDouble();

if ( a == b ) {
    System.out.println("a and b are equal");
} else if (a < b) {
    System.out.println("a is less than b");
} else {
    System.out.println("a is greater than b");
}

Problem set: Selection

Visit https://codingbat.com/java and complete the Logic-1 and Logic-2 exercises.

Note: To pass the CodingBat auto-marker, your code will need to return an answer rather than print it using a function like System.out.println().

For example, to provide a correct answer to the cigarParty problem, use return() as below.

public boolean cigarParty(int cigars, boolean isWeekend) {
  if (isWeekend && cigars >= 40) {
    return(true);
  } else if (cigars >= 40 && cigars <= 60) {
    return(true);
  } else {
    return(false);
  }
}