while loop : execute code and THEN check condition

Discussion in 'Ruby' started by Adam Akhtar, Sep 7, 2008.

  1. Adam Akhtar

    Adam Akhtar Guest

    Im sure this was possible in C but in ruby i couldnt find something
    similiar.

    I want a control structure which will execute the code first and then
    check if the condition is true or not. something like

    do

    something

    while (condition)
    --
    Posted via http://www.ruby-forum.com/.
     
    Adam Akhtar, Sep 7, 2008
    #1
    1. Advertising

  2. Adam Akhtar wrote:
    > Im sure this was possible in C but in ruby i couldnt find something
    > similiar.
    >
    > I want a control structure which will execute the code first and then
    > check if the condition is true or not. something like
    >
    > do
    >
    > something
    >
    > while (condition)


    begin
    something
    end while condition

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Sep 7, 2008
    #2
    1. Advertising

  3. Adam Akhtar

    Heesob Park Guest

    2008/9/7 Adam Akhtar <>:
    > Im sure this was possible in C but in ruby i couldnt find something
    > similiar.
    >
    > I want a control structure which will execute the code first and then
    > check if the condition is true or not. something like
    >
    > do
    >
    > something
    >
    > while (condition)
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    begin
    something
    end while condition

    Or

    begin
    something
    end until condition

    Regards,

    Park Heesob
     
    Heesob Park, Sep 7, 2008
    #3
  4. Adam Akhtar

    Robert Dober Guest

    Although
    begin
    ...
    end while condition

    is the idiomatic way to do this I often write my loops as follows

    loop do
    ...
    break if/unless condition
    ...
    end

    I prefer it even more if my code can be refactored to

    loop do
    ...
    return ...
    ...
    end

    Cheers
    Robert


    --=20
    C'est v=E9ritablement utile puisque c'est joli.

    Antoine de Saint Exup=E9ry
     
    Robert Dober, Sep 7, 2008
    #4
    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. -
    Replies:
    12
    Views:
    710
    Remon van Vliet
    Jun 15, 2005
  2. Replies:
    0
    Views:
    330
  3. Bill W.
    Replies:
    13
    Views:
    309
    Phillip Gawlowski
    May 9, 2011
  4. Replies:
    15
    Views:
    241
  5. Isaac Won
    Replies:
    9
    Views:
    420
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page