Ruby/C2/Arithmetic-and-Relational-Operators/English
Title of script: Looping Statements
Author: Anjana Nair
Keywords: while, until, redo, break, loop, Ruby, video tutorial
Visual Cue | Narration |
Slide 1 | Welcome to the tutorial on while and until loops in Ruby. |
Slide 2 | In this tutorial we will learn to use-
|
Slide 3
System Requirements |
Here we are using
|
Slide 4
Pre-requisites |
To follow this tutorial, you must have Internet Connection.
|
Before we begin, recall that we had created “ttt” directory earlier.
Let's go to that directory. | |
Switch to the terminal window which has all the commands
for creating the directories and the prompt should be in looping-statements directory |
Then to ruby-tutorial and looping-statements directory. |
Now that we are in that folder, let’s move ahead. | |
Slide 5
“while” loop |
The syntax of the while loop in Ruby is as follows:
while “boolean expression” ruby code end
|
Switch to gedit where you have already opened the
file “while-loop.rb” with the while loop code typed inside. |
Create a new file in gedit as shown in the basic level Ruby tutorials.
Name it while-loop.rb |
I have a working example of the while loop.
You can pause the tutorial, and type the code as we go through it. | |
Highlight “while” | I have declared a while loop in this example. |
Highlight “i = 0” | First, I declared a local variable i and initialized it with value 0. |
Continue the highlight “while i >-10” | Then I declare a while loop.
This loop will execute as long as the variable i is greater than -10. |
Highlight “puts” | The puts method declared within the while loop will display the output. |
Highlight “i -= 1” | After the output is displayed, we decrement the value of i by 1. |
i will adopt this decremented value before the next iteration. | |
The variable i gets decremented in every iteration. | |
This goes on till i reaches the value -10, | |
At this point the while condition fails. | |
It subsequently breaks out of the loop and stops printing the output. | |
Now, let us switch to the terminal and type
ruby while-loop.rb and see the output. | |
Highlight the output:
The number: 0 gets printed out The number: -1 gets printed out The number: -2 gets printed out The number: -3 gets printed out The number: -4 gets printed out The number: -5 gets printed out The number: -6 gets printed out The number: -7 gets printed out The number: -8 gets printed out The number: -9 gets printed out |
The output will consist of a list of numbers 0 through -9.
<Pause> |
You should now be able to write your own while loop in Ruby.
| |
Slide 6
“until” loop |
The syntax for the until loop in Ruby is -
ruby code end Let us look at an example. |
Switch to gedit where you have already opened the
file “until-loop.rb” with the loop code typed inside. |
Create a new file in gedit as shown in the basic level Ruby tutorials.
And name it until-loop.rb |
I have a working example of the until loop.
You can pause the tutorial, and type the code as we go through it. | |
Highlight “until” | I have declared an until loop in this example. |
Highlight “i = 0” | We had declared a local variable i and initialized it to 0. |
Continue the highlight “until i < -10” | Then we declare an until loop. |
This loop will execute as long as the variable i is greater than -10. | |
Highlight “puts” | The puts method will display the output. |
Highlight “i -= 1” | After the output is displayed, value of i is decremented by 1. |
i will adopt this decremented value before the next iteration. | |
The variable i gets decremented during every iteration. | |
This goes on till i reaches the value -11. | |
At this point the until condition fails. | |
Subsequently, it breaks out of the loop and stops printing the output. | |
Now switch to the terminal and type
ruby until-loop.rb and see the output. | |
Highlight the output:
The number: 0 gets printed out The number: -1 gets printed out The number: -2 gets printed out The number: -3 gets printed out The number: -4 gets printed out The number: -5 gets printed out The number: -6 gets printed out The number: -7 gets printed out The number: -8 gets printed out The number: -9 gets printed out The number: -10 gets printed out |
The output will consist of a list of numbers 0 through -10.
<Pause> |
You should now be able to write your own until loop in Ruby.
Let's now move on to the redo construct. | |
Slide 7
“redo” |
The syntax for redo in Ruby is as follows:
(a collection of objects).each do |item| a conditional statement on an item ruby code redo end end Let us look at an example. |
Switch to gedit where you have already opened the
file “redo-loop.rb” with the loop code typed inside. |
Create a new file in gedit as shown in the basic level Ruby tutorials, and name it redo-loop.rb |
I have a working example of the redo loop.
You can pause the tutorial, and type the code as we go through it. | |
Highlight “each” | I have declared an each loop in this example. |
i|” | We have declared an each loop to iterate through numbers 10 to 20. |
Continue the highlight “if i == 20” | Then, we define an if conditional statement. |
Continue the highlight | This loop will execute for every number between 10 to 20. |
Continue the highlight | It will enter the inner if conditional block only if the value of i is equal to 20. |
Highlight “puts” | The puts method declared within the each loop displays the output. |
Continue the highlight “if i == 20” | Once the program enters the if conditional block, it will first print the output.
Then it will execute redo. |
redo will execute the iteration of the most internal loop. | |
It will do so without checking the loop condition.
Our condition being if i == 20. | |
The result will be an infinite loop, since the value of i will not change from 20. | |
Let's switch to the terminal and type
ruby redo-loop.rb
| |
Highlight the output:
The value has reached it's limit 20 The value has reached it's limit 20 The value has reached it's limit 20 The value has reached it's limit 20 The value has reached it's limit 20 The value has reached it's limit 20 The value has reached it's limit 20 The value has reached it's limit 20 The value has reached it's limit 20 The value has reached it's limit 20 The value has reached it's limit 20 .................................................. |
The output will consist of an infinite loop that never ends.
<Pause> |
Next, let us look at the break statement. | |
Slide 8
“break” statement |
The syntax for the break statement in Ruby is -
a looping statement a conditional statement break end conditional end loop
|
Switch to gedit where you have already opened the
file “break-loop.rb” with the loop code typed inside. |
Create a new file in gedit as shown in the basic level Ruby tutorials.
|
I have a working example of the break statement.
| |
Highlight “each” | I have declared an each loop in this example.
|
Highlight “puts” | The puts method here will display the output for numbers 11 to 19. |
hHighlight “if” and then "break" | Once the value becomes 20, the program enters the conditional if block.
|
Now open the terminal and type
ruby break-loop.rb
| |
Highlight the output:
The value has reached it's limit 10 The value has reached it's limit 11 The value has reached it's limit 12 The value has reached it's limit 13 The value has reached it's limit 14 The value has reached it's limit 15 The value has reached it's limit 16 The value has reached it's limit 17 The value has reached it's limit 18 The value has reached it's limit 19 |
The output will consist of numbers 10 through 19.
<Pause> |
Now you should be able to create your own break construct.
<Pause> | |
Slide 9 | <<Pause>>
This brings us to the end of this Spoken Tutorial. |
Slide 10
Summary |
Let's summarize.
In this tutorial we have learnt to use
|
Slide 11
Assignment |
As as assignment
Write a Ruby program using
|
Slide 12
About the Spoken Tutorial Project |
Watch the video available at the following link.
It summarizes the Spoken Tutorial project. If you do not have good bandwidth, you can download and watch it. |
Slide 13 | The Spoken Tutorial Project Team :
contact at spoken hyphen tutorial dot org |
Slide 14
Acknowledgements |
Spoken Tutorial Project is a part of the Talk to a Teacher project.
It is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
Previous slide | This is Anjana Nair signing off. Thank you |