### Type INT

is there a way to compare a variable to an int without having to type if(i = 1 || i == 2 ... etc?
A switch case works:
 1234567 switch(Variable) { case 1: //Code case 2: //Same Code case 3: //You Get The Idea //... }

Last edited on
True but the problem with the switch statement, which i tried earlier, is that the algorithm i attempting to use does not function properly with it.
Instead of writing if(i == 1 || i == 2 || i == 3) you can write if (1 <= i && i <= 3).
Last edited on
I hope this isn't homework :)

 123456789101112131415161718192021222324252627 #include #include #include using namespace std; int main() { int i = 100; int x=0; while (x<=999) { if (x==i) { cout << x << " is equal to " << i << endl; break; } else { cout << x << " is not equal to " << i << endl; x++; } } return(0); }

To make it run faster, comment out the "is not equal to" line.

Or maybe this is what you want.

 1234567891011121314151617181920 #include #include #include using namespace std; int main() { int i = 100; int x = 99; if (x!=i) { cout << x << " is not equal to " << i << endl; } else { cout << x << " is equal to " << i << endl; } return(0); }
Last edited on
Topic archived. No new replies allowed.