If there are a number of possible choices all conditional on the same integer variable, you can use a switch.
int a = 2; switch ( a ) { case 1 : { print( "One" ) ; break; } case 2 : { print( "Two" ) ; break; } default : { print( "Many" ); } }
If a is equal to 1, this program will print "One" and then break out of the switch block.
Leaving out the break
would cause all of the rest of the cases to be executed
until a break was reached.
default
is a catch all which will match any case.
You can also switch on a character variable:
char c = readChar(); switch ( a ) { case ' ' : { println( "Space" ) ; break; } case '\n' : { println( "New Line" ) ; break; } default : { println( c ); } }