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 reader = new java.util.Scanner(System.in);
System.out.print("a: ");
double a = reader.nextDouble();
System.out.print("b: ");
double b = reader.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");
}

Exercises: Selection

https://codingbat.com/java

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,

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