Switch Statement


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Switch Statement

  1. #1
    Join Date
    Dec 2005
    Posts
    27

    Switch Statement

    Can someone please explain to me how to do a proper Switch Statement
    Thanx
    --Stormswimmer

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Code:
    /** generate a number from 1 to 12 */
    int rnd = ( int ) ( Math.random * 12 ) + 1;
    
    int num = 0;
    
    /** start switch statement */
    switch( rnd ) { // if (rnd == ...
    case 0: // 0)
        num = 5;
        /** fall though (no break statement) */
    case 1: // 1)
        num += 3;
        break;
    case 2: // 2 ||
    case 3: // 3 ||
    case 4: // 4)
        num = 10;
        break;
    default: // else
        num = -5;
        break;
    }
    they only accept an int or a char. they are very similar to if statements.. the code above is the same as:
    Code:
    /** generate a number from 1 to 12 */
    int rnd = ( int ) ( Math.random * 12 ) + 1;
    
    int num = 0;
    
    if ( rnd == 0 ) {
        num = 5;
        num += 3;
    } else if ( rnd == 1 ) {
        num += 3;
    } else if (( rnd == 2 ) || ( rnd == 3 ) || ( rnd == 4 )) {
        num = 10;
    } else {
        num = -5;
    }
    i feel more organized using a switch statement, but you can only use switch statements with ints or chars so if your dealing with Objects, you have to use if statements.
    Last edited by destin; 12-26-2005 at 11:54 PM.

  3. #3
    Join Date
    Dec 2005
    Posts
    27
    How do you write it? What are the cases and when do you use them?

  4. #4
    Join Date
    Dec 2005
    Posts
    27
    What is going on here
    Code:
    public class SwitchDemo {
        public static void main(String[] args) {
    
            int month = 8;
            switch (month) {
                case 1:  System.out.println("January"); break;
                case 2:  System.out.println("February"); break;
                case 3:  System.out.println("March"); break;
                case 4:  System.out.println("April"); break;
                case 5:  System.out.println("May"); break;
                case 6:  System.out.println("June"); break;
                case 7:  System.out.println("July"); break;
                case 8:  System.out.println("August"); break;
                case 9:  System.out.println("September"); break;
                case 10: System.out.println("October"); break;
                case 11: System.out.println("November"); break;
                case 12: System.out.println("December"); break;
                default: System.out.println("Not a month!");break;
            }
        }
    }

  5. #5
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    checks to see what month is equal:
    Code:
    public class SwitchDemo {
        public static void main(String[] args) {
    
            int month = 8;
            switch (month) {
                // if (month == 1) System.out.println("January");
                case 1:  System.out.println("January"); break;
                // else if (month == 1) System.out.println("Febuary");
                case 2:  System.out.println("February"); break;
                // ...etc.
                case 3:  System.out.println("March"); break;
                case 4:  System.out.println("April"); break;
                case 5:  System.out.println("May"); break;
                case 6:  System.out.println("June"); break;
                case 7:  System.out.println("July"); break;
                case 8:  System.out.println("August"); break;
                case 9:  System.out.println("September"); break;
                case 10: System.out.println("October"); break;
                case 11: System.out.println("November"); break;
                case 12: System.out.println("December"); break;
                // else System.out.println("Not a month!");
                default: System.out.println("Not a month!");break;
            }
        }
    }
    So since month is equal to 8, "August" will be printed.
    If month was 12, "December" would be printed.
    if month was 15, "Not a month!" would be printed.
    Last edited by destin; 12-27-2005 at 12:02 AM.

  6. #6
    Join Date
    May 2005
    Posts
    75
    You don't need to do anything special to execute a switch, it's an alternative to using an if/else statement.

    Here's a simple example ...
    Code:
    int num = 3;
    switch(num){
         case 1: System.out.println("num = 1!"); break;
         case 2: System.out.println("num = 2!"); break;
         case 3: System.out.println("num = 3!"); break;
         default: System.out.println("num is unknown");
    }
    The switch function determines what to do depending on the value of a variable.

    The case value is saying if the input variable is equal to this, then execute the following code. Break tells the program to exit the switch statement. Default is executed if no case was found to be true.

    Switches are not limited to integers, this is valid too:
    Code:
    char letter = 'A';
    switch(letter){
         case 'A': System.out.println("You typed A"); break;
         case 'B': System.out.println("You typed B"); break;
         case 'C': System.out.println("You typed C"); break;
         default: System.out.println("I don't know my ABC's");
    }
    The above code is exactly the same as if you entered the following if/else statement, it's just a matter of preference.
    Code:
    char letter = 'A';
    if(letter == 'A'){
         System.out.println("You typed A");
    } else if (letter == 'B'){
         System.out.println("You typed B");
    } else if (letter == 'C'){
         System.out.println("You typed C");
    } else {
         System.out.println("I don't know my ABC's");
    }
    Last edited by srekcus; 12-27-2005 at 12:15 AM.

  7. #7
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by srekcus
    You don't need to do anything special to execute a switch, it's an alternative to using an if/else statement.
    if-else statements resolve to booleans, therefore you can put anything in there that will resolve to a boolean (ie. a method that returns a boolean). You can use it alternatively, but only if you are dealing with ints (/chars).

    Quote Originally Posted by srekcus
    Switches are not limited to integers, this is valid too:
    well.. characters and integers are really the samething..
    Code:
    System.out.println((int) 'h' );
    will print:
    104

    just as
    Code:
    System.out.println((char) 104 );
    will print:
    h

    i don't mean to be annoying srekcus, i just want to make sure stormswimmer thoroughly undestands the concept

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You can also use Enums to be the value which is compared in a switch structure.

Similar Threads

  1. How do you loop a switch statement?
    By 72chevy in forum Java
    Replies: 3
    Last Post: 11-01-2005, 10:48 AM
  2. Replies: 2
    Last Post: 05-18-2003, 11:16 PM
  3. Use of Select Case or Switch Statement
    By Daryl Shockey in forum Database
    Replies: 2
    Last Post: 05-28-2002, 04:50 PM
  4. Access And SQL date statement
    By Saiful in forum VB Classic
    Replies: 7
    Last Post: 11-24-2000, 06:21 AM
  5. switch statement
    By James Lin in forum .NET
    Replies: 9
    Last Post: 08-15-2000, 11:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center