Basic Lessons on Programming in Ruby

Lesson 7 - Do Loops
What I assume you have:
  1. A Linux / UNIX / Mac computer with Ruby installed. If you don't have this, you need to Google how to get Ruby installed to compile Ruby programs from the terminal or command line.
  2. The basic ability to run commands like ls , cp , vi (or some other command-line editor) , etc.
  3. You have completed lesson 6
  4. The ability to figure things out!
---------------------------------------------

Lesson 7 - Do Loops

This lesson shows a type of loop that is regularly used in Ruby. It is called a "do loop". First we will show a simple program that illustrates how to use a do loop. The we will show a program that does exactly what the example program from the last lesson did, but it uses a "do loop" instead of a "for loop" to accomplish its task. There is a little more to learn about "do loops", but this will get us started!

  1. First, use your text editor to type the following lines into a text file, exactly as you see them here. Save the file with the filename lesson7.rb

    • #!/usr/bin/ruby
      i = 0
      4.times do
      i = i+1
      puts "i = #{i}"
      end
      puts
      thisMany=7
      puts "Now printing 1 through #{thisMany} loop iterations"
      i = 0
      thisMany.times do
      i = i+1
      puts "i = #{i}"
      end
      puts "-----------------------------"
      puts
      print "Type in a starting integer and press the 'Return' key: "
      start = gets.chomp.to_i
      puts
      print "Type in an ending integer and press the 'Return' key: "
      finish = gets.chomp.to_i
      puts
      print "Type in an increment (as an integer) and press the 'Return' key: "
      inc = gets.chomp.to_i
      puts
      # start=5
      # finish=10
      # inc=1
      iterations = (finish - start)/inc + 1
      i = start - inc
      iterations.times do
      i = i + inc
      puts "i = #{i}"
      end

  2. Now, make your program executable (chmod +x lesson7.rb) and run your program (./lesson7.rb) and look at the result. It should look like this (if you enter the same start, end, and increment values I did):

    • i = 1
      i = 2
      i = 3
      i = 4

      Now printing 1 through 7 loop iterations
      i = 1
      i = 2
      i = 3
      i = 4
      i = 5
      i = 6
      i = 7
      -----------------------------

      Type in a starting integer and press the 'Return' key: 2

      Type in an ending integer and press the 'Return' key: 11

      Type in an increment (as an integer) and press the 'Return' key: 3

      i = 2
      i = 5
      i = 8
      i = 11

  3. If anything looks different when you run your program, then check VERY carefully that you typed your program EXACTLY like the instructions above!!!

  4. If you just can't get your program to produce the same output as what I said it should look like, you probably need to email your instructor and ask him why before you proceed to the exercises below.

  5. Now, use your text editor to type the following lines into a text file, exactly as you see them here. Save the file with the filename lesson7b.rb

    • #!/usr/bin/ruby
      puts "This program prints a number, its square, it's square-root and a random"
      puts "point result raised to a decimal power for a given range of values "
      puts "specified in a for loop."
      puts
      howMany=5
      num = 1
      howMany.times do
      print num , " , " , num**2 , " , " , num**(1/2) , " , " , (num+0.27)**2.1 , "\n"
      num = num + 1
      end
      puts "------------------------------------------"
      start = 13
      finish = 16
      iterations = finish - start + 1
      num = start
      iterations.times do
      print num , " , " , num**2 , " , " , num**(1/2.0) , " , " , (num+0.27)**2.1 , "\n"
      num = num + 1
      end
      puts

  6. Now, make your program executable (chmod +x lesson7b.rb) and run your program (./lesson7b.rb) and look at the result. It should look like this (if you enter the same start, end, and increment values I did):

    • This program prints a number, its square, it's square-root and a random
      point result raised to a decimal power for a given range of values
      specified in a for loop.

      1 , 1 , 1 , 1.65191544601299
      2 , 4 , 1 , 5.59312209818106
      3 , 9 , 1 , 12.0378875076265
      4 , 16 , 1 , 21.0813557728505
      5 , 25 , 1 , 32.7945965130094
      ------------------------------------------
      13 , 169 , 3.60555127546399 , 228.049402136907
      14 , 196 , 3.74165738677394 , 265.638086722531
      15 , 225 , 3.87298334620742 , 306.240043522693
      16 , 256 , 4.0 , 349.875765089772

  7. If anything looks different when you run your program, then check VERY carefully that you typed your program EXACTLY like the instructions above!!!

  8. If you just can't get your program to produce the same output as what I said it should look like, you probably need to email your instructor and ask him why before you proceed to the exercises below.


Summary of Lesson 7 on Ruby Do Loops:
  1. Create a new program using the lines given above and name it "lesson7.rb".
  2. Create a new program using the lines given above and name it "lesson7rb.rb".
  3. Make the programs executable, run it, and make sure the output looks EXACTLY like the examples given.
  4. Notice how the "do loop" with the ".times" iterator works a little differently from the for loop.
Now, if you understood all this you should be able to do the following exercises:
  1. Make a copy of your program above and name it "lesson7ex1.rb" (cp lesson7.rb lesson7ex1.rb).
  2. Modify your lesson7ex1.rb program to use a do loop to ask the user to enter a start and an end value from the keyboard (like we asked for start and end values in our lesson6.rb program). Using these values, show the 4th power and 4th root of all the integers between those starting and ending values. Arrange your output in nice looking columns.
Return to the main Lessons page
Return to Rejoice Always Home

Last updated on ... May 15, 2012

Created on ... May 15, 2012

These lessons were created by David Joyner. All rights reserved. You may use them to learn Ruby or teach Ruby to others as long as you DO NOT CHARGE for these materials! For any other use, permission may be asked of David Joyner at david.joyner@lcu.edu.