1. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

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.

Java 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:

Java 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:

Java 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.

2. Think of switch statements as easier if/else ladders:

Java 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();
}```

3. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
Originally Posted by ra4king
Think of switch statements as easier if/else ladders:

Java 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.

Java 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;}```

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

5. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
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.

6. Whatever works and fits your program best :)

7. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
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.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•