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

Raj
Raj
Member
54 Points
17 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: 59
Total Answered: 1
Total Marked As Answer: 1
Posted On: 14-Sep-2022 02:25

Share:   fb twitter linkedin
Answers
Rashmi
Rashmi
Member
206 Points
38 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