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

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

Latest revision as of 20:00, 18 August 2013

Title of script: Arithmetic & Relational Operators in Ruby

Author:Spoken Tutorial Team, IIT Bombay

Keywords: Arithmetic Operators, Relational Operators, video tutorial


Visual Cue Narration
Slide 1 Welcome to the Spoken Tutorial on Arithmetic & Relational Operators in Ruby.
Slide 2

Learning Objectives

In this tutorial we will learn about
  • Arithmetic Operators
  • Operator Precedence
  • Relational Operators
Slide 3

System Requirements

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

Pre-requisites

To follow this tutorial you must know how to use Terminal and Text editor in Linux.

You must also be familiar with irb

If not, for relevant tutorials, please visit our website

Slide 5

Arithmetic Operators

(need to change the slide)

Now let us learn about arithmetic operators.

Ruby has following arithmetic operators.

+ Addition: eg. a+b.

- Subtraction: eg. a-b.

/ Division: eg. a/b.

* Multiplication: eg. a*b.

% Modulus: eg. a%b.

** Exponent : eg a**b

Let us try these arithmetic operators using irb.
Switch to the terminal

Press Ctrl+alt+t

Open the terminal by pressing Ctrl, Alt and T keys simultaneously.

A terminal window appears on your screen.

Type irb <<press enter Type irb

and press Enter to launch the interactive Ruby.

Type 10+ 20 <<press enter Type 10 plus 20

and press Enter.

Highlight 30 The addition operation is performed and the result 30 is displayed.
Similarly the subtraction and multiplication operations can be performed.
Let us try the division operator.
Type 10/4<<press Enter Type 10 slash 4

and press Enter.

Highlight 2 Here you can see the result is truncated to the nearest whole number which is 2.
To get a more accurate answer, we need to express one number as float
Type 10.0/4 <<press Enter Type 10.0 slash 4

and press Enter.

Highlight 2.5 Now we get the result as 2.5
Let's now try the modulus operator.

The modulus operator returns the remainder as output.

Type 12%5

press Enter

Type 12 percentage sign 5

and press Enter.

Highligtht 2 Here 12 is divided by 5 and the remainder 2 is returned back.
Now let's try the exponent operator.
Type 2**5<<press enter Type 2 followed by the asterisk symbol twice and then 5 and press Enter.
This means that 2 is raised to the power of 5.
Highlight 32 So we get the output as 32.
Next, let us learn about operator precedence.
Slide 6

What is Operator Precedence?

When several operations occur in a mathematical expression,
  • each part is evaluated
  • and resolved in a predetermined order
  • called operator precedence.
Slide 7

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.

Slide 8

Operator Precedence

This slide lists all operators from highest precedence to lowest.

{ } ( ) -- 1st Priority

* / % -- 2nd priority

+ - -- 3rd Priority

< <= > >= -- 4th Priority

= = = != -- 5th Prior and so on

Slide 9

Example of Operator Precedence

For example -

3 + 4 * 5         returns 23 and not 35

The multiplication operator (*) has higher precedence than the addition operator (+)

and thus will be evaluated first.

Highlight 4*5

Highlight 3+

Hence four fives are twenty and then three is added to 20 to give the output as 23
Lets us see some more examples based on operator precedence.
Switch back to the terminal.

Press Crtl+l

Let's go back to the terminal.

Press Crtl and l keys simultaneously to clear the irb console.

Type

7-2*3<< Press Enter

Now type 7 minus 2 multiply by 3

and press Enter

Highlight 1 We get the answer as 1.

Here the asterisk symbol has higher priority than the minus sign.

So the multiplication opertion is performed first and

then subtraction is performed.

Lets us see an another example.
Type

(10+2)/4<<Press Enter

Type

Within brackets 10 plus 2 slash 4

Press Enter

Highlight 3 We get the answer as 3.
In this case () - bracket has the higher priority than /- division.

So the operation inside the bracket that is addition is performed first.

Then division is performed.

<<Pause>>

Now, let us learn about Relational Operators.

Let's switch back to slides.
Slide 10

Relational Operator

Relational operators are also known as comparison operators.

Expressions using relational operators return boolean values.

Slide 11

Relational Operator

Relation Operators in Ruby are
  • == Equals to        Eg. a==b
  • .eql? Equals to        Eg. a.eql?b
  • != Not equals to        Eg. a!=b
  • < Less than        Eg. a<b
  • > Greater than        Eg. a>b
  • <= Less than or equal to        Eg. a<=b
  • >= Greater than or equal to         Eg. a>=b
  • <=> Combined comparison Eg. a<=>b
Now let us try some of these operators.
Switch back to the terminal

press ctrl+l

Go to the terminal.

Press ctrl and L keys simultaneously to clear the irb console.

Type

10 == 10 << Press Enter

Lets us try equals to operator.

So type

10 equals equals 10

Press Enter

Highlight true We get the output as true.
.eql? opeartor is same as equals to operator.

Lets try it out

Type

10 .eql?10 << Press Enter

Now type

10 .eql?10

Press Enter

Highlight true We get the output as true
10 != 10<<Press Enter Now lets try not equal to operator.

Type

10 not equals 10

Press Enter

Highlight false We get the output as false.

This is because the two numbers are equal.

Press ctrl+l Clear the irb console by pressing Ctrl and L simultaneously.
Let us now try less than operator.
Type

10 < 5

Type

10 less than 5

Here if first operand is less than second then it will return true

otherwise it will return false

Press Enter

Highlight False We get the output as false because 10 is not less than 5
We will now try greater than operator
Type

5 > 2

Type

5 greater than 2

Here if first operand is greater than second then it will return true

otherwise it will return false

Press Enter
Highlight True In this case, we get the output as True because 5 is indeed greater than 2
Press ctrl+l Clear the irb console by pressing Ctrl and L simultaneously
We will now try the less than equal to operator
Type

12 <= 12 << Press Enter

Type

12 less than equal to 12

Press Enter

Here if first operand is less than or equal to second then it returns true

otherwise it returns false

Press Enter
Highlight True We get the output as True because 12 is equal to 12
You can try out the greater than or equal to operator likewise.
Now let's try the combined comparision operator.
The combined comparision operator

Returns 0 if first operand equals second

Returns 1 if first operand is greater than the second

and

Returns -1 if first operand is less than the second operand

Type

3 <=> 3 << Press Enter

Let's see how it works with an example

Type

3 less than equals greater than 3

Press Enter

Highlight 0 We get the output as 0

because both the operands are equal i.e. both are three

Type

4 <=> 3 << Press Enter

Now, let's change one of the operands to 4

Type

4 less than equals greater than 3

Press Enter

Highlight 1 We get the output as 1

Since 4 is greater than 3

Type

4 <=> 7 << Press Enter

Now, let's change this example again

Type

4 less than equals greater than 7

Press Enter

Highlight -1 We get the output as -1

Since 4 is less than 7

<pause>

Slide11

Assignment

As an assignment

Solve the following examples using irb and check the output

  • 10 + (2 * 5) – (8 / 2) = ?
  • 4*5/2+7=?
  • Also, try arithmetic operators using methods
<<Pause>>

This brings us to the end of this Spoken Tutorial.

Let's summarize

Slide 12

Summary

In this tutorial we have learnt about
  • Arithmetic Operators + - * /
  • Operator Precedence
  • Relational Operators
  • using many examples
Slide 13

About the Spoken Tutorial Project

Watch the video available at the following link.

It summarises the Spoken Tutorial project.

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

Slide 14 The Spoken Tutorial Project Team :

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to

contact at spoken hyphen tutorial dot org

Slide 15

Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project.

It is supported by the National Mission on Education through ICT, MHRD, Government of India.

More information on this Mission is available at:

spoken hyphen tutorial dot org slash NMEICT hyphen Intro.

Slide 16

About the contributor

This script has been contributed by the Spoken Tutorial Team, IIT Bombay

And this is Anjana Nair signing off

Thank you

Contributors and Content Editors

Nancyvarkey