Difference between revisions of "Ruby/C3/for-and-each-Looping-Statements/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': '''Looping Statements''' '''Author: Anjana Nair''' '''Keywords: for, each, loop, Ruby, video tutorial''' {| style="border-spacing:0;" | style="border…')
 
Line 2: Line 2:
  
 
'''Author: Anjana Nair'''
 
'''Author: Anjana Nair'''
 
  
 
'''Keywords: for, each, loop, Ruby, video tutorial'''
 
'''Keywords: for, each, loop, Ruby, video tutorial'''
  
 
+
{| border=1
 
+
|| '''Visual Cue'''
{| style="border-spacing:0;"
+
|| '''Narration'''
| style="border-top:1pt solid #000000;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center>
+
| style="border:1pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1
+
| Slide 1
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the tutorial on '''for '''and '''each Loops''' in '''Ruby'''.
+
| Welcome to the tutorial on '''for '''and '''each Loops''' in '''Ruby'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2
+
| Slide 2
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn
+
| In this tutorial we will learn
  
 
* Meaning of the term “loop”
 
* Meaning of the term “loop”
Line 24: Line 21:
 
* Usage of “'''for'''” loop and
 
* Usage of “'''for'''” loop and
 
* Usage of “'''each'''” looping construct
 
* Usage of “'''each'''” looping construct
*
 
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 3
+
| Slide 3
  
 
System Requirements
 
System Requirements
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we are using
+
| Here we are using
  
 
* '''Ubuntu''' version 12.04
 
* '''Ubuntu''' version 12.04
 
* '''Ruby''' 1.9.3
 
* '''Ruby''' 1.9.3
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4
+
| Slide 4
  
 
Pre-requisites
 
Pre-requisites
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To follow this tutorial, you must have '''Internet''' Connection.
+
| To follow this tutorial, you must have '''Internet''' Connection.
 
+
  
 
You must also have knowledge of '''Linux''' commands, '''Terminal''' and '''Text-editor.'''
 
You must also have knowledge of '''Linux''' commands, '''Terminal''' and '''Text-editor.'''
 
  
 
If not, for relevant tutorials, please visit our website.
 
If not, for relevant tutorials, please visit our website.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let me explain what is referred to as a “'''loop'''” in '''Ruby'''.
+
| Now let me explain what is referred to as a “'''loop'''” in '''Ruby'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5
+
| Slide 5
  
 
What is a loop?
 
What is a loop?
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| A '''loop '''is a command or set of commands that are executed a fixed number of times.
+
| A '''loop '''is a command or set of commands that are executed a fixed number of times.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6
+
| Slide 6
  
 
Looping statements
 
Looping statements
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Ruby''' has the following main looping statements.
+
| '''Ruby''' has the following main looping statements.
  
 
* '''for '''
 
* '''for '''
Line 71: Line 61:
 
* '''while'''
 
* '''while'''
 
* '''until '''
 
* '''until '''
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn how to use the '''for''' and '''each''' looping constructs.
+
| In this tutorial we will learn how to use the '''for''' and '''each''' looping constructs.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to gedit  
+
| Switch to gedit  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on “'''dash-home'''” and type '''gedit''' to invoke the editor.
+
| Click on “'''dash-home'''” and type '''gedit''' to invoke the editor.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal
+
| Switch to terminal
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Similarly, click on “'''dash-home'''” and select '''Terminal''' to open the '''terminal'''.
+
| Similarly, click on “'''dash-home'''” and select '''Terminal''' to open the '''terminal'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type mkdir ttt and press Enter  
+
| Type mkdir ttt and press Enter  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In the '''terminal,''' type “'''mkdir ttt'''”.
+
| In the '''terminal,''' type “'''mkdir ttt'''”.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type cd ttt and press Enter  
+
| Type cd ttt and press Enter  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then “'''cd'''” into that directory.
+
| Then “'''cd'''” into that directory.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type mkdir ruby-tutorial and press Enter  
+
| Type mkdir ruby-tutorial and press Enter  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then type “'''mkdir ruby-tutorial'''”,  
+
| Then type “'''mkdir ruby-tutorial'''”,  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type cd ruby-tutorial and press Enter  
+
| Type cd ruby-tutorial and press Enter  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And then “'''cd''' '''ruby-tutorial'''”.
+
| And then “'''cd''' '''ruby-tutorial'''”.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type mkdir looping-statements and press Enter  
+
| Type mkdir looping-statements and press Enter  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then type “'''mkdir looping-statements'''”  
+
| Then type “'''mkdir looping-statements'''”  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type cd looping-statements and press Enter  
+
| Type cd looping-statements and press Enter  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And then “'''cd looping-statements'''”
+
| And then “'''cd looping-statements'''”
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we have created the requisite folders.  
+
| Now we have created the requisite folders.  
  
  
Line 118: Line 106:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
+
| Slide 7
  
 
“for” loop
 
“for” loop
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The syntax of the “'''for'''” loop in '''Ruby''' is as follows:
+
| The syntax of the “'''for'''” loop in '''Ruby''' is as follows:
 
+
  
 
'''for “variable” in “a collection of objects”'''
 
'''for “variable” in “a collection of objects”'''
Line 129: Line 116:
  
 
'''end'''
 
'''end'''
 
  
 
Let us try to understand it with an example.  
 
Let us try to understand it with an example.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to gedit where you have already opened the file “for-loop.rb” with the '''for loop '''code typed inside.
+
| Switch to gedit where you have already opened the file “for-loop.rb” with the '''for loop '''code typed inside.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Create a new file in '''gedit''' as shown in the basic level '''Ruby '''tutorials.
+
| Create a new file in '''gedit''' as shown in the basic level '''Ruby '''tutorials.
 
+
  
 
And name it “'''for-loop.rb'''”
 
And name it “'''for-loop.rb'''”
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I already have a working example of the “'''for”''' loop.  
+
| I already have a working example of the “'''for”''' loop.  
  
 
You can type the code as we go through this example.
 
You can type the code as we go through this example.
 
  
 
Pause the tutorial while you do this.
 
Pause the tutorial while you do this.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight “for”
+
| Highlight “for”
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I have declared a “'''for”''' loop in this example.
+
| I have declared a “'''for”''' loop in this example.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight (1..20)
+
| Highlight (1..20)
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have a set of numbers 1 to 20.
+
| We have a set of numbers 1 to 20.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight “for i in (1..20)”
+
| Highlight “for i in (1..20)”
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We declare a variable called “'''i'''” within the “'''for”''' loop.
+
| We declare a variable called “'''i'''” within the “'''for”''' loop.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Continue the highlight
+
| Continue the highlight
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The variable '''“i” '''gets initialised to the first element in the set of numbers 1 to 20.
+
| The variable '''“i” '''gets initialised to the first element in the set of numbers 1 to 20.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Continue the highlight
+
| Continue the highlight
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The “'''for'''” loop declaration causes the code to iterate over each element in the set 1 to 20.  
+
| The “'''for'''” loop declaration causes the code to iterate over each element in the set 1 to 20.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight “puts”
+
| Highlight “puts”
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The “'''puts'''” method declared within the “'''for'''” loop is responsible for generating the output.
+
| The “'''puts'''” method declared within the “'''for'''” loop is responsible for generating the output.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the terminal and type  
+
| Switch to the terminal and type  
  
 
ruby for-loop.rb
 
ruby for-loop.rb
Line 180: Line 164:
  
 
Point to the output
 
Point to the output
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now open the '''terminal''' and type  
+
| Now open the '''terminal''' and type  
  
 
“'''ruby for-loop.rb'''”  
 
“'''ruby for-loop.rb'''”  
Line 188: Line 172:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Analyzing the output
+
| Analyzing the output
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output will be an '''array''' of numbers 1 to 20.
+
| The output will be an '''array''' of numbers 1 to 20.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this example, we declared a “'''for'''” loop for an inclusive range.
+
| In this example, we declared a “'''for'''” loop for an inclusive range.
  
 
It included all numbers from 1 to 20.
 
It included all numbers from 1 to 20.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Next we shall look at implementing the “'''for'''” loop for a non-inclusive range.
+
| Next we shall look at implementing the “'''for'''” loop for a non-inclusive range.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch back to the same file.
+
| Switch back to the same file.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's get back to the same file we were looking at.
+
| Let's get back to the same file we were looking at.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Continue to type the next part of the code.
+
| Continue to type the next part of the code.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight “for i in (1...20)”
+
| Highlight “for i in (1...20)”
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Non-inclusive means it will not include the last element in the collection of objects.
+
| Non-inclusive means it will not include the last element in the collection of objects.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Continue the highlight
+
| Continue the highlight
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here a “'''for'''” loop is implemented for a non-inclusive range of numbers 1 to 20.
+
| Here a “'''for'''” loop is implemented for a non-inclusive range of numbers 1 to 20.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Continue the highlight
+
| Continue the highlight
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| You will notice shortly that the number 20 will not be printed in the output.
+
| You will notice shortly that the number 20 will not be printed in the output.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the 3 dots only
+
| Highlight the 3 dots only
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The last digit does not get included when you have 3 dots between numbers.
+
| The last digit does not get included when you have 3 dots between numbers.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the terminal and type  
+
| Switch to the terminal and type  
  
 
ruby for-loop.rb
 
ruby for-loop.rb
Line 232: Line 216:
  
 
Point to the output
 
Point to the output
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''No'''w open the terminal and type “'''ruby for-loop.rb'''”  
+
| '''No'''w open the terminal and type “'''ruby for-loop.rb'''”  
  
  
Line 238: Line 222:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Analyzing the output
+
| Analyzing the output
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output will still be an array of numbers but will not include the number 20.
+
| The output will still be an array of numbers but will not include the number 20.
  
 
<nowiki><Pause></nowiki>
 
<nowiki><Pause></nowiki>
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, you should be capable enough to write your own “'''for'''” loop.
+
| Now, you should be capable enough to write your own “'''for'''” loop.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 8
+
| Slide 8
  
 
“each” loop
 
“each” loop
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The syntax of the “'''each'''” loop in '''Ruby '''is as follows:
+
| The syntax of the “'''each'''” loop in '''Ruby '''is as follows:
 
+
  
 
'''“a collection of objects”.each do |item|'''
 
'''“a collection of objects”.each do |item|'''
Line 264: Line 247:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to gedit where you have already opened the file “each-loop.rb” with the '''each loop '''code typed inside.
+
| Switch to gedit where you have already opened the file “each-loop.rb” with the '''each loop '''code typed inside.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Create a new file in '''gedit''' as shown in the basic level '''Ruby''' tutorials.
+
| Create a new file in '''gedit''' as shown in the basic level '''Ruby''' tutorials.
  
  
Line 271: Line 254:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I already have a working example of the “'''each”''' loop.  
+
| I already have a working example of the “'''each”''' loop.  
  
  
Line 281: Line 264:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight “for”
+
| Highlight “for”
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I have declared an “'''each'''” loop in this example.
+
| I have declared an “'''each'''” loop in this example.
  
 
We have a set of numbers 1 to 20.
 
We have a set of numbers 1 to 20.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight “for i in (1..20)”
+
| Highlight “for i in (1..20)”
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We declare a variable called “'''i'''” within the “'''each'''” loop.
+
| We declare a variable called “'''i'''” within the “'''each'''” loop.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Continue the highlight
+
| Continue the highlight
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The “'''i'''” variable gets initialised to the first element in the set of numbers 1 to 20.
+
| The “'''i'''” variable gets initialised to the first element in the set of numbers 1 to 20.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Continue the highlight
+
| Continue the highlight
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The '''“each”''' loop declaration causes the code to iterate over each element in the set 1 to 20.  
+
| The '''“each”''' loop declaration causes the code to iterate over each element in the set 1 to 20.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight “puts”
+
| Highlight “puts”
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The “'''puts'''” method declared within the “'''each'''” loop is responsible for generating the output.
+
| The “'''puts'''” method declared within the “'''each'''” loop is responsible for generating the output.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the terminal and type “'''ruby each-loop.rb'''”  
+
| Switch to the terminal and type “'''ruby each-loop.rb'''”  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now open the '''terminal''' and type “'''ruby each-loop.rb'''”  
+
| Now open the '''terminal''' and type “'''ruby each-loop.rb'''”  
  
  
Line 310: Line 293:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Analyzing the output
+
| Analyzing the output
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output will be an array of numbers 1 to 20.
+
| The output will be an array of numbers 1 to 20.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In the above example we declared an “'''each'''” loop for an inclusive range.
+
| In the above example we declared an “'''each'''” loop for an inclusive range.
  
  
Line 321: Line 304:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Next we shall look at implementing the “'''each'''” loop for a non-inclusive range.
+
| Next we shall look at implementing the “'''each'''” loop for a non-inclusive range.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch back to gedit
+
| Switch back to gedit
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's get back to the same file we were looking at.
+
| Let's get back to the same file we were looking at.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Continue to type the next part of the code.
+
| Continue to type the next part of the code.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight “for i in (1...20)”
+
| Highlight “for i in (1...20)”
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Non-inclusive means it will not include the last element in the collection of objects.
+
| Non-inclusive means it will not include the last element in the collection of objects.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Continue the highlight
+
| Continue the highlight
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here a “'''each'''” loop is implemented for a non-inclusive range of numbers 1 to 20.
+
| Here a “'''each'''” loop is implemented for a non-inclusive range of numbers 1 to 20.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Continue the highlight
+
| Continue the highlight
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| You will notice shortly that the number 20 will not be printed in the output.
+
| You will notice shortly that the number 20 will not be printed in the output.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the 3 dots only
+
| Highlight the 3 dots only
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The last digit does not get included when you have 3 dots between numbers.
+
| The last digit does not get included when you have 3 dots between numbers.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the terminal and type  
+
| Switch to the terminal and type  
  
 
ruby each-loop.rb
 
ruby each-loop.rb
Line 355: Line 338:
  
 
Point to the output
 
Point to the output
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''No'''w open the terminal and type “'''ruby for-loop.rb'''”
+
| '''No'''w open the terminal and type “'''ruby for-loop.rb'''”
  
  
Line 361: Line 344:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now you should be capable enough to write your own “'''each'''” loop.
+
| Now you should be capable enough to write your own “'''each'''” loop.
  
 
<nowiki><Pause></nowiki>
 
<nowiki><Pause></nowiki>
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 9
+
| Slide 9
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| How would we determine which looping contruct to choose?
+
| How would we determine which looping contruct to choose?
  
 
Let us try to recall the “'''for'''” loop construct.
 
Let us try to recall the “'''for'''” loop construct.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Open “for-loop.rb”
+
| Open “for-loop.rb”
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In the first example, we iterated over a set of numbers 1 to 20 using “'''for'''”.
+
| In the first example, we iterated over a set of numbers 1 to 20 using “'''for'''”.
  
  
Line 383: Line 366:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight “for number in (1..20)”
+
| Highlight “for number in (1..20)”
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| When you invoke the “'''for'''” loop, Ruby is actually calling the “'''each'''” method behind the scenes.
+
| When you invoke the “'''for'''” loop, Ruby is actually calling the “'''each'''” method behind the scenes.
  
  
Line 390: Line 373:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Since the call to “'''for'''” in turn calls “each”, it is preferable to use the “'''each'''” loop instead.
+
| Since the call to “'''for'''” in turn calls “each”, it is preferable to use the “'''each'''” loop instead.
  
 
<nowiki><Pause></nowiki>
 
<nowiki><Pause></nowiki>
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 10
+
| Slide 10
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| <nowiki><<Pause>></nowiki>
+
| <nowiki><<Pause>></nowiki>
  
 
This brings us to the end of this Spoken Tutorial.
 
This brings us to the end of this Spoken Tutorial.
Line 404: Line 387:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 11
+
| Slide 11
  
 
Summary
 
Summary
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we have learnt
+
| In this tutorial we have learnt
  
 
* Usage of “'''for'''” loop
 
* Usage of “'''for'''” loop
Line 413: Line 396:
 
* Reasoning behind using “'''each'''” instead of “'''for'''”  
 
* Reasoning behind using “'''each'''” instead of “'''for'''”  
 
* Examples of using the above looping constructs
 
* Examples of using the above looping constructs
 
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 12
+
| Slide 12
  
 
Assignment
 
Assignment
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As as assignment  
+
| As as assignment  
  
 
Write a ruby program using  
 
Write a ruby program using  
Line 431: Line 412:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 13
+
| Slide 13
  
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at the following link.
+
| Watch the video available at the following link.
  
 
It summarises the Spoken Tutorial project.
 
It summarises the Spoken Tutorial project.
Line 441: Line 422:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 14
+
| Slide 14
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team :
+
| The Spoken Tutorial Project Team :
  
  
Line 454: Line 435:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 15
+
| Slide 15
  
 
Acknowledgements
 
Acknowledgements
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project.
+
| 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.
 
It is supported by the National Mission on Education through ICT, MHRD, Government of India.
Line 466: Line 447:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 16
+
| Slide 16
  
 
About the contributor
 
About the contributor
Line 472: Line 453:
  
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| * This tutorial has been contributed by Ruby Software Pvt. Ltd
+
| * This tutorial has been contributed by Ruby Software Pvt. Ltd
 
* http://www.ruby-software.com
 
* http://www.ruby-software.com
 
* Thank you
 
* Thank you
 
 
 
 
|}
 
|}

Revision as of 13:10, 18 April 2013

Title of script: Looping Statements

Author: Anjana Nair

Keywords: for, each, loop, Ruby, video tutorial

Visual Cue Narration
Slide 1 Welcome to the tutorial on for and each Loops in Ruby.
Slide 2 In this tutorial we will learn
  • Meaning of the term “loop”
  • The different kinds of loops in Ruby
  • Usage of “for” loop and
  • Usage of “each” looping construct
Slide 3

System Requirements

Here we are using
  • Ubuntu version 12.04
  • Ruby 1.9.3
Slide 4

Pre-requisites

To follow this tutorial, you must have Internet Connection.

You must also have knowledge of Linux commands, Terminal and Text-editor.

If not, for relevant tutorials, please visit our website.

Now let me explain what is referred to as a “loop” in Ruby.
Slide 5

What is a loop?

A loop is a command or set of commands that are executed a fixed number of times.
Slide 6

Looping statements

Ruby has the following main looping statements.
  • for
  • each
  • while
  • until
In this tutorial we will learn how to use the for and each looping constructs.
Switch to gedit Click on “dash-home” and type gedit to invoke the editor.
Switch to terminal Similarly, click on “dash-home” and select Terminal to open the terminal.
Type mkdir ttt and press Enter In the terminal, type “mkdir ttt”.
Type cd ttt and press Enter Then “cd” into that directory.
Type mkdir ruby-tutorial and press Enter Then type “mkdir ruby-tutorial”,
Type cd ruby-tutorial and press Enter And then “cd ruby-tutorial”.
Type mkdir looping-statements and press Enter Then type “mkdir looping-statements
Type cd looping-statements and press Enter And then “cd looping-statements
Now we have created the requisite folders.


Let's move ahead.

Slide 7

“for” loop

The syntax of the “for” loop in Ruby is as follows:

for “variable” in “a collection of objects”

ruby code

end

Let us try to understand it with an example.

Switch to gedit where you have already opened the file “for-loop.rb” with the for loop code typed inside. Create a new file in gedit as shown in the basic level Ruby tutorials.

And name it “for-loop.rb

I already have a working example of the “for” loop.

You can type the code as we go through this example.

Pause the tutorial while you do this.

Highlight “for” I have declared a “for” loop in this example.
Highlight (1..20) We have a set of numbers 1 to 20.
Highlight “for i in (1..20)” We declare a variable called “i” within the “for” loop.
Continue the highlight The variable “i” gets initialised to the first element in the set of numbers 1 to 20.
Continue the highlight The “for” loop declaration causes the code to iterate over each element in the set 1 to 20.
Highlight “puts” The “puts” method declared within the “for” loop is responsible for generating the output.
Switch to the terminal and type

ruby for-loop.rb


Point to the output

Now open the terminal and type

ruby for-loop.rb


and see the output.

Analyzing the output The output will be an array of numbers 1 to 20.
In this example, we declared a “for” loop for an inclusive range.

It included all numbers from 1 to 20.

Next we shall look at implementing the “for” loop for a non-inclusive range.
Switch back to the same file. Let's get back to the same file we were looking at.
Continue to type the next part of the code.
Highlight “for i in (1...20)” Non-inclusive means it will not include the last element in the collection of objects.
Continue the highlight Here a “for” loop is implemented for a non-inclusive range of numbers 1 to 20.
Continue the highlight You will notice shortly that the number 20 will not be printed in the output.
Highlight the 3 dots only The last digit does not get included when you have 3 dots between numbers.
Switch to the terminal and type

ruby for-loop.rb


Point to the output

Now open the terminal and type “ruby for-loop.rb


and see the output.

Analyzing the output The output will still be an array of numbers but will not include the number 20.

<Pause>

Now, you should be capable enough to write your own “for” loop.
Slide 8

“each” loop

The syntax of the “each” loop in Ruby is as follows:

“a collection of objects”.each do |item|

ruby code

end


Let us try to understand it with an example.

Switch to gedit where you have already opened the file “each-loop.rb” with the each loop code typed inside. Create a new file in gedit as shown in the basic level Ruby tutorials.


And name it “each-loop.rb

I already have a working example of the “each” loop.


You can type the code as we go through this example.


Pause the tutorial while you do this.

Highlight “for” I have declared an “each” loop in this example.

We have a set of numbers 1 to 20.

Highlight “for i in (1..20)” We declare a variable called “i” within the “each” loop.
Continue the highlight The “i” variable gets initialised to the first element in the set of numbers 1 to 20.
Continue the highlight The “each” loop declaration causes the code to iterate over each element in the set 1 to 20.
Highlight “puts” The “puts” method declared within the “each” loop is responsible for generating the output.
Switch to the terminal and type “ruby each-loop.rb Now open the terminal and type “ruby each-loop.rb


and see the output.

Analyzing the output The output will be an array of numbers 1 to 20.
In the above example we declared an “each” loop for an inclusive range.


It included all numbers from 1 to 20.

Next we shall look at implementing the “each” loop for a non-inclusive range.
Switch back to gedit Let's get back to the same file we were looking at.
Continue to type the next part of the code.
Highlight “for i in (1...20)” Non-inclusive means it will not include the last element in the collection of objects.
Continue the highlight Here a “each” loop is implemented for a non-inclusive range of numbers 1 to 20.
Continue the highlight You will notice shortly that the number 20 will not be printed in the output.
Highlight the 3 dots only The last digit does not get included when you have 3 dots between numbers.
Switch to the terminal and type

ruby each-loop.rb


Point to the output

Now open the terminal and type “ruby for-loop.rb


and see the output.

Now you should be capable enough to write your own “each” loop.

<Pause>

Slide 9 How would we determine which looping contruct to choose?

Let us try to recall the “for” loop construct.

Open “for-loop.rb” In the first example, we iterated over a set of numbers 1 to 20 using “for”.


Execute “ruby for-loop.rb” in your terminal and see the output.


Now look at the code in gedit.

Highlight “for number in (1..20)” When you invoke the “for” loop, Ruby is actually calling the “each” method behind the scenes.


And calling “each” or “for” will generate the same output.

Since the call to “for” in turn calls “each”, it is preferable to use the “each” loop instead.

<Pause>

Slide 10 <<Pause>>

This brings us to the end of this Spoken Tutorial.

Let's summarize.

Slide 11

Summary

In this tutorial we have learnt
  • Usage of “for” loop
  • Usage of “each” construct
  • Reasoning behind using “each” instead of “for
  • Examples of using the above looping constructs
Slide 12

Assignment

As as assignment

Write a ruby program using

  • the appropriate loop construct
  • to create an output of even numbers
  • from a set of numbers, say 1 to 20


Slide 13

About the Spoken Tutorial Project

Watch the video available at the following link.

It summarises the Spoken Tutorial project.

If you do not have good bandwidth, you can download and watch it.

Slide 14 The Spoken Tutorial Project Team :


Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to

contact at spoken hyphen tutorial dot org

Slide 15

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.

Slide 16

About the contributor


* This tutorial has been contributed by Ruby Software Pvt. Ltd

Contributors and Content Editors

Anjana, Nancyvarkey, Sneha