RE: Case Statements and INequalities

  • From: "Delaunay Christophe" <christophe.delaunay@xxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Tue, 18 Sep 2007 13:41:58 +0200

Hi Jared,

You wrote: 

[JW]
>My question is whether or not I can specify anything other than direct
>values for case statements?

[ChD]
If you're working in C/C++, you won't. In other languages, maybe?

[JW]
>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.

[ChD]
In terms of efficiency, this is the same as writing:

if ( x < 10 ) {
        ... Do what's appropriate in this first case;
} else if ( x > 100 ) {
        ... Do what's appropriate in this second case;
} else {
        ... Do what's appropriate in this default case;
}

Of course, inequality testing is just a little bit less efficient than
testing pure equalityies but in this case, sinc you would have many
equalities to test, it's obvuiously better to test two inequality than a
lot of equalities.

Have a nice day. ChD
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: