Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

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

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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!


  2. #2
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

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

    Perhaps you mean something like:

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

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

  5. The Following User Says Thank You to jashburn For This Useful Post:

    Seananigans (March 5th, 2014)

  6. #5
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Fall-thru Switch

    nvm
    Last edited by KucerakJM; March 6th, 2014 at 11:03 AM.

  7. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

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

Similar Threads

  1. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 03:19 PM
  2. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 3rd, 2013, 11:21 PM
  3. [SOLVED] Nested Switch Statement Fall Through
    By Nate in forum Loops & Control Statements
    Replies: 6
    Last Post: July 19th, 2012, 01:49 PM
  4. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  5. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM

Tags for this Thread