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 2 of 2

Thread: Switch with modulus value

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch with modulus value

    Hi.

    Kindly help me understand how the output of the switch below became 0 4 9

    for(var i=0;i<10;i++)
    {
    switch(i%5)
    {

    case 2: i*=i;break;
    case 3: i++;break;
    case 1:i+=3;
    case 4:
    i++;
    continue;
    default : break;
    }
    System.out.print(i+" ");
    i++;
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch with modulus value

    Print the value of i%5 so you can see the value that the switch statement sees.
    Notice the missing break statement for case 1.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. My modulus result is wrong.
    By Loh Jane in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2014, 02:55 PM
  2. Using Modulus to alternate between 2 functions?
    By Rexoa in forum Java Theory & Questions
    Replies: 5
    Last Post: February 25th, 2014, 11:59 PM
  3. modulus question
    By meangreen2003 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 18th, 2014, 05:19 PM
  4. BruceForce Method and Modulus HELP!
    By JAVAHELPP in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 13th, 2013, 03:38 AM
  5. Modulus with BigIntegers
    By JDreben in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2012, 10:00 PM

Tags for this Thread