3 Replies - 1178 Views - Last Post: 01 August 2016 - 03:52 PM Rate Topic: -----

#1 AmericanPi  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 14
  • Joined: 30-July 16

Newb Question bout if/elseif

Posted 30 July 2016 - 04:59 PM

Hey all! First Post!!

=begin
i can't debug this code.
i think i need to fix the way i'm taking and assigning the input.
or it could be the way im writing my if, elsif statements. 
=end

puts "Enter Your Age : "

age = gets.to_i

if (age < 5)
	puts "You are not old enough to take this survey."
else (age >= 5) || (age <= 6)
	puts "You are in kindergarten."
elseif (age >= 6) && (age <=14)


elseif 
	puts "Gave it a shot"
end


Is This A Good Question/Topic? 0
  • +

Replies To: Newb Question bout if/elseif

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2511
  • View blog
  • Posts: 3,988
  • Joined: 21-June 11

Re: Newb Question bout if/elseif

Posted 30 July 2016 - 05:36 PM

The keyword is spelled elsif. elseif is interpreted as the name of a method, that's why you get an exception about the method not existing when that part of the code is reached.

elsif takes a condition and else doesn't. So else (age >= 5) || (age <= 6) is interpreted as an unconditional else-block whose first statement is (age >= 5) || (age <= 6), which does nothing.

Note that once you fix that and (age >= 5) || (age <= 6) is interpreted as a condition, it will always be true because any age that's not >= 5 will necessarily be <= 6. So at least one of the two conditions will always be true.
Was This Post Helpful? 1
  • +
  • -

#3 AmericanPi  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 14
  • Joined: 30-July 16

Re: Newb Question bout if/elseif

Posted 01 August 2016 - 03:22 PM

GOT IT!!!

elsif (not spelled elseif) requires a condition.
else does not
elsif is the beef in my if burger (in this instance).

Thanks allot. I've been trying to finagle that for a few days.

here's my code.



puts "Enter Your Age : "

age = gets.to_i

if (age < 5)
	puts "You are not old enough to take this survey."
elsif (age == 5) || (age == 6)
	puts "You are in kindergarten."
elsif (age >= 7) && (age <= 13)
        puts "you are in middlechool."
elsif (age >= 14) && (age <= 18)
        puts "You are in High School."
else 
	puts "GO TO COLLEGE!!!!"
#i thought of doing this but it would be the same thing as the else i have at the end. and the else is much shorter so...
#elseif (age > 18)
#       puts "GO TO COLLEGE!!"

end


i get a path error on this pc.
Can you tell me if it's working?
Was This Post Helpful? 0
  • +
  • -

#4 AmericanPi  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 14
  • Joined: 30-July 16

Re: Newb Question bout if/elseif

Posted 01 August 2016 - 03:52 PM

Fixed all that path error stuff.
sorry to post so much...don't see how to edit my posts.
tx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1