2

I am writing an if-else statement, and I want the program to do nothing if a certain condition is met. The following code shows what I mean.

if(number == 0) {
//Do nothing
} else if(number % 2 == 0) {
//Do something
} else if(number % 2 == 1) {
//Do something else
}

If number is equal to zero, what I want is for the program to leave this if-else statement and move to the next portion of code, which is within a while loop

  • You answered your own question. By putting no code in your `if` statement, nothing gets done if number equals 0. – Benjamin Urquhart May 01 '19 at 22:15
  • If don't want to do anything then don't write if condition – Ryuzaki L May 01 '19 at 22:17
  • At least one of those comparisons is redundant. There's no need, for example, to test whether the remainder is 1 after you already know the remainder is not 0. –  May 02 '19 at 00:39

2 Answers2

4
if (number != 0)
{
   if (number % 2 == 0)
   {
      //Do Something
   }
   else if (number % 2 == 1)
   {
       // Do something else
   }
}
3

Your own code is the answer:

if(number == 0) {
    //Do nothing

    // If number is zero, nothing will be executed
    // (because there's nothing to execute) and the
    // code will move to the next statement not in this
    // if chain

} else if(number % 2 == 0) {
    //Do something
} else if(number % 2 == 1) {
    //Do something else
}
Benjamin Urquhart
  • 1,626
  • 12
  • 18