Saturday, March 31, 2012

When Statements

Switch statements and if-else statements really accomplish the same thing in a programming language; and to be honest the trouble lies on the if-else statements (dangling modifier problem). And, while the Switch statement can handle multiple conditions, it effectively lacks the ability to evaluate them. My solution to both these problems is a new keyword called the When statement**.
if ( $time == 3 ) { print("3pm"); }
else if ( $time > 3 ) { print("After 3pm"); }
else { print("Anytime but 3pm"); }

switch ($time) {
  case 3: print("3pm"); break;
  default: print("Anytime but 3pm");
}
when($time) {
  3: print("3pm");
  >3: print("After 3pm");
  else: print("Anytime but 3pm");
}
The Switch statement can't evaluate conditions; so, the expression >3 (greater than 3) is not possible. The When statement can handle multiple conditions like the Switch statement, and it can evaluate conditions like the If-else statement. This effectively allows the When statement to replace both the If-else statement and the Switch statement in traditional programming languages. Furthermore, this produces less overhead and better organization of logic blocks.

Additionally, the When statement can evaluate multiple parameters.
when ($time, $space){
  $time > 3: print("It's afternoon");
  $space != $time: print("space and time are not equivalent");
  else: print("This is the default option");
}

** Could also be called the monty_switch in reference to my last name.