Re: Case Statements and INequalities

  • From: "Marlon Brandão de Sousa" <splyt.lists@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Tue, 18 Sep 2007 11:51:10 -0300

But, guy, do it in the present! what language are you using in this
thread subject?
Marlon

2007/9/18, Jared Wright <wright.jaredm@xxxxxxxxx>:
> Marlon, Thanks for the illustrations, and your point is taken. I'll be
> more conscious of these things in future.
>
> JW
> Marlon Brandão de Sousa wrote:
> > A simple suggestion:
> > When you have a question DO NOT ASSUME THAT everybody know what you
> > are doing and what you need. I have read this thread and, if I didn't
> > miss anything in your post, you didn't make references about what
> > language you're using. This both frustrates folks who are whilling to
> > help you and also can lead to incorrect responses, as people can guess
> > wrong what you need and tell things that will not work.
> > For example, in C / c++ I agree with Chris. You specify the variable
> > you're tring to use in a switch statement and have to test it against
> > constants to equality only. Notice that I said constants only, you
> > can't even test it against variables.
> > This said, if, as suggested, you try this code:
> >
> > #include <iostream>
> > using namespace std;
> > int main()
> > {
> > int a = 5;
> > switch(a)
> > {
> > case a < 5:
> > cout << "done";
> > default:
> > cout << "none";
> > }
> > return 0;
> > }
> >
> > The GNU compiler returns this:
> >
> > test.cpp: In function `int main()':
> > test.cpp:8: error: `a' cannot appear in a constant-expression
> >
> > Because a is a variable and not a constant, C and c++ dont allow you
> > to use it in a case expression.
> > This other code will create the same error:
> >
> > #include <iostream>
> > using namespace std;
> > int main()
> > {
> > int a = 5;
> > int b = 3;
> > switch(a)
> > {
> > case b:
> > cout << "two equals";
> > default:
> > cout << "none";
> > }
> > return 0;
> > }
> >
> > tst.cpp: In function `int main()':
> > tst.cpp:9: error: `b' cannot appear in a constant-expression
> >
> > A simple read in the C or c++ specifications should let it clear.
> > but oh! May be I used time to show you compiler errors and explaining
> > concepts and you are even not using C or c++ ...
> > Well, I hope this helped anyway.
> > Marlon
> >
> > 2007/9/18, Jared Wright <wright.jaredm@xxxxxxxxx>:
> >
> >> Hey all, Two in one night; I'm on a role. Anyway... I am familiar with
> >> the properties of a switch statement and how case statements work with
> >> it. My question is whether or not I can specify anything other than
> >> direct values for case statements? For an example... Let's  assume I
> >> have a variable of X, and I indicate this variable in a switch
> >> statement. I want the flow of control to go one place if X is less than
> >> 10, another if X is between 10 and 100, and another if X is greater than
> >> 100. I see how to specify these inequalities using if statements, but it
> >> seems that using a switch statement with three separate cases would be a
> >> more efficient way of handling this. But all I see in the online
> >> documentation I've dug up is case statements directly defined
> >> and nothing about case statements that cover a range of potential values
> >> for the variable indicated in the switch statement. I know that I can
> >> assign multiple case statements to one block of code, but I can't
> >> imagine having to specify each potential value in the range this way.
> >> Thoughts on how I can clean this up are welcome, and as always, are
> >> appreciated.
> >>
> >> JW
> >> __________
> >> View the list's information and change your settings at
> >> //www.freelists.org/list/programmingblind
> >>
> >>
> >>
> >
> >
> >
>
> __________
> View the list's information and change your settings at
> //www.freelists.org/list/programmingblind
>
>


-- 
When you say "I wrote a program that crashed Windows," people just
stare at you blankly and say "Hey, I got those with the system, for
free."
Linus Torvalds
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: