How to compare in switch case with two values/variables?

Raj
Raj
648 Points
26 Posts

I'm trying switch case as follows:

switch (strVal1)
{
   case "hello":
            if(strVal2=="true"){
                  // do something
             }
      break;
      case "hello":
             if(strVal2=="false"){
                // do something
             }
   etc ....
}

Do we have any other way to compare two variable's values in switch case?

Views: 3371
Total Answered: 1
Total Marked As Answer: 1
Posted On: 14-Sep-2022 02:25

Share:   fb twitter linkedin
Answers
Rashmi
Rashmi
1068 Points
19 Posts
         

Yes, switch case allows to pass multiple variable and we can compare inside the case as:

switch (strVal1, strVal2)
{
   case ("hello", "true"):
                  // do something
      break;
      case ("hello", "false"):
                // do something
   etc ....
}
Posted On: 14-Sep-2022 05:14
thanks. works for me.
 - Raj  19-Sep-2022 00:03
 Log In to Chat