can the case in switch condition be negative??eg case: -1.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

can the case in switch condition be negative??eg case: -1..

10th Sep 2016, 1:42 PM
RATNABH KUMAR RAI
2 Answers
+ 1
the type of the expression inside case should be implicitly convertible to the same type as the result of the expression of the switch block. The code ------- extern class Person; // Externally defined class. ... Person *person1, *person2, person3; ... switch(person3) { case *person1: // person3 is equivalent to person1 case *person2: // person3 is equivalent to person2 } ------- is perfectly valid. But you should define the equality operator and copy/move contructors to make what you want when comparing two Person instances.
10th Sep 2016, 1:56 PM
trickybear
+ 1
yes code is like- #include <iostream> using namespace std; int main() { int i=(-20); switch (i) { case 2: cout<<"not equal"; break; case (-20): cout<<"equal"; break; default: cout<<"useless"; } return 0; } //output- equal
10th Sep 2016, 3:58 PM
Shrinivas Deshpande
Shrinivas Deshpande - avatar