# switch statements

• 04-10-2011, 10:00 PM
jim01
switch statements
I am having a problem understanding switch statements. What I am confused about is where the logic goes. For instance, I created the below problem using if statements. The program verifies whether or not it is true that numbers whose sum of digits is divisible by 3 represent numbers divisible by 3.

Code:

``` import java.util.Scanner; public class ProgrammingProblemTwo {     public static void main(String[] args) {             //Create new scanner             Scanner input = new Scanner(System.in);             //Ask user to input a five digit number             System.out.println("Please input a five digit number");             //create variable number             int number = input.nextInt();             //create variable first digit in order to isolate the first digit             int firstDigit = number / 10000;             //Create variables a and secondDigit in order to isolate the second digit             int a = number / 1000;             int secondDigit = a % 10;             //Create variables b and thirdDigit in order to isolate the third digit             int b = number / 100;             int thirdDigit = b % 10;             //Create variables c and fourthDigit in order to isolate the fourth digit             int c = number / 10;             int fourthDigit = c % 10;             ////Create variable fifthDigit in order to isolate the fifth digit             int fifthDigit = number % 10;             int sum = firstDigit + secondDigit + thirdDigit + fourthDigit +                     fifthDigit;             if(number % 3 == 0 && sum % 3 == 0){                     System.out.println("Both " + number + " and sum " + sum +                             " are divisible by 3");             }                     if(number % 3 != 0 && sum % 3 != 0){                             System.out.println("Both " + number + " and sum " + sum +                             " are indivisible by 3");                     }                             if(number % 3 == 0 && sum % 3 != 0 || number % 3 != 0 && sum % 3 == 0){                                     System.out.println("The famous statement is wrong");                             }   } }```
Where I get confused about is where the logic is at in switch statements. For example, here is the code I have so far using the switch statement example in the book:

Code:

``` import java.util.Scanner; public class ProgrammingProblemTwo {     public static void main(String[] args) {             //Create new scanner             Scanner input = new Scanner(System.in);             //Ask user to input a five digit number             System.out.println("Please input a five digit number");             //create variable number             int number = input.nextInt();             //create variable first digit in order to isolate the first digit             int firstDigit = number / 10000;             //Create variables a and secondDigit in order to isolate the second digit             int a = number / 1000;             int secondDigit = a % 10;             //Create variables b and thirdDigit in order to isolate the third digit             int b = number / 100;             int thirdDigit = b % 10;             //Create variables c and fourthDigit in order to isolate the fourth digit             int c = number / 10;             int fourthDigit = c % 10;             ////Create variable fifthDigit in order to isolate the fifth digit             int fifthDigit = number % 10;             int sum = firstDigit + secondDigit + thirdDigit + fourthDigit +                     fifthDigit;         switch (number){                     case 1: {System.out.println("Both " + number + " and sum " + sum +                                             " are divisible by 3"); break;}                     case 2: {System.out.println("Both " + number + " and sum " + sum +                                             " are indivisible by 3"); break;}                     case 3: {System.out.println("The famous statement is wrong");break;}                 }     } }```
the syntax information from the book makes no sense to me because all it shows is:

Code:

``` switch (expression) {     case label1: {statements;break;}     case label2: {statements;break;}     ...     ...     case labelk: {statements;break;}     default: {statements;break;} }```
The book example shows that the statements section is where you add the System.out.println information. It doesn't explain where the logic is at.
• 04-10-2011, 10:03 PM
ra4king
Think of switch statements as easier if/else ladders:

Code:

```swith(value) {     case ifValueEqualsThis:         runThisCode();         break; //if you don't put break here, all code below this will be run too.     case ifValueEqualsThat:         runThatCode();         break;     ....     default:         nothingElseFits(); }```
• 04-10-2011, 10:21 PM
jim01
Quote:

Originally Posted by ra4king
Think of switch statements as easier if/else ladders:

Code:

```swith(value) {     case ifValueEqualsThis:         runThisCode();         break; //if you don't put break here, all code below this will be run too.     case ifValueEqualsThat:         runThatCode();         break;     ....     default:         nothingElseFits(); }```

So the logic is part of the case. OK. What if you have two values, such as in this case I have number and sum. I attempted to cut and paste my logic into the case but I get "incompatible types" when I compile it.

Code:

``` switch (sum){                     case number % 3 == 0 && sum % 3 == 0: {System.out.println(                             "Both " + number + " and sum " + sum + " are divisible by 3"); break;}                     case number % 3 != 0 && sum % 3 != 0: {System.out.println(                             "Both " + number + " and sum " + sum + " are indivisible by 3"); break;}                     case number % 3 == 0 && sum % 3 != 0 || number % 3 != 0 && sum % 3 == 0:                             {System.out.println("The famous statement is wrong");break;}```
• 04-10-2011, 10:23 PM
ra4king
Now you're treating it as a boolean acceptor.
Whenever it runs over each "case" it does if(sum == caseValue).
• 04-10-2011, 10:41 PM
jim01
Quote:

Originally Posted by ra4king
Now you're treating it as a boolean acceptor.
Whenever it runs over each "case" it does if(sum == caseValue).

OK, I get it now. It would appear that I can't use a switch statement for this particular problem because the program has to print either "Both number and sum are divisible by 3," "Both n and sum are indivisible by 3," or "The famous statement is wrong." Therefore I have to be able to compare the sum and number variables.
• 04-10-2011, 10:44 PM
ra4king
Whatever works and fits your program best :)
• 04-10-2011, 10:51 PM
jim01
Quote:

Originally Posted by ra4king
Whatever works and fits your program best :)

Thank you very much for your help. Even though I cannot use a switch statement in this particular program, you helped me understand how to use it.
• 04-10-2011, 10:52 PM
ra4king
You're welcome :)