Difference between revisions of "Ruby/C2/Arithmetic-and-Relational-Operators/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
'''Title of script''': Arithmetic & Relational Operators in Ruby
+
'''Title of script''': '''Looping Statements'''
  
'''Author:Spoken Tutorial Team, IIT Bombay'''
+
'''Author: Anjana Nair'''
  
'''Keywords: Arithmetic Operators, Relational Operators, video tutorial'''
+
 
 +
'''Keywords: while, until, redo, break, loop, Ruby, video tutorial'''
  
  
Line 10: Line 11:
 
| '''Visual Cue'''
 
| '''Visual Cue'''
 
| '''Narration'''
 
| '''Narration'''
 
 
|-
 
|-
| Slide 1
+
|Slide 1
| Welcome to the Spoken Tutorial on Arithmetic & Relational Operators in '''Ruby.'''
+
|Welcome to the tutorial on '''while '''and '''until loops '''in '''Ruby'''.
  
 
|-
 
|-
| Slide 2
+
|Slide 2
 
+
|In this tutorial we will learn to use-
Learning Objectives
+
* '''while''' loop
| In this tutorial we will learn about
+
* '''until '''loop
 
+
* '''redo'''
* Arithmetic Operators
+
* '''break'''
* Operator Precedence
+
* Relational Operators<br/>
+
  
 
|-
 
|-
| Slide 3
+
|Slide 3
  
 
System Requirements
 
System Requirements
| Here we are using  
+
|Here we are using
  
* '''Ubuntu linux''' version 12.04  
+
* '''Ubuntu '''version 12.04
* '''Ruby''' 1.9.3
+
* '''Ruby'''1.9.3
  
 
|-
 
|-
| Slide 4
+
|Slide 4
  
 
Pre-requisites
 
Pre-requisites
| To follow this tutorial you must know how to use''' Terminal''' and Text editor in '''Linux.'''
+
|To follow this tutorial, you must have '''Internet '''Connection.
  
You must also be familiar with '''irb'''
 
  
If not, for relevant tutorials, please visit our website
+
You must also have knowledge of '''Linux '''commands, '''Terminal '''and '''Text-editor.'''
  
|-
 
| Slide 5
 
  
Arithmetic Operators
+
If not, for relevant tutorials, please visit our website.
  
(need to change the slide)
+
|-
| Now let us learn about arithmetic operators.
+
|
 +
|Before we begin, recall that we had created “ttt” directory earlier.
  
'''Ruby''' has following arithmetic operators.
+
Let's go to that directory.
  
+ Addition: eg. a+b.
+
|-
 +
|Switch to the terminal window which has all the commands
 +
 +
for creating the directories and the prompt should be in
  
- Subtraction: eg. a-b.  
+
'''looping-statements''' directory
 +
|Then to ruby-tutorial and looping-statements directory.
  
/ Division: eg. a/b.  
+
|-
 +
|
 +
|Now that we are in that folder, let’s move ahead.
  
<nowiki>* Multiplication: eg. a*b. </nowiki>
+
|-
 +
|Slide 5
  
% Modulus: eg. a%b.
+
“while” loop
 +
|The syntax of the '''while''' loop in '''Ruby '''is as follows:
  
<nowiki>** Exponent : eg a**b</nowiki>
+
'''while “boolean expression”'''
  
|-
+
'''ruby code'''
|
+
| Let us try these arithmetic operators using '''irb'''.
+
 
+
|-
+
| Switch to the terminal
+
  
'''Press Ctrl+alt+t'''
+
'''end'''
  
| Open the terminal by pressing '''Ctrl''', '''Alt '''and '''T''' keys simultaneously.
 
  
A terminal window appears on your screen.
+
Let us look at an example.
  
 
|-
 
|-
| Type '''irb '''<nowiki><<press</nowiki>''' '''enter
+
|Switch to gedit where you have already opened the
  
| Type''' irb'''
+
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.
  
and press''' Enter''' to launch the''' interactive Ruby.'''
+
Name it '''while-loop.rb'''
  
 
|-
 
|-
| Type '''10+ 20 '''<nowiki><<press</nowiki> enter
+
|
| Type '''10 plus 20'''
+
|I have a working example of the '''while '''loop.
  
and press '''Enter'''.
+
You can pause the tutorial, and type the code as we go through it.
  
 
|-
 
|-
| Highlight''' 30'''
+
|Highlight “while”
| The addition operation is performed and the result '''30''' is displayed.
+
|I have declared a '''while '''loop in this example.
  
 
|-
 
|-
|  
+
|Highlight “i = 0”
| Similarly the subtraction and multiplication operations can be performed.
+
|First, I declared a local variable '''i '''and initialized it with value '''0'''.
  
 
|-
 
|-
|  
+
|Continue the highlight “while i >-10”
| Let us try the division operator.
+
|Then I declare a '''while''' loop.
 +
 
 +
This loop will execute as long as the variable '''i '''is greater than '''-10'''.  
  
 
|-
 
|-
| Type '''10/4'''<nowiki><<press </nowiki>Enter
+
|Highlight “puts”
| Type '''10 slash 4'''
+
|The '''puts''' method declared within the '''while''' loop will display the output.
  
and press '''Enter'''.
+
|-
 +
|Highlight “i -= 1”
 +
|After the output is displayed, we decrement the value of '''i''' by 1.
  
 
|-
 
|-
| Highlight''' 2'''
+
|
| Here you can see the result is truncated to the nearest whole number which is '''2.'''
+
|'''i''' will adopt this decremented value before the next iteration.
  
 
|-
 
|-
|  
+
|
| To get a more accurate answer, we need to express one number as '''float'''
+
| The variable '''i '''gets decremented in every iteration.
  
 
|-
 
|-
| Type '''10.0/4 '''<nowiki><<press Enter</nowiki>
+
|
| Type '''10.0 slash 4'''
+
|This goes on till i''' '''reaches the value '''-10''',
 
+
and press '''Enter'''.
+
  
 
|-
 
|-
| Highlight '''2.5'''
+
|
| Now we get the result as '''2.5'''
+
|At this point the '''while condition '''fails.
  
 
|-
 
|-
 
|  
 
|  
| Let's now try the '''modulus '''operator.
+
| It subsequently breaks out of the loop and stops printing the output.
 
+
The '''modulus''' operator returns the remainder as output.
+
  
 
|-
 
|-
| Type '''12%5'''
+
|
 +
|Now, let us switch to the '''terminal '''and type
  
press Enter
+
'''ruby while-loop.rb'''  
| Type '''12 percentage sign 5 '''
+
  
and press '''Enter'''.
+
and see the output.
  
 
|-
 
|-
| Highligtht '''2 '''
+
|Highlight the output:
| Here '''12 '''is divided by '''5''' and the remainder''' 2 '''is returned back.
+
  
|-
+
The number: 0 gets printed out
|
+
| Now let's try the '''exponent''' operator.
+
  
|-
+
The number: -1 gets printed out
| Type''' 2**5'''<nowiki><<press </nowiki>'''enter'''
+
| Type '''2 followed by the asterisk symbol twice and then 5 '''and press '''Enter'''.
+
  
|-
+
The number: -2 gets printed out
|
+
| This means that '''2''' is raised to the power of '''5.'''
+
  
|-
+
The number: -3 gets printed out
| Highlight '''32'''
+
| So we get the output as '''32.'''
+
  
|-
+
The number: -4 gets printed out
|
+
| Next, let us learn about operator precedence.
+
  
|-
+
The number: -5 gets printed out
| Slide 6
+
  
'''What is Operator Precedence?'''
+
The number: -6 gets printed out
  
| When several operations occur in a mathematical expression,
+
The number: -7 gets printed out
  
* each part is evaluated
+
The number: -8 gets printed out
* and resolved in a predetermined order
+
 
* called '''operator precedence'''.
+
The number: -9 gets printed out
 +
|The output will consist of a list of numbers '''0''' through '''-9'''.
 +
 
 +
<nowiki><Pause></nowiki>
  
 
|-
 
|-
| Slide 7
+
|
 +
|You should now be able to write your own '''while''' loop in '''Ruby'''.
  
'''What is Operator Precedence?'''
 
| This means that the operator which has '''highest priority''' is executed first.
 
  
This is then followed by the next operator in the '''priority''' order and so on.
+
Let's look at the '''until''' loop next.
  
 
|-
 
|-
| Slide 8
+
|Slide 6
  
'''Operator Precedence'''
+
“until” loop
| This slide lists all operators from highest precedence to lowest.
+
|The syntax for the '''until''' loop in '''Ruby '''is -
  
'''{ } ( )''' -- 1st Priority
 
  
'''<nowiki>* </nowiki>/ %''' -- 2nd priority
+
'''until “boolean expression”'''
  
'''+ -''' -- 3rd Priority
+
'''ruby code'''
  
'''<nowiki>< </nowiki><nowiki><= </nowiki>> >=''' -- 4th Priority
+
'''end'''
  
'''<nowiki>= </nowiki><nowiki>= = </nowiki>!=''' -- 5th Prior and so on
+
Let us look at an example.
  
 
|-
 
|-
| Slide 9
+
|Switch to gedit where you have already opened the
  
Example of Operator Precedence
+
file “until-loop.rb” with the '''loop '''code typed inside.
 +
|Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
  
| For example -  
+
And name it '''until-loop.rb'''
 +
|-
 +
|
 +
|I have a working example of the '''until '''loop.
  
'''3 + 4 * 5'''  &nbsp;  &nbsp;  &nbsp;  &nbsp; returns '''23''' and not '''35'''
+
You can pause the tutorial, and type the code as we go through it.
  
The multiplication operator ('''<nowiki>*</nowiki>''') has higher precedence than the addition operator ('''+''')
+
|-
 +
|Highlight “until”
 +
|I have declared an '''until '''loop in this example.
  
and thus will be evaluated first.
+
|-
 +
|Highlight “i = 0”
 +
|We had declared a local variable '''i '''and initialized it to '''0'''.
  
 
|-
 
|-
| Highlight 4*5
+
|<nowiki>Continue the highlight “until i < -10” </nowiki>
 +
|Then we declare an '''until''' loop.
  
Highlight 3+
+
|-
| Hence four fives are twenty and then three is added to 20 to give the output as '''23'''
+
|
 +
| This loop will execute as long as the variable '''i '''is greater than '''-10'''.
  
 
|-
 
|-
|  
+
|Highlight “puts”
| Lets us see some more examples based on operator precedence.
+
|The '''puts''' method will display the output.
  
 
|-
 
|-
| Switch back to the terminal.
+
|Highlight “i -= 1”
 +
|After the output is displayed, value of '''i''' is decremented by 1.
  
Press '''Crtl+l'''
+
|-
| Let's go back to the terminal.
+
|
 +
|'''i''' will adopt this decremented value before the next iteration.
  
Press''' Crtl and l''' keys simultaneously to clear the '''irb''' console.
+
|-
 +
|
 +
| The variable '''i '''gets decremented during every iteration.
  
 
|-
 
|-
| Type
+
|
 +
|This goes on till '''i '''reaches the value '''-11'''.
  
'''7-2*3'''<nowiki><< Press </nowiki>Enter
+
|-
| Now type '''7 minus 2 multiply by 3 '''
+
|
 +
|At this point the '''until condition '''fails.
  
and press '''Enter'''
+
|-
 +
|
 +
| Subsequently, it breaks out of the loop and stops printing the output.
  
 
|-
 
|-
| Highlight '''1'''
+
|
| We get the answer as '''1.'''
+
|Now switch to the '''terminal '''and type
  
Here the '''asterisk''' symbol has higher '''priority''' than the '''minus''' sign.
+
'''ruby until-loop.rb '''and see the output.
  
So the '''multiplication''' opertion is performed first and
+
|-
 +
|Highlight the output:
  
then '''subtraction''' is performed.
+
The number: 0 gets printed out
  
|-
+
The number: -1 gets printed out
|
+
| Lets us see an another example.
+
  
|-
+
The number: -2 gets printed out
| Type
+
  
'''(10+2)/4'''<nowiki><<Press</nowiki> Enter
+
The number: -3 gets printed out
  
| Type
+
The number: -4 gets printed out
  
'''Within brackets 10 plus 2 slash 4'''
+
The number: -5 gets printed out
  
Press '''Enter'''
+
The number: -6 gets printed out
  
|-
+
The number: -7 gets printed out
| Highlight '''3'''
+
| We get the answer as '''3.'''
+
  
|-
+
The number: -8 gets printed out
|
+
| In this case '''() - bracket''' has the higher '''priority''' than '''/- division'''.
+
  
So the operation inside the bracket that is '''addition''' is performed first.
+
The number: -9 gets printed out
  
Then '''division''' is performed.
+
The number: -10 gets printed out
 +
|The output will consist of a list of numbers '''0''' through '''-10'''.
 +
 
 +
<nowiki><Pause></nowiki>
  
 
|-
 
|-
|  
+
|
| <nowiki><<Pause>></nowiki>
+
|You should now be able to write your own '''until''' loop in '''Ruby'''.
  
Now, let us learn about Relational Operators.
+
Let's now move on to the '''redo''' construct.
  
 
|-
 
|-
|  
+
|Slide 7
| Let's switch back to slides.
+
  
|-
+
“'''redo'''”
| Slide 10
+
|The syntax for '''redo''' in '''Ruby '''is as follows:
  
Relational Operator
+
'''(a collection of objects).each do |item|'''
| Relational operators are also known as '''comparison''' operators.
+
  
Expressions using relational operators return '''boolean''' values.
+
'''a conditional statement on an item'''
  
|-
+
'''ruby code'''
| Slide 11
+
  
Relational Operator
+
''' redo'''
| Relation Operators in '''Ruby''' are
+
  
* '''<nowiki>==</nowiki>''' Equals to &nbsp;  &nbsp;  &nbsp;  &nbsp;Eg. a==b
+
'''end'''
* .eql? Equals to &nbsp;  &nbsp;  &nbsp;  &nbsp;Eg. a.eql?b
+
* '''!= '''Not equals to &nbsp;  &nbsp;  &nbsp;  &nbsp;Eg. a!=b
+
* '''<nowiki>< </nowiki>'''<nowiki>Less than &nbsp;  &nbsp;  &nbsp;  &nbsp;Eg. a<b</nowiki>
+
* '''>''' Greater than &nbsp;  &nbsp;  &nbsp;  &nbsp;Eg. a>b
+
* '''<nowiki><=</nowiki>''' <nowiki>Less than or equal to &nbsp;  &nbsp;  &nbsp;  &nbsp;Eg. a<=b</nowiki>
+
* '''>=''' Greater than or equal to &nbsp;  &nbsp;  &nbsp;  &nbsp; Eg. a>=b
+
* '''<nowiki><=> </nowiki>'''<nowiki>Combined comparison Eg. a<=>b</nowiki>
+
  
|-
+
'''end'''
|
+
 
| Now let us try some of these operators.
+
Let us look at an example.
  
 
|-
 
|-
| Switch back to the terminal
+
|Switch to gedit where you have already opened the  
  
press ctrl+l
+
file “redo-loop.rb” with the '''loop '''code typed inside.
| Go to the terminal.
+
|Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials, and name it '''redo-loop.rb'''
 
+
Press '''ctrl''' and '''L''' keys simultaneously to clear the '''irb''' console.
+
  
 
|-
 
|-
| Type
+
|
 +
|I have a working example of the '''redo '''loop.
  
'''<nowiki>10 == 10 << </nowiki>'''Press''' '''Enter
+
You can pause the tutorial, and type the code as we go through it.
| Lets us try '''equals to''' operator.
+
  
So type
+
|-
 +
|Highlight “'''each'''”
 +
|I have declared an '''each '''loop in this example.
  
'''10 equals equals 10'''
+
|-
 +
|Highlight “(10..20).each do |i|”
 +
|We have declared an '''each''' loop to iterate through numbers 10 to 20.
  
Press '''Enter'''
+
|-
 +
|Continue the highlight “if i == 20”
 +
|Then, we define an '''if '''conditional statement.
  
 
|-
 
|-
| Highlight''' true'''
+
|Continue the highlight
| We get the output as '''true.'''
+
|This loop will execute for every number between '''10 '''to '''20'''.
  
 
|-
 
|-
|  
+
|Continue the highlight
| '''.eql?''' opeartor is same as '''equals to''' operator.
+
|It will enter the inner '''if '''conditional block only if the value of '''i''' is equal to '''20'''.
  
Lets try it out
+
|-
 +
|Highlight “puts”
 +
|The '''puts''' method declared within the '''each''' loop displays the output.
  
 
|-
 
|-
| Type
+
|Continue the highlight “if i == 20”
 +
|Once the program enters the '''if '''conditional block, it will first print the output.
  
'''<nowiki>10 .eql?10 << </nowiki>'''Press''' '''Enter
+
Then it will execute '''redo'''.
| Now type
+
  
'''10 .eql?10'''
+
|-
 +
|
 +
|'''redo''' will execute the iteration of the most internal loop.
 +
 
 +
|-
 +
|
 +
|It will do so without checking the loop condition.
  
Press '''Enter'''
+
Our condition being '''if i == 20'''.
  
 
|-
 
|-
| Highlight '''true'''
+
|
| We get the output as '''true'''
+
|The result will be an infinite loop, since the value of '''i''' will not change from '''20'''.
  
 
|-
 
|-
| '''<nowiki>10 != 10<<</nowiki>'''Press''' '''Enter
+
|
| Now lets try '''not equal to '''operator.
+
|Let's switch to the '''terminal '''and type
  
Type
+
'''ruby redo-loop.rb'''
  
'''10 not equals 10'''
 
  
Press '''Enter'''
+
and see the output.
  
 
|-
 
|-
| Highlight''' false '''
+
|Highlight the output:
| We get the output as '''false.'''
+
  
This is because the two numbers are equal.
 
  
|-
+
>ruby redo-loop.rb
| Press ctrl+l
+
| Clear the '''irb''' console by pressing '''Ctrl '''and '''L''' simultaneously.
+
  
|-
+
The value has reached it's limit 20
|
+
| Let us now try''' less than '''operator.
+
  
|-
+
The value has reached it's limit 20
| '''Type'''
+
  
'''<nowiki>10 < 5 </nowiki>'''
+
The value has reached it's limit 20
| Type
+
  
'''10 less than 5'''
+
The value has reached it's limit 20
  
|-
+
The value has reached it's limit 20
|
+
| Here if first operand is less than second then it will return '''true'''
+
  
otherwise it will return '''false'''
+
The value has reached it's limit 20
  
Press '''Enter'''
+
The value has reached it's limit 20
  
|-
+
The value has reached it's limit 20
| Highlight '''False'''
+
| We get the output as '''false '''because '''10''' is not less than '''5'''
+
  
|-
+
The value has reached it's limit 20
|
+
| We will now try '''greater than '''operator
+
  
|-
+
The value has reached it's limit 20
| Type
+
  
'''5 > 2'''
+
The value has reached it's limit 20
| Type
+
  
'''5 greater than 2 '''
+
..................................................
 +
|The output will consist of an infinite loop that never ends.
  
|-
 
|
 
| Here if first operand is greater than second then it will return '''true'''
 
  
otherwise it will return '''false'''
+
Press Ctrl + c to terminate the infinite loop
  
|-
+
<nowiki><Pause></nowiki>
|
+
| Press '''Enter'''
+
  
 
|-
 
|-
| Highlight '''True'''
+
|
| In this case, we get the output as '''True '''because '''5''' is indeed greater than '''2'''
+
|Next, let us look at the '''break''' statement.
  
 
|-
 
|-
| Press ctrl+l
+
|Slide 8
| Clear the '''irb '''console by pressing '''Ctrl '''and '''L''' simultaneously
+
  
|-
+
“'''break'''” statement
|  
+
|The syntax for the '''break''' statement in '''Ruby '''is -
| We will now try the''' less than equal to '''operator
+
  
|-
+
'''a looping statement'''
| Type
+
  
'''<nowiki>12 <= 12 </nowiki>'''<nowiki><< Press </nowiki>Enter
+
'''a conditional statement'''
| Type
+
  
'''12 less than equal to 12'''
+
'''break'''
  
Press '''Enter'''
+
'''end conditional'''
  
|-
+
'''end loop'''
|
+
| Here if first operand is less than or equal to second then it returns '''true'''
+
  
otherwise it returns '''false'''
 
  
|-
+
Let us look at an example.
|
+
| Press '''Enter'''
+
  
 
|-
 
|-
| Highlight '''True'''
+
|Switch to gedit where you have already opened the
| We get the output as '''True '''because''' 12''' is equal to''' 12'''
+
  
|-
+
file “break-loop.rb” with the '''loop '''code typed inside.
|  
+
|Create a new file in '''gedit '''as shown in the basic level '''Ruby '''tutorials.
| You can try out the '''greater than or equal to''' operator likewise.
+
  
|-
+
 
|
+
And name it '''break-loop.rb'''
| Now let's try the '''combined comparision''' operator.
+
  
 
|-
 
|-
|  
+
|
| The '''combined comparision''' operator
+
|I have a working example of the '''break '''statement.
  
Returns '''0''' if first operand equals second
 
  
Returns '''1''' if first operand is greater than the second
+
You can pause the tutorial, and type the code as we go through this example.
  
and
+
|-
 +
|Highlight “'''each'''”
 +
|I have declared an '''each '''loop in this example.
  
Returns '''-1''' if first operand is less than the second operand
+
 
 +
It is similar to the one we used earlier.
  
 
|-
 
|-
| Type
+
|Highlight “puts”
 +
|The '''puts''' method here will display the output for numbers '''11''' to '''19'''.
  
'''<nowiki>3 <=> 3</nowiki>'''<nowiki> << Press </nowiki>'''Enter'''
+
|-
| Let's see how it works with an example
+
|hHighlight “if” and then "break"
 
+
|Once the value becomes '''20,''' the program enters the conditional '''if''' block.
Type
+
  
'''3 less than equals greater than 3'''
 
  
Press '''Enter'''
+
At this point, it will encounter the '''break''' statement and break out of the loop.
  
 
|-
 
|-
| Highlight '''0'''
+
|
| We get the output as '''0'''
+
|Now open the '''terminal '''and type
  
because both the operands are equal i.e. both are '''three'''
+
'''ruby break-loop.rb '''
 +
 
 +
 
 +
and see the output.
  
 
|-
 
|-
| Type
+
|Highlight the output:
  
'''<nowiki>4 <=> 3 << </nowiki>'''Press''' Enter'''
 
| Now, let's change one of the operands to '''4'''
 
  
Type
+
>ruby break-loop.rb
  
'''4 less than equals greater than 3'''
+
The value has reached it's limit 10
  
Press''' Enter'''
+
The value has reached it's limit 11
  
|-
+
The value has reached it's limit 12
| Highlight '''1'''
+
| We get the output as '''1 '''
+
  
Since''' 4''' is greater than '''3'''
+
The value has reached it's limit 13
  
|-
+
The value has reached it's limit 14
| Type
+
  
'''4<nowiki> <=> 7 << </nowiki>'''Press''' Enter'''
+
The value has reached it's limit 15
| Now, let's change this example again
+
  
Type
+
The value has reached it's limit 16
  
'''4 less than equals greater than 7'''
+
The value has reached it's limit 17
  
Press''' Enter'''
+
The value has reached it's limit 18
  
|-
+
The value has reached it's limit 19
| Highlight '''-1'''
+
|The output will consist of numbers '''10''' through '''19'''.
| We get the output as '''-1'''
+
  
Since''' 4''' is less than '''7'''
+
<nowiki><Pause></nowiki>
 
+
<nowiki><pause></nowiki>
+
  
 
|-
 
|-
| Slide11
+
|
 +
|Now you should be able to create your own '''break '''construct.
  
Assignment
+
<nowiki><Pause></nowiki>
| As an assignment
+
  
Solve the following examples using '''irb''' and check the output
+
|-
 +
|Slide 9
 +
|<nowiki><<Pause>></nowiki>
  
*'''10 + (2 * 5) – (8 / 2) = ?'''
+
This brings us to the end of this Spoken Tutorial.
*'''4*5/2+7=?'''
+
*Also, try arithmetic operators using methods
+
  
 
|-
 
|-
|  
+
|Slide 10
| <nowiki><<Pause>></nowiki>
+
  
This brings us to the end of this Spoken Tutorial.
+
Summary
 +
|Let's summarize.
 +
 
 +
In this tutorial we have learnt to use
  
Let's summarize
+
* '''while''' loop
 +
* '''until''' construct
 +
* '''redo'''
 +
* '''break''' construct
  
 
|-
 
|-
| Slide 12
+
|Slide 11
  
Summary
+
Assignment
| In this tutorial we have learnt about
+
|As as assignment
  
* Arithmetic Operators + - * /
+
Write a '''Ruby '''program using
* Operator Precedence
+
 
* Relational Operators
+
* (I have yet to come up with an assignment)
* using many examples
+
  
 
|-
 
|-
| Slide 13
+
|Slide 12
  
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
| Watch the video available at the following link.
+
|Watch the video available at the following link.
  
It summarises the Spoken Tutorial project.
+
It summarizes the Spoken Tutorial project.
  
 
If you do not have good bandwidth, you can download and watch it.  
 
If you do not have good bandwidth, you can download and watch it.  
  
 
|-
 
|-
| Slide 14
+
|Slide 13
| The Spoken Tutorial Project Team :
+
|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
+
  
 +
*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
 
contact at spoken hyphen tutorial dot org
  
 
|-
 
|-
| Slide 15
+
|Slide 14
  
Acknowledgement
+
Acknowledgements
| 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 602: Line 569:
  
 
|-
 
|-
| Slide 16
+
|Previous slide
 
+
|This is Anjana Nair signing off. Thank you
About the contributor
+
 
+
| This script has been contributed by the Spoken Tutorial Team, IIT Bombay
+
 
+
And this is Anjana Nair signing off
+
 
+
Thank you
+
  
 
|}
 
|}

Revision as of 19:09, 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.
hHighlight “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

  • (I have yet to come up with an assignment)
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

Nancyvarkey