CoSTARS Note #8:
Conditionals ("if" Statements)


1.  if

ok Not ok
if (x > 0)
   System.out.println("positive");
if x > 0
   System.out.println("positive");
  if (x > 0)
System.out.println("positive");
  if (x > 0);
   System.out.println("positive");
if (x > 0) System.out.println("positive"); if (x > 0) System.out.println("positive")
if (x > 0) {
   System.out.println("positive");
}
if (x > 0) (
   System.out.println("positive");
)
if (x > 0) {
   System.out.println(x);
   System.out.println(" is positive");
}
if (x > 0)
   System.out.println(x);
   System.out.println(" is positive");

2.  else if

ok Not ok
if (x > 0)
   System.out.println("positive");
else if (x < 0)
   System.out.println("negative");
if (x > 0)
   System.out.println("positive");
if (x < 0)
   System.out.println("negative");
System.out.println("User pressed " + ch); if (ch == 'A')
   System.out.println("User pressed A");
else if (ch == 'B')
   System.out.println("User pressed B");
else if (ch == 'C')
   System.out.println("User pressed C");
else if (ch == 'D')
   System.out.println("User pressed D");
else if (ch == 'E')
   System.out.println("User pressed E");

3.  else

ok Not ok
if (x > 0)
   System.out.println("positive");
else
   System.out.println("not positive");
if (x > 0)
   System.out.println("positive");
else
   System.out.println("negative");
if (x > 0)
   System.out.println("positive");
else if (x < 0)
   System.out.println("negative");
else
   System.out.println("zero");
if (x > 0)
   System.out.println("positive");
else if (x < 0)
   System.out.println("negative");
else if (x == 0)
   System.out.println("zero");
System.out.print(x);
if (x > 0)
   System.out.println(" is positive");
else
   System.out.println(" is not positive");

// or

System.out.print(x + " is ");
if (x <= 0)
   System.out.print(" not ");
System.out.println("positive");
if (x > 0) {
   System.out.print(x);
   System.out.println(" is positive");
}
else {
   System.out.print(x);
   System.out.println(" is not positive");
}

4 && (and)

ok Not ok
if ((x > 0) && (y > 0))
   System.out.println("both positive");
if (x > 0) {
   if (y > 0)
      System.out.println("both positive");
}
  if (x > 0 && y > 0)
   System.out.println("both positive");
if ((x != 0) && (y/x > z))
   System.out.println("yes!");
if ((y/x > z) && (x != 0))
   System.out.println("yes!");
if ((x > 0) &&
    (x < 10) &&
    (y > 0) &&
    (y < 10))
   System.out.println("in bounds");
if ((x > 0) && (x < 10) && (y > 0) &&
    (y < 10))
   System.out.println("in bounds");
if ((i >= 0) &&
    (i < s.length()) &&
    (s.charAt(i) == 'A'))
   System.out.println("yes!");
if ((s.charAt(i) == 'A') &&
    (i >= 0) &&
    (i < s.length())
   System.out.println("yes!");

// or

if ((i > 0) &&
    (i < s.length()) &&
    (s.charAt(i) == 'A'))
   System.out.println("yes!");

// or

if ((i >= 0) &&
    (i <= s.length()) &&
    (s.charAt(i) == 'A'))
   System.out.println("yes!");

5.  ||  (or)

ok Not ok
if ((x > 0) || (y > 0))
   System.out.println("one or both positive");
if (x > 0)
   System.out.println("one or both positive");
if (y > 0)
   System.out.println("one or both positive");
if ((x > 0) &&
    ((y > Math.sqrt(x)) || (z > Math.sqrt(x))))
   System.out.println("yes!");

// or

if ((x > 0) &&
    ((y > Math.sqrt(x)) ||
     (z > Math.sqrt(x))))
   System.out.println("yes!");
if ((x > 0) && ((y > Math.sqrt(x)) ||
    (z > Math.sqrt(x))))
   System.out.println("yes!");
 

6.  !  (not)

ok Not ok
if (x <= 0)
   System.out.println("not positive");
if (!(x > 0))
   System.out.println("not positive");
if (s.contains("abc"))
   System.out.println("red");
else
   System.out.println("blue");
if (!s.contains("abc"))
   System.out.println("blue");
else
   System.out.println("red");
if (b)
   System.out.println("true!");

// or

if (b == true)
   System.out.println("true!");
if (!!b)
   System.out.println("true!");

// or

if (b = true)
   System.out.println("true!");

// or

if (b != false)
   System.out.println("true!");
if (!b)
   System.out.println("false!");

// or

if (b == false)
   System.out.println("false!");
if (b = false)
   System.out.println("false!");

// or

if (!(b == true))
   System.out.println("false!");

// or

if (b != true)
   System.out.println("false!");

7.  nested if's

ok Not ok
if (x > 0) {
   if (y > 0)
      System.out.println("both positive");
}
else
  System.out.println("x is not positive");
if (x > 0)
   if (y > 0)
      System.out.println("both positive");
else
  System.out.println("x is not positive");

8.  Some Practice