-1

I am new to java, so want to understand which statement to be used and when ?

Program has a condition where there are three checks needs to be done. I am not clear which statement to go for.

Please help me in understanding the statement usage

  • 1
    Possible duplicate of [Why the switch statement and not if-else?](http://stackoverflow.com/questions/449273/why-the-switch-statement-and-not-if-else) – Dan May 12 '17 at 09:30

1 Answers1

0

The if statement is normally used if you just check one or more different conditions that can be connected with logical operators like and and or.

//if the color is red or blue
if (color == 'red' | color == 'blue' {
    //do something.
}

The switch statement can be used if you have one condition that has to be handled different, depending on its value.

//do different things depending on the color
switch (color) {
    case 'red':
        //do something
        break;
    case 'blue':
        //do something else
        break;
}
dukemadcat
  • 23
  • 1
  • 5