Difference between revisions of "Ruby/C3/while-and-until-Looping-Statements/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': '''Looping Statements''' '''Author: Anjana Nair''' '''Keywords: while, until, redo, break, loop, Ruby, video tutorial''' {| style="border-spacing:0;"…')
 
 
Line 2: Line 2:
  
 
'''Author: Anjana Nair'''
 
'''Author: Anjana Nair'''
 
  
 
'''Keywords: while, until, redo, break, loop, Ruby, video tutorial'''
 
'''Keywords: while, until, redo, break, loop, Ruby, video tutorial'''
  
  
 
+
{| border=1
{| style="border-spacing:0;"
+
| '''Visual Cue'''
| style="background-color:#ffffff;border-top:1pt 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;"| <center>'''Visual Cue'''</center>
+
| '''Narration'''
| style="background-color:#ffffff;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.095cm;"| <center>'''Narration'''</center>
+
 
+
 
|-
 
|-
| 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;"| Slide 1
+
| Slide 1
| 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;"| Welcome to the tutorial on '''while '''and '''until loops '''in '''Ruby'''.
+
| Welcome to the tutorial on '''while '''and '''until loops '''in '''Ruby'''.
  
 
|-
 
|-
| 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;"| Slide 2
+
| Slide 2
| 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;"| In this tutorial we will learn to use-
+
| In this tutorial we will learn to use-
  
 
* '''while''' loop
 
* '''while''' loop
Line 24: Line 21:
 
* '''redo'''
 
* '''redo'''
 
* '''break'''
 
* '''break'''
 
 
  
 
|-
 
|-
| 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;"| Slide 3
+
| Slide 3
  
 
System Requirements
 
System Requirements
| 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;"| 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="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;"| Slide 4
+
| Slide 4
  
 
Pre-requisites
 
Pre-requisites
| 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;"| 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="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;"|  
+
|  
| 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;"| Before we begin, recall that we had created “ttt” directory earlier.
+
| Before we begin, recall that we had created '''ttt''' directory earlier.
 
+
  
 
Let's go to that directory.
 
Let's go to that directory.
  
 
|-
 
|-
| 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;"| Switch to the terminal window which has all the commands for creating the directories and the prompt should be in '''looping-statements '''directory
+
| Switch to the terminal window which has all the commands  
| 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;"| Then to ruby-tutorial and looping-statements directory.
+
 
+
  
 +
for creating the directories and the prompt should be in
  
 +
'''looping-statements '''directory
 +
| Then to '''ruby-tutorial''' and '''looping-statements''' directory.
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| Now that we are in that folder, let’s move ahead.
+
| Now that we are in that folder, let’s move ahead.
  
 
|-
 
|-
| 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;"| Slide 5
+
| Slide 5
  
 
“while” loop
 
“while” loop
| 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;"| The syntax of the '''while''' loop in '''Ruby '''is as follows:
+
| The syntax of the '''while''' loop in '''Ruby '''is as follows:
  
  
Line 80: Line 71:
  
 
'''end'''
 
'''end'''
 
  
 
Let us look at an example.
 
Let us look at an example.
  
 
|-
 
|-
| 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;"| Switch to gedit where you have already opened the file “while-loop.rb” with the '''while loop '''code typed inside.
+
| Switch to '''gedit''' where you have already opened
| 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;"| Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
+
  
 +
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'''
 
Name it '''while-loop.rb'''
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| I have a working example of the '''while '''loop.
+
| I have a working example of the '''while '''loop.
 
+
  
 
You can pause the tutorial, and type the code as we go through it.
 
You can pause the tutorial, and type the code as we go through it.
  
 
|-
 
|-
| 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;"| Highlight “while”
+
| Highlight “while”
| 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;"| I have declared a '''while '''loop in this example.
+
| I have declared a '''while '''loop in this example.
  
 
|-
 
|-
| 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;"| Highlight “i = 0”
+
| Highlight “i = 0”
| 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;"| First, I declared a local variable '''i '''and initialized it with value '''0'''.
+
| First, I declared a local variable '''i '''and initialized it with value '''0'''.
  
 
|-
 
|-
| 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;"| Continue the highlight “while i >-10”
+
| Continue the highlight “while i >-10”
| 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;"| Then I declare a '''while''' loop.
+
| Then I declare a '''while''' loop.
  
 
This loop will execute as long as the variable '''i '''is greater than '''-10'''.  
 
This loop will execute as long as the variable '''i '''is greater than '''-10'''.  
  
 
|-
 
|-
| 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;"| Highlight “puts”
+
| Highlight “puts”
| 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;"| The '''puts''' method declared within the '''while''' loop will display the output.
+
| The '''puts''' method declared within the '''while''' loop will display the output.
  
 
|-
 
|-
| 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;"| Highlight “i -= 1”
+
| Highlight “i -= 1”
| 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;"| After the output is displayed, we decrement the value of '''i''' by 1.
+
| After the output is displayed, we decrement the value of '''i''' by 1.
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| '''i''' will adopt this decremented value before the next iteration.
+
| '''i''' will adopt this decremented value before the next iteration.
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:none;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;"|  
+
|
| style="background-color:#ffffff;border-top:none;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;"| The variable '''i '''gets decremented in every iteration.
+
|The variable '''i '''gets decremented in every iteration.
  
 
|-
 
|-
| 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;"|  
+
|  
| 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 goes on till i''' '''reaches the value '''-10''',  
+
| This goes on till i''' '''reaches the value '''-10''',  
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| At this point the '''while condition '''fails.
+
| At this point the '''while condition '''fails.
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:none;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;"|  
+
|
| style="background-color:#ffffff;border-top:none;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;"| It subsequently breaks out of the loop and stops printing the output.
+
| It subsequently breaks out of the loop and stops printing the output.
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| Now, let us switch to the '''terminal '''and type
+
| Now, let us switch to the '''terminal '''and type '''ruby while-loop.rb''' and see the output.
 
+
'''ruby while-loop.rb'''  
+
 
+
 
+
and see the output.
+
  
 
|-
 
|-
| 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;"| Highlight the output:
+
| Highlight the output:
 
+
  
 
The number: 0 gets printed out
 
The number: 0 gets printed out
Line 172: Line 158:
  
 
The number: -9 gets printed out
 
The number: -9 gets printed out
| 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;"| The output will consist of a list of numbers '''0''' through '''-9'''.
+
 
 +
| The output will consist of a list of numbers '''0''' through '''-9'''.
  
 
<nowiki><Pause></nowiki>
 
<nowiki><Pause></nowiki>
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| You should now be able to write your own '''while''' loop in '''Ruby'''.
+
| You should now be able to write your own '''while''' loop in '''Ruby'''.
 
+
  
 
Let's look at the '''until''' loop next.
 
Let's look at the '''until''' loop next.
  
 
|-
 
|-
| 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;"| Slide 6
+
| Slide 6
  
 
“until” loop
 
“until” loop
| 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;"| The syntax for the '''until''' loop in '''Ruby '''is -
+
| The syntax for the '''until''' loop in '''Ruby '''is -
  
  
Line 195: Line 181:
  
 
'''end'''
 
'''end'''
 
  
 
Let us look at an example.
 
Let us look at an example.
  
 
|-
 
|-
| 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;"| Switch to gedit where you have already opened the file “until-loop.rb” with the '''loop '''code typed inside.
+
| Switch to gedit where you have already opened  
| 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;"| Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
+
  
 +
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'''
 
And name it '''until-loop.rb'''
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| I have a working example of the '''until '''loop.
+
| I have a working example of the '''until '''loop.
 
+
  
 
You can pause the tutorial, and type the code as we go through it.
 
You can pause the tutorial, and type the code as we go through it.
  
 
|-
 
|-
| 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;"| Highlight “until”
+
| Highlight “until”
| 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;"| I have declared an '''until '''loop in this example.
+
| I have declared an '''until '''loop in this example.
  
 
|-
 
|-
| 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;"| Highlight “i = 0”
+
| Highlight “i = 0”
| 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;"| We had declared a local variable '''i '''and initialized it to '''0'''.
+
| We had declared a local variable '''i '''and initialized it to '''0'''.
  
 
|-
 
|-
| 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;"| <nowiki>Continue the highlight “until i < -10” </nowiki>
+
| <nowiki>Continue the highlight “until i < -10” </nowiki>
| 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;"| Then we declare an '''until''' loop.
+
| Then we declare an '''until''' loop.
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:none;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;"|  
+
|
| style="background-color:#ffffff;border-top:none;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 loop will execute as long as the variable '''i '''is greater than '''-10'''.
+
| This loop will execute as long as the variable '''i '''is greater than '''-10'''.
  
 
|-
 
|-
| 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;"| Highlight “puts”
+
| Highlight “puts”
| 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;"| The '''puts''' method will display the output.
+
| The '''puts''' method will display the output.
  
 
|-
 
|-
| 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;"| Highlight “i -= 1”
+
| Highlight “i -= 1”
| 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;"| After the output is displayed, value of '''i''' is decremented by 1.
+
| After the output is displayed, value of '''i''' is decremented by 1.
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| '''i''' will adopt this decremented value before the next iteration.
+
| '''i''' will adopt this decremented value before the next iteration.
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:none;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;"|  
+
|
| style="background-color:#ffffff;border-top:none;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;"| The variable '''i '''gets decremented during every iteration.
+
|The variable '''i '''gets decremented during every iteration.
  
 
|-
 
|-
| 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;"|  
+
|  
| 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 goes on till '''i '''reaches the value '''-11'''.
+
| This goes on till '''i '''reaches the value '''-11'''.
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| At this point the '''until condition '''fails.
+
| At this point the '''until condition '''fails.
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:none;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;"|  
+
|  
| style="background-color:#ffffff;border-top:none;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;"| Subsequently, it breaks out of the loop and stops printing the output.
+
| Subsequently, it breaks out of the loop and stops printing the output.
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| Now switch to the '''terminal '''and type
+
| Now switch to the '''terminal '''and type '''ruby until-loop.rb '''and see the output.
 
+
'''ruby until-loop.rb '''and see the output.
+
  
 
|-
 
|-
| 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;"| Highlight the output:
+
| Highlight the output:
 
+
  
 
The number: 0 gets printed out
 
The number: 0 gets printed out
Line 288: Line 272:
  
 
The number: -10 gets printed out
 
The number: -10 gets printed out
| 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;"| The output will consist of a list of numbers '''0''' through '''-10'''.
+
 
 +
| The output will consist of a list of numbers '''0''' through '''-10'''.
  
 
<nowiki><Pause></nowiki>
 
<nowiki><Pause></nowiki>
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| You should now be able to write your own '''until''' loop in '''Ruby'''.
+
| You should now be able to write your own '''until''' loop in '''Ruby'''.
  
 
Let's now move on to the '''redo''' construct.
 
Let's now move on to the '''redo''' construct.
  
 
|-
 
|-
| 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;"| Slide 7
+
| Slide 7
  
 
“'''redo'''”
 
“'''redo'''”
| 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;"| The syntax for '''redo''' in '''Ruby '''is as follows:
+
| The syntax for '''redo''' in '''Ruby '''is as follows:
 
+
  
 
'''(a collection of objects).each do |item|'''
 
'''(a collection of objects).each do |item|'''
Line 321: Line 305:
  
 
|-
 
|-
| 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;"| Switch to gedit where you have already opened the file “redo-loop.rb” with the '''loop '''code typed inside.
+
| Switch to gedit where you have already opened  
| 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;"| Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials, and name it '''redo-loop.rb'''
+
  
|-
+
the file '''redo-loop.rb''' with the '''loop '''
| 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;"|
+
| 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;"| I have a working example of the '''redo '''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.
 
You can pause the tutorial, and type the code as we go through it.
  
 
|-
 
|-
| 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;"| Highlight “'''each'''”
+
| Highlight “'''each'''”
| 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;"| I have declared an '''each '''loop in this example.
+
| I have declared an '''each '''loop in this example.
  
 
|-
 
|-
| 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;"| Highlight “(10..20).each do |i|”
+
| Highlight “(10..20).each do |i|”
| 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;"| We have declared an '''each''' loop to iterate through numbers 10 to 20.
+
| We have declared an '''each''' loop to iterate through numbers 10 to 20.
  
 
|-
 
|-
| 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;"| Continue the highlight “if i == 20”
+
| Continue the highlight “if i == 20”
| 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;"| Then, we define an '''if '''conditional statement.
+
| Then, we define an '''if '''conditional statement.
  
 
|-
 
|-
| 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;"| Continue the highlight
+
| Continue the highlight
| 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 loop will execute for every number between '''10 '''to '''20'''.
+
| This loop will execute for every number between '''10 '''to '''20'''.
  
 
|-
 
|-
| 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;"| Continue the highlight
+
| Continue the highlight
| 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;"| It will enter the inner '''if '''conditional block only if the value of '''i''' is equal to '''20'''.
+
| It will enter the inner '''if '''conditional block only if the value of '''i''' is equal to '''20'''.
  
 
|-
 
|-
| 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;"| Highlight “puts”
+
| Highlight “puts”
| 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;"| The '''puts''' method declared within the '''each''' loop displays the output.
+
| The '''puts''' method declared within the '''each''' loop displays the output.
  
 
|-
 
|-
| 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;"| Continue the highlight “if i == 20”
+
| Continue the highlight “if i == 20”
| 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;"| Once the program enters the '''if '''conditional block, it will first print the output.
+
| Once the program enters the '''if '''conditional block, it will first print the output.
  
 
Then it will execute '''redo'''.
 
Then it will execute '''redo'''.
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| '''redo''' will execute the iteration of the most internal loop.
+
| '''redo''' will execute the iteration of the most internal loop.
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| It will do so without checking the loop condition.
+
| It will do so without checking the loop condition.
  
 
Our condition being '''if i == 20'''.
 
Our condition being '''if i == 20'''.
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| The result will be an infinite loop, since the value of '''i''' will not change from '''20'''.
+
| The result will be an infinite loop, since the value of '''i''' will not change from '''20'''.
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| Let's switch to the '''terminal '''and type
+
| Let's switch to the '''terminal '''and type '''ruby redo-loop.rb'''  
 
+
'''ruby redo-loop.rb'''  
+
 
+
  
 
and see the output.
 
and see the output.
  
 
|-
 
|-
| 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;"| Highlight the output:
+
| Highlight the output:
 
+
  
 
>ruby redo-loop.rb
 
>ruby redo-loop.rb
Line 413: Line 396:
  
 
..................................................
 
..................................................
 +
| The output will consist of an infinite loop that never ends.
  
  
 
+
Press '''Ctrl + C''' to terminate the infinite loop
| 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;"| The output will consist of an infinite loop that never ends.
+
 
+
 
+
Press Ctrl + c to terminate the infinite loop
+
  
 
<nowiki><Pause></nowiki>
 
<nowiki><Pause></nowiki>
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| Next, let us look at the '''break''' statement.
+
| Next, let us look at the '''break''' statement.
  
 
|-
 
|-
| 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;"| Slide 8
+
| Slide 8
  
 
“'''break'''” statement
 
“'''break'''” statement
| 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;"| The syntax for the '''break''' statement in '''Ruby '''is -
+
| The syntax for the '''break''' statement in '''Ruby '''is -
  
 
'''a looping statement'''
 
'''a looping statement'''
Line 446: Line 426:
 
Let us look at an example.
 
Let us look at an example.
  
 +
|-
 +
| 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.
| 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;"| Switch to gedit where you have already opened the file “break-loop.rb” with the '''loop '''code typed inside.
+
| 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;"| Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
+
 
+
  
 
And name it '''break-loop.rb'''
 
And name it '''break-loop.rb'''
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| I have a working example of the '''break '''statement.
+
| I have a working example of the '''break '''statement.
 
+
  
 
You can pause the tutorial, and type the code as we go through this example.
 
You can pause the tutorial, and type the code as we go through this example.
  
 
|-
 
|-
| 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;"| Highlight “'''each'''”
+
| Highlight “'''each'''”
| 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;"| I have declared an '''each '''loop in this example.
+
| I have declared an '''each '''loop in this example.
 
+
  
 
It is similar to the one we used earlier.
 
It is similar to the one we used earlier.
  
 
|-
 
|-
| 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;"| Highlight “puts”
+
| Highlight “puts”
| 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;"| The '''puts''' method here will display the output for numbers '''11''' to '''19'''.
+
| The '''puts''' method here will display the output for numbers '''11''' to '''19'''.
  
 
|-
 
|-
| 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;"| hHighlight “if” and then "break"
+
| Highlight “if” and then "break"
| 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;"| Once the value becomes '''20,''' the program enters the conditional '''if''' block.
+
| Once the value becomes '''20,''' the program enters the conditional '''if''' block.
  
  
Line 482: Line 460:
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| Now open the '''terminal '''and type
+
| Now open the '''terminal '''and type
  
 
'''ruby break-loop.rb '''
 
'''ruby break-loop.rb '''
 
  
 
and see the output.
 
and see the output.
  
 
|-
 
|-
| 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;"| Highlight the output:
+
| Highlight the output:
 
+
  
 
>ruby break-loop.rb
 
>ruby break-loop.rb
Line 515: Line 491:
  
 
The value has reached it's limit 19
 
The value has reached it's limit 19
| 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;"| The output will consist of numbers '''10''' through '''19'''.
+
 
 +
| The output will consist of numbers '''10''' through '''19'''.
  
 
<nowiki><Pause></nowiki>
 
<nowiki><Pause></nowiki>
  
 
|-
 
|-
| 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;"|  
+
|  
| 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;"| Now you should be able to create your own '''break '''construct.
+
| Now you should be able to create your own '''break '''construct.
  
 
<nowiki><Pause></nowiki>
 
<nowiki><Pause></nowiki>
  
 
|-
 
|-
| 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;"| Slide 9
+
| Slide 9
| 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;"| <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.
  
 
|-
 
|-
| 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;"| Slide 10
+
| Slide 10
  
 
Summary
 
Summary
| 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;"| Let's summarize.
+
| Let's summarize.
  
 
In this tutorial we have learnt to use
 
In this tutorial we have learnt to use
Line 543: Line 520:
 
* '''redo'''
 
* '''redo'''
 
* '''break''' construct
 
* '''break''' construct
 
 
  
 
|-
 
|-
| 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;"| Slide 11
+
| Slide 11
  
 
Assignment
 
Assignment
| 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;"| As as assignment
+
| As as assignment
  
 
Write a '''Ruby '''program using
 
Write a '''Ruby '''program using
  
* (I have yet to come up with an assignment)
+
*  
 
+
 
+
  
 
|-
 
|-
| 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;"| Slide 12
+
| Slide 12
  
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
| 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;"| Watch the video available at the following link.
+
| Watch the video available at the following link.
  
 
It summarizes the Spoken Tutorial project.
 
It summarizes the Spoken Tutorial project.
Line 569: Line 542:
  
 
|-
 
|-
| 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;"| Slide 13
+
| Slide 13
| 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;"| The Spoken Tutorial Project Team :
+
| The Spoken Tutorial Project Team :
  
  
Line 582: Line 555:
  
 
|-
 
|-
| 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;"| Slide 14
+
| Slide 14
  
 
Acknowledgements
 
Acknowledgements
| 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;"| 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 594: Line 567:
  
 
|-
 
|-
| 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
+
| 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
+
| This is Anjana Nair signing off. Thank you
  
 
|}
 
|}

Latest revision as of 19:55, 18 August 2013

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-
  • while loop
  • until loop
  • redo
  • break
Slide 3

System Requirements

Here we are using
  • Ubuntu version 12.04
  • Ruby1.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.

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

Let us look at an example.

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.

Let's look at the until loop next.

Slide 6

“until” loop

The syntax for the until loop in Ruby is -


until “boolean expression”

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

and see the output.

Highlight the output:

>ruby redo-loop.rb

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.


Press Ctrl + C to terminate the infinite loop

<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


Let us look at an example.

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.

And name it break-loop.rb

I have a working example of the break statement.

You can pause the tutorial, and type the code as we go through this example.

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

It is similar to the one we used earlier.

Highlight “puts” The puts method here will display the output for numbers 11 to 19.
Highlight “if” and then "break" Once the value becomes 20, the program enters the conditional if block.


At this point, it will encounter the break statement and break out of the loop.

Now open the terminal and type

ruby break-loop.rb

and see the output.

Highlight the output:

>ruby break-loop.rb

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

  • while loop
  • until construct
  • redo
  • break construct
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 :


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

Contributors and Content Editors

Anjana, Nancyvarkey