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)
     
    Adam Akhtar, Sep 7, 2008
    #1
    1. Advertisements

  2. begin
    something
    end while condition
     
    Joel VanderWerf, Sep 7, 2008
    #2
    1. Advertisements

  3. Adam Akhtar

    Heesob Park Guest

    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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.