# Coding a statement that increments from 20-40 (only in even numbers) - fall-thru switch

• March 5th, 2014, 05:44 PM
Seananigans
Coding a statement that increments from 20-40 (only in even numbers) - fall-thru switch
Hello all,

I need help comprehending a fall-thru switch and how to catch the odd numbers using the option in the switch.

Here is my code:

public class HolstienSFallThruSwitch

{
public static void main(String[] args)
{

for(int i=20; i<=40; i+=2)
{

switch (i)
{
}
System.out.println(i);
}
}
}

It prints only the even numbers incrementing by 2, starting with the number 20. How would one "catch" the odd numbers in the switch? Thank you!
• March 5th, 2014, 05:56 PM
Seananigans
Fall-thru Switch
Hello all,

I need help comprehending a fall-thru switch that would increment 20-40 using even numbers, and how to catch the odd numbers using the option in the switch.

Here is my code:

public class HolstienSFallThruSwitch

{
public static void main(String[] args)
{

for(int i=20; i<=40; i+=2)
{

switch (i)
{
}
System.out.println(i);
}
}
}

It prints only the even numbers incrementing by 2, starting with the number 20. How would one "catch" the odd numbers in the switch? Thank you!
• March 5th, 2014, 07:31 PM
Norm
Re: Coding a statement that increments from 20-40 (only in even numbers) - fall-thru switch
The posted code shouldn't pass any odd numbers to the switch.
• March 5th, 2014, 08:05 PM
jashburn
Re: Coding a statement that increments from 20-40 (only in even numbers) - fall-thru switch
Perhaps you mean something like:

Code :

```public class FallThroughSwitch {   public static void main(String[] args) {   for (int i = 20; i <= 40; i++) { switch (i) { case 21: case 23: case 25: case 27: case 29: case 31: case 33: case 35: case 37: case 39: break; default: System.out.println(i); break; } } } }```

With the +1 increments, the odd-numbered cases will fall through until the last case (39), at which it breaks out of the switch-case statement. For all the rest, and therefore the odd-numbered cases, the "default" case catches them.

If that is what's being asked, I'm not sure why would anyone want to do this other than to illustrate switch-case fall-through. As with your first attempt, you're better off incrementing by 2.

If for some unforeseeable reason +2 increments are not possible, then it'll be better to use the modulus operator to massively simplify the switch-case statements, e.g.,

Code :

``` for (int i = 20; i <= 40; i++) { switch (i % 2) { case 1: break; default: System.out.println(i); break; } }```

Btw, use switch-case fall-through sparingly as it can lead to bugs. It's very easy to overlook the "break" (or absence of) statement. I don't use it in my work...
• March 5th, 2014, 08:45 PM
KucerakJM
Re: Fall-thru Switch
nvm
• March 5th, 2014, 10:58 PM
Seananigans
Re: Coding a statement that increments from 20-40 (only in even numbers) - fall-thru switch
Thank you for the explanation. Our instructor was using it to illustrate the switch-case fall-through, and I wanted a clearer understanding.