if there exist multiple conditions the switch


If there exist multiple conditions, the switch statement is suggested. It is because only one expression gets evaluated depending on which control jumps directly to the respective case.

switch(myVar) {

case 1:

//if myVar is 1 it is executed case 'sample':

//if myVar is 'sample' (or 1, see the next paragraph)

//this is executed case false:

//if myVar is false (or 1 or 'sample', see the next paragraph)

//this is executed default:

//if myVar does not satisfy any case, (or if this is

//1 or 'sample' or false, see the next paragraph)

//this is executed

}

As illustrated in this code, depending on the value of "myvar", the statement of the respective case gets executed. If case is satisfied, the code ahead of that case will also be executed unless the break statement is utilized. In the above instance, if myVar is 1, the code for case 'false',  case 'sample' & 'default' will all be executed as well.

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: if there exist multiple conditions the switch
Reference No:- TGS0266441

Expected delivery within 24 Hours