Is an elsif loop possible?

Discussion in 'Ruby' started by Devin Rawlek, Mar 11, 2011.

  1. Devin Rawlek

    Devin Rawlek Guest

    Hi Everyone,

    I don't know if this is possible as I haven't found anything on google
    or in here. Is it possible to have an elsif to be looped?

    $Str = gets.chomp
    if $Str.to_i < 0
    puts 'Sorry you cannot be that weak. Please enter a valid stat.'
    elsif $Str.to_i > StartStats.to_i
    puts 'You cannot give more stats than what you have available.
    Please enter a valid stat.'
    else puts 'Your Strength is '+($Str)
    end

    Is there a way to make this a loop so that if $Str.to_i < 0 it would
    restart and ask for an input again? Even if the answer is the same it
    would still restart?

    I tried setting up a retry and redo but I couldn't get it to work.

    Thank you for your time.

    --
    Posted via http://www.ruby-forum.com/.
     
    Devin Rawlek, Mar 11, 2011
    #1
    1. Advertising

  2. Steve Klabnik, Mar 11, 2011
    #2
    1. Advertising

  3. Devin Rawlek

    Devin Rawlek Guest

    Thank you Steve. That was exactly what I needed.

    This works perfectly.

    $Str = gets.chomp
    $Str.to_i < 0
    until $Str.to_i > 0
    puts 'Sorry you cannot be that weak. Please enter a valid stat.'
    $Str = gets.chomp
    end
    $Str.to_i > StartStats.to_i
    until $Str.to_i < StartStats.to_i
    puts 'You cannot give more stats than what you have available. Please
    enter a valid stat.'
    $Str = gets.chomp
    end

    That site is an excellent resource.
    Thanks again Steve.

    --
    Posted via http://www.ruby-forum.com/.
     
    Devin Rawlek, Mar 11, 2011
    #3
  4. Devin Rawlek

    7stud -- Guest

    Devin Rawlek wrote in post #986991:
    > Thank you Steve. That was exactly what I needed.
    >
    > This works perfectly.
    >
    > $Str = gets.chomp
    > $Str.to_i < 0


    What is that line for? As written it does nothing: the comparison
    evaluates to either true or false and then the value is discarded.

    > until $Str.to_i > 0



    Note that any until loop can be written as a while loop:

    while $Str.to_i <= 0

    and while loops are much clearer in meaning.

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 11, 2011
    #4
  5. > and while loops are much clearer in meaning.

    That's a purely subjective point to make and, in my opinion, often =
    untrue.

    while !x.nil?
    // body
    end

    vs.

    until x.nil?
    // body
    end

    is a simple example where I like the until version better. Once =
    DeMorgan's
    law kicks in for certain negations, until loops can become much cleaner. =
    Example:

    while !x.nil? || y < 0
    // body
    end

    until x.nil? && y >=3D 0
    // body
    end

    I personally think the until loop there is *much* simpler to read.

    Michael Edgar

    http://carboni.ca/

    On Mar 11, 2011, at 5:57 PM, 7stud -- wrote:

    > Devin Rawlek wrote in post #986991:
    >> Thank you Steve. That was exactly what I needed.
    >>=20
    >> This works perfectly.
    >>=20
    >> $Str =3D gets.chomp
    >> $Str.to_i < 0

    >=20
    > What is that line for? As written it does nothing: the comparison=20
    > evaluates to either true or false and then the value is discarded.
    >=20
    >> until $Str.to_i > 0

    >=20
    >=20
    > Note that any until loop can be written as a while loop:
    >=20
    > while $Str.to_i <=3D 0
    >=20
    > and while loops are much clearer in meaning.
    >=20
    > --=20
    > Posted via http://www.ruby-forum.com/.
    >=20
     
    Michael Edgar, Mar 11, 2011
    #5
  6. [Note: parts of this message were removed to make it a legal post.]

    Agreed, especially in this instance. You want to process until the input is
    correct. 'while not' is just awkward.
     
    Steve Klabnik, Mar 11, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. unexpected

    Avoiding if..elsif statements

    unexpected, Aug 25, 2006, in forum: Python
    Replies:
    11
    Views:
    586
    Tal Einat
    Aug 27, 2006
  2. michaelb
    Replies:
    0
    Views:
    468
    michaelb
    Sep 30, 2006
  3. karlwijk

    if/elsif problem

    karlwijk, Apr 16, 2007, in forum: VHDL
    Replies:
    3
    Views:
    673
    quantum_dot
    Apr 18, 2007
  4. palo
    Replies:
    0
    Views:
    1,312
  5. Isaac Won
    Replies:
    9
    Views:
    407
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page