1

The following code should print whether intenger value is odd or even with fall through switch statement and for statements

for(int i=2; i<=10; i+=2)
{

  switch(i)
  {
    case 1: 
    {System.out.printf("\nNot printing odd numbers");}
    case 2: 
      System.out.printf("\n %d is an even number.", i);
    //case 3:
    //case 4: 

  }//end switch

}//end for
linda chang
  • 21
  • 1
  • 4
  • Why do you want to use switch? – Aniket Kulkarni Oct 10 '14 at 05:04
  • @AniketKulkarni my prof ask me to, but i dont know how to deal with it. – linda chang Oct 10 '14 at 05:04
  • this should be the output. it just dont get to me. Not printing odd numbers! \n 2 is an even number. Not printing odd numbers! \n 4 is an even number. Not printing odd numbers! \n6 is an even number. Not printing odd numbers! \n8 is an even number. Not printing odd numbers! \n10 is an even number. – linda chang Oct 10 '14 at 05:05
  • 1
    None of the responses uses fall-through technique though. – Voicu Oct 10 '14 at 05:09
  • @Voicu there's no need to use fall-through for this case. What will happen if somebody changes 10 to 100 later and the code should still verify if `i` is odd or even? You won't write fall-through until 99... – Luiggi Mendoza Oct 10 '14 at 05:12
  • @LuiggiMendoza: Bryan is asked (probably homework) to use fall-through though. – Voicu Oct 10 '14 at 05:13
  • @Voicu You might've mixed up people here; I don't see a Bryan anywhere. – Dennis Meng Oct 10 '14 at 06:33
  • @DennisMeng: I was referring to the OP, who changed names meanwhile. – Voicu Oct 10 '14 at 15:51
  • @Voicu Ordinarily I'd assume something like that happened; it's just that I don't really expect a name change from "Bryan" to "Linda" – Dennis Meng Oct 10 '14 at 22:02

6 Answers6

8

Change i+=2 to i++ and i+=2 will give you value of i as 2,4,6,8,10 which means only even numbers.

switch(i%2)
  {
  case 0:
     //even number
     break;
  case 1:
     //Odd Number
     break;
  }
akash
  • 22,664
  • 11
  • 59
  • 87
2

There is no need given your for loop,

for(int i=2; i<=10; i+=2)

i will not be odd. Based on your switch and problem statement I think you wanted,

for(int i=1; i<=10; i++) {
  switch(i) {
  case 2: case 4: case 6: case 8: case 10: 
    System.out.printf("\n %d is an even number.", i);
    break;
  default:
    System.out.printf("\nNot printing odd numbers");
  }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

I believe a fall-through switch should look like this. I have ommitted your outer for loop for simplicity.

  switch (i)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 9:
        System.out.printf("\nNot printing odd numbers");
     break;

        case 2:
        case 4:
        case 6:
        case 8:
        System.out.printf("\n %d is an even number.", i);
     break;
}

You essentially Fallthrough some cases (all odd numbers and all even numbers). Hence the term. You can read more about fallthrough here.

John Joe
  • 12,412
  • 16
  • 70
  • 135
midhunhk
  • 5,560
  • 7
  • 52
  • 83
1
     for (int i = 2; i <= 10; i++) {

            switch (i % 2) {
            case 0: // even number
                System.out.printf("\n %d is an even number.", i);
                break;
            case 1: // odd number
                System.out.printf("\nNot printing odd numbers");
                break;          

            }// end switch

        }// end for
Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31
1

Try this

for (int i = 2; i <= 10; i++) {

            switch (i % 2) {

            case 0:
                System.out.printf("\n%d is an even number.", i);
                break;
            case 1:
                System.out.printf("\nNot printing odd numbers");
                break;

          }// end switch

        }// end for
    }
dasrohith
  • 533
  • 2
  • 8
  • 21
0
nt num=10;//any number you want
nt last=num%10;
        switch(last)
         {
                 case 0:
                 case 2:
                 case 4:
                 case 6:
                 case 8:
                System.out.println("numbet is even" +num) ;
                   break ;
                   default :
                System.out.println("number is odd" +num)
           } 
             //end of switch block
            //odd or even using fall through language java
            //GAGAN GANJWAR