Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Discussion on loops in Ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 372
    Comment on it

    Welcome to Findnerd. Today we are going to discuss loops in Ruby. If we talk about the loops then you have used this in other programming languages but in ruby we use loops in some different ways. Loop is nothing but a piece of code which executes over and over again. There will be a condition which decides the loop turns. Please have a look.

     

    loop do 
    ...
    end

     

    In above you can see, we are using loop statement to create a loop. In this, from do to end is the block of the loop. There are different controls available to maintain the functioning of the loop. Please have a  look.

     

    A) break : This statement can stop the whole process or loop.

     

    B) next : This statement can skip the rotation as per condition.

     

    C) redo:  This statement can redo the loop.

     

    D) retry : This statement can restart the whole loop over.

     

    In above mentioned controls we will discuss only first two statements because rest two statements use in complex loops so we will discuss them later. Please have a look.

     

    v=0
    loop do
    v+=1
    end

     

     

    If we execute the above loop then it will never stop because it is an infinite loop. Now we need to use the controls like break to stop the loop on specific condition. Please have a look.

     

    v=0
    loop do
    v+=1
    break if v>=10
    puts "Demo counter " + to_s(v)
    end

     

     

    If you run above loop then output will be like below.

     

    Demo counter1
    Demo counter2
    Demo counter3
    Demo counter4
    Demo counter5
    Demo counter6
    Demo counter7
    Demo counter8
    Demo counter9

     

     

    Now we are going take an example on next statement. Please have a look.

     

    v=0
    loop do
    v+=1
    break if v>=10
    next if v==3
    puts "Demo Next " + to_s(v)
    end

     

     

    If you run the above then output will be like below.

     

    Demo Next1
    Demo Next2
    Demo Next4
    Demo Next5
    Demo Next6
    Demo Next7
    Demo Next8
    Demo Next9

     

    You can see in above output loop has been skipped the number 3.

     

    If we talk about the while loop then you have used in other programming language but in ruby it uses in different way. Please have a look.

     

    v=0
    while v<=10
    v+=1
    puts 'While loop demo' + to_s(v)
    
    end

     

     

    You can use above code. In while loop we passed the condition which will control the loop running and there is no need any other control statements like break , next etc.

     

    until loop works something different way. Please have a look.

     

    v=32
    until v<=1
    v-=1
    puts 'Until loop demo' + to_s(v)
    end

     

     

    In above code we are using until loop where we are decrementing the variable value and printing the same. We can use both loop such as while and until in only one line. Please have a look.

     

    v=0
     puts  v+=1 while v<=10
    
    v=23
     puts  v-=1 until v<=1

     

     

    Thank you for being with us!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: