Nested Ifs

Brad Cupp
2/4/2014

The stopifnot function


stopifnot(logical)

  • If logical is true, nothing happens
  • If logical is false, returns an error

The stopifnot function

> x <- 4
> stopifnot(x < 8)
> Error: x < 8 is not TRUE

The stopifnot function

> x <- 9
> stopifnot(x < 8)
> 

Strongest assertions

if (salary > MIN) {
  if (salary <= MAX) {
    tax = (salary - MIN) * LOW_RATE;
  } else {
    tax = (MAX - MIN) * LOW_RATE
        + (salary - MAX) * HIGH_RATE;
  }
  message = “Your taxes are ...”;
} else {
  message = “You pay no taxes.”;
}

Strongest assertions

if (salary > MIN) {
  if (salary <= MAX) {



    tax = (salary - MIN) * LOW_RATE;
  } else {



    tax = (MAX - MIN) * LOW_RATE
        + (salary - MAX) * HIGH_RATE;
  }



  message = “Your taxes are ...”;
} else {



  message = “You pay no taxes.”;
}

Strongest assertions

if (salary > MIN) {
  if (salary <= MAX) {

    stopifnot(MIN < salary && salary <= MAX)

    tax = (salary - MIN) * LOW_RATE;
  } else {



    tax = (MAX - MIN) * LOW_RATE
        + (salary - MAX) * HIGH_RATE;
  }



  message = “Your taxes are ...”;
} else {



  message = “You pay no taxes.”;
}

Slide With Plot

plot of chunk unnamed-chunk-1