Advertise Here

Author Topic: Switch Statements  (Read 4519 times)

0 Members and 1 Guest are viewing this topic.

Offline Mdog

  • SMF For Free Member
  • *
  • Posts: 48
    • View Profile

  • Total Badges: 11
    Badges: (View All)
    Topic Starter Combination Level 2 Level 1 10 Posts
Switch Statements
« on: June 11, 2009, 10:04:51 pm »
The most common form of the switch statement takes an integer type argument (or an argument that can be automatically promoted to an integer type) and selects among a number of alternative integer constant case branches:

Code: [Select]
public void switchStatement(int expression)
{
switch( expression )
{
case int constantExpression:
statement;
[ case int constantExpression:
statement; ]
...
[ default:
statement; ]
}
}

The case expression for each branch must evaluate to a different constant integer value during compiling.  An optional default case can be specified to catch unmatched conditions.  When executed, the switch simply finds the branch matching its conditional expression (or the default branch) and executes the corresponding statement.  The switch statement can then continue executing branches after the matched branch until it hits the end of the switch or a special statement called break.  Here are a couple of examples:

Code: [Select]
public void test(int value)
{
switch ( value ) {
case 1:
System.out.println( 1 );
case 2:
System.out.println( 2 );
case 3:
System.out.println( 3 );
}
// prints 2, 3
}

Using break to terminate each branch is more common:

Code: [Select]
public void test(int retVal)
{
switch ( retVal )
{
case MyClass.GOOD:
...
break;
case MyClass.BAD:
...
break;
defult:
...
break;
}
}

In this example, only one branch: GOOD, BAD, or the default is executed.  The "fall through" behavior of the switch is justified when you want to cover several possible case values with the same statement, without resorting to a bunch of if-else statements:

Code: [Select]
public void test(int value)
{
switch( value ) {
case 1:
case 2:
case 3:
System.out.println("3" );
break;
case 4:
System.out.println("4" );
break;
case 5:
case 6:
System.out.println("6" );
break;
}
}

This example effectively groups the six possible values into three cases.

I hope this has helped you.  Please post comments.
« Last Edit: June 12, 2009, 06:51:56 pm by Mdog »

Offline Games

  • SMF For Free Member
  • *
  • Posts: 43
    • View Profile
    • Games

  • Total Badges: 11
    Badges: (View All)
    Apple User Topic Starter Combination Level 2 Level 1
Re: Switch Statements
« Reply #1 on: August 26, 2009, 05:32:18 pm »
Nice Mdog. Switches>If all the time if you can use the,  ;D

www.everygame.smfforfree.com
If you even remotely like games, join today!

 

Related Topics

  Subject / Started by Replies Last post
14 Replies
3890 Views
Last post February 25, 2007, 12:08:08 pm
by Laugh-nd-kid
2 Replies
1514 Views
Last post March 31, 2007, 09:59:55 am
by Pietra Atomica
14 Replies
3100 Views
Last post November 08, 2007, 07:55:19 pm
by LightningKnight
0 Replies
5929 Views
Last post February 04, 2008, 07:27:04 pm
by slayer766
0 Replies
3357 Views
Last post February 09, 2009, 07:57:56 pm
by Marsada