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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(3 intermediate revisions by 2 users not shown)
Line 66: Line 66:
 
| 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.
  
|-
 
| 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  
 
| Type mkdir ttt and press Enter  
| In the '''terminal,''' type “'''mkdir ttt'''”.
+
| Before we begin, please create the following folders in your home directory,ttt, ruby-tutorial, looping-statements.  please create the sub- folder as mentioned here. 
 
+
|-
+
| 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'''”
+
 
+
 
|-
 
|-
 
|  
 
|  
Line 131: Line 106:
 
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.
 
  
 
|-
 
|-
Line 147: Line 121:
 
|-
 
|-
 
| Continue the highlight
 
| Continue the highlight
| The variable '''“i” '''gets initialised to the first element in the set of numbers 1 to 20.
+
| The variable '''“i” '''gets initialized to the first element in the set of numbers 1 to 20.
  
 
|-
 
|-
Line 185: Line 159:
 
| 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.
  
|-
 
| Switch back to the same file.
 
| Let's get back to the same file we were looking at.
 
  
 
|-
 
|-
Line 216: Line 187:
  
 
Point to the output
 
Point to the output
| '''No'''w open the terminal and type “'''ruby for-loop.rb'''”  
+
| '''Now open the terminal and type “'''ruby for-loop.rb'''”  
  
  
Line 261: Line 232:
  
  
Pause the tutorial while you do this.
+
  
 
|-
 
|-
Line 275: Line 246:
 
|-
 
|-
 
| Continue the highlight
 
| Continue the highlight
| The “'''i'''” variable gets initialised to the first element in the set of numbers 1 to 20.
+
| The “'''i'''” variable gets initialized to the first element in the set of numbers 1 to 20.
  
 
|-
 
|-
Line 338: Line 309:
  
 
Point to the output
 
Point to the output
| '''No'''w open the terminal and type “'''ruby for-loop.rb'''”
+
| '''Now open the terminal and type “'''ruby each-loop.rb'''”
  
  
Line 351: Line 322:
 
|-
 
|-
 
| Slide 9
 
| Slide 9
| How would we determine which looping contruct to choose?
+
| How would we determine which looping construct to choose?
  
 
Let us try to recall the “'''for'''” loop construct.
 
Let us try to recall the “'''for'''” loop construct.
Line 357: Line 328:
 
|-
 
|-
 
| Open “for-loop.rb”
 
| Open “for-loop.rb”
| 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 from 1 to 20 using “'''for'''”.
  
  
Execute “ruby for-loop.rb” in your terminal and see the output.
+
Execute “ruby for-loop.rb” in you have to anyways and see the output.
  
  
Line 401: Line 372:
  
 
Assignment
 
Assignment
| As as assignment  
+
| As an assignment  
  
 
Write a ruby program using  
 
Write a ruby program using  
Line 447: Line 418:
  
 
|-
 
|-
| Slide 16
+
| style="background-color:#ffffff;border-top:0.25pt solid #000000;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.095cm;"| Previous slide
 
+
| style="background-color:#ffffff;border-top:0.25pt solid #000000;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.095cm;"| This [http://www.ruby-software.com/ i]s Anjana Nair signing off. Thank you
About the contributor
+
 
+
 
+
  
| * This tutorial has been contributed by Ruby Software Pvt. Ltd
 
* http://www.ruby-software.com
 
* Thank you
 
 
|}
 
|}

Latest revision as of 09:05, 27 June 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.


Type mkdir ttt and press Enter Before we begin, please create the following folders in your home directory,ttt, ruby-tutorial, looping-statements. please create the sub- folder as mentioned here.


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.


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


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.



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 initialized 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 each-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 construct 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 from 1 to 20 using “for”.


Execute “ruby for-loop.rb” in you have to anyways 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 an 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.

Previous slide This is Anjana Nair signing off. Thank you

Contributors and Content Editors

Anjana, Nancyvarkey, Sneha