4 Replies - 314 Views - Last Post: 10 December 2013 - 06:44 PM Rate Topic: -----

#1 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

case statement question

Posted 09 December 2013 - 11:08 PM

Why doesn't this work??

avr = gets.chomp

def get_grade(avr)
  case avr
  when avr < 60
    return("F") 
  when avr >= 60 && < 70
    return("D")
  when avr >= 70 && < 80
    return("C")
  when avr >= 80 && < 90
    return("B")
  else
    return("A)
  end
end



my errors are as follows:

grades.rb:7: syntax error, unexpected '<'
  when (avr >= 60 && < 70)
                      ^
grades.rb:9: syntax error, unexpected '<'
  when (avr >= 70 && < 80)
                      ^
grades.rb:11: syntax error, unexpected '<'
  when (avr >= 80 && < 90)
                      ^
Gregorys-MacBook-Pro:Programs gregoryheaphyheaphy$ ruby grades.rb
grades.rb:7: syntax error, unexpected '<'
  when (avr >= 60 && < 70)
                      ^
grades.rb:9: syntax error, unexpected '<'
  when (avr >= 70 && < 80)
                      ^
grades.rb:11: syntax error, unexpected '<'
  when (avr >= 80 && < 90)


This post has been edited by macosxnerd101: 10 December 2013 - 06:44 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 1
  • +

Replies To: case statement question

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • Joined: 21-June 11

Re: case statement question

Posted 10 December 2013 - 02:59 AM

A case statement compares one value (the one that the case expression evaluates to) against a set of other values (the ones that the when expressions evaluate to) and then executes the branch where the values matched (i.e. were equal when compared with ===) if one exists.

In your case all the when expressions evaluate to booleans. So you basically have something like this:

case avr
when true
...
when false
...
when false
...
end



Since avr is most likely neither equal to true nor false, none of the when clauses match, so nothing gets executed.

To achieve what you want, you could use ranges (which compare === to any value they contain) in your when clauses or you could replace case avr with just case. case without an argument simply evaluates the first clause where the argument to when is truthy, so it basically acts like a series of if-elsifs.
Was This Post Helpful? 1
  • +
  • -

#3 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: case statement question

Posted 10 December 2013 - 09:38 AM

thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Re: case statement question

Posted 10 December 2013 - 06:39 PM

You might also want to note that in the case of case statements, things are evaluated with a ===, meaning within range in some cases. This is especially useful in cases like yours. The second thing to note is that Ruby implies returns.

score = gets.chomp.to_f

case score
when 90..100 then 'A'
when 80..90 then 'B'
when 70..80 then 'C'
when 60..70 then 'D'
else 'F'
end



Your issue seems to primarily be that you keep inferring a variable where Ruby will not work like that. Such as: x < 10 && > 5. That's invalid, you would need x < 10 && x > 5 to make that work, or more correctly use a range such as (5..10)
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,451
  • Joined: 27-December 08

Re: case statement question

Posted 10 December 2013 - 06:44 PM

Please remember to use code tags when posting: :code:.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1