how to do u use greater than or less than signs with switch and case statements?
case > 5
case < 5
i'm using that but that obviously doesn't work
Printable View
how to do u use greater than or less than signs with switch and case statements?
case > 5
case < 5
i'm using that but that obviously doesn't work
I think it's easier with if statements, but try this:
Code:switch(a)
case a > 5:
//do something
break;
Use this instead:
Code:#include <iostream.h>
void main()
{
int a;
int b;
cout <<"Value for a: ";
cin>>a;
cout<<"Value for b: ";
cin>>b;
if(a > b)
cout<<"a is greater than b"<<endl;;
if (a < b)
cout<<"a is lesser than b"<<endl;
if (a == b)
cout<<"a and b are equal"<<endl;
}
the if is definitely easier but i have to use a switch
statement as part of an assignment.
thx harry but i'm very familiar with case statements, what i wanted to accomplish, however, was to compare the switch against another number
similar to this in VB:
[CODE
case is < 5:
case is > 5:
[/CODE]
I know what you want, but what I'm saying is that you can't use expressions with variable results in a switch statement. No two cases can evaluate to the same result. There is no 'case Is < 5' style solution.
I know you said that you have to use switch statements in the assignment, but isn't there anywhere else you could us a switch statement? This seems like an inappropriate situation for one.
case 1:
case 2:
case 3:
case 4: code here
break
I think of this..
Code:int y=45;
switch(y>5)
{
case 0:
printf("Y is less than or equal to 5");
break;
case 1:
printf("Y is Greater than 5");
break;
default:
}