Re: Case Statements and INequalities

  • From: Veli-Pekka Tätilä <vtatila@xxxxxxxxxxxxxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Tue, 18 Sep 2007 16:30:09 +0300

Jared,
Which language's case statement do you mean?

I don't know which language but. Assuming for the moment you ment ruby,
since control structures in it are expressions that can serve as
r-values, you can do the case example in many ways. One of them is the
case equality operator === as defined for the lazy Range objets that can
be specified as literals, much like in Perl. That is:

x = gets
case x.to_i
when 1 .. 9: puts "Less than 10"
when 10 .. 99: puts "Less than 100"
else puts "Something else"
end

IF you put line breaks before the condition and the statements after it,
you can drop the colons, too.


-- 
With kind regards Veli-Pekka Tätilä (vtatila@xxxxxxxxxxxxxxxxxxxx)
Accessibility, game music, synthesizers and programming:
http://www.student.oulu.fi/~vtatila

Jared Wright wrote:
> 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.
__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: