Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Learning Code Examples

Switch Statements

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
Switch Statements package com.ack.learning; public class SwitchStatements { public static void main( String[] args ) { int amount = 10; // simple switch statement switch( amount ) { case 1: System.out.println( "amount is 1" ); break; case 2: System.out.println( "amount is 2" ); break; default: System.out.println( "don't care about amount" ); } // more complicated switch statement switch( amount ) { case 1: System.out.println( "amount was 1" ); break; case 10: case 4: case 3: if( amount == 10 ) { System.out.println( "amount was ten" ); } else { System.out.println( "amount was 4 or 3, actually -> " + amount ); } break; default: System.out.println( "default action" ); } // switch on a char char c = 't'; switch( c ) { case 'b': System.out.println( "hit the b character" ); break; case 't': System.out.println( "hit the t character" ); break; default: System.out.println( "unsupported character" ); } } }