Control Statements

Rascal supports the classics: if, else, while, do, switch...

If/else

 
 if 1 == 1: 
     p "yes!\n" 
 else: 
     p "what?\n" 
 

while

 
 int i = 10 
 while i > 0: 
     p "i is %d\n", i 
 

do-while

 
 int i = 10 
 do: 
     p "i is %d\n", i 
 while i > 0 
 

switch

A switch statement in C looks like:
 
 switch( i ) 
 { 
     case 0: 
         printf( "hello\n" ); 
         break; 
     case 1: 
         printf( "goodbye\n" ); 
         break; 
     default: 
         printf( "ummm...\n" ); 
 } 
 
This syntax is ripe for simplifying. The keyword 'case' isn't really necessary. Falling through one case to another is a special case, but the non-special case ('break') is the case that requires the 'break' keyword. Rascal takes the opposite approach - you do not fall into the next case unless you specifically use 'fall'. Rascal also lets you use a range of values for a case statement.
 
 switch i: 
     0: 
         p "hello\n" 
     1: 
         p "goodbye\n" 
     2..5: 
         p "yay!\n" 
         fall 
     default: 
         p "ummm...\n" 
 


NEXT: When are parenthesis needed for function calls?