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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 2: Line 2:
  
 
'''Author:Spoken Tutorial Team, IIT Bombay'''
 
'''Author:Spoken Tutorial Team, IIT Bombay'''
 
'''Video: Aditi Gokarn, SNDT Summer Intern, 2013'''
 
  
 
'''Keywords: Arithmetic Operators, Relational Operators, video tutorial'''
 
'''Keywords: Arithmetic Operators, Relational Operators, video tutorial'''
Line 610: Line 608:
 
| This script has been contributed by the Spoken Tutorial Team, IIT Bombay
 
| This script has been contributed by the Spoken Tutorial Team, IIT Bombay
  
And this is Aditi Gokarn from SNDT
+
And this is Anjana Nair signing off
  
 
Thank you
 
Thank you
  
 
|}
 
|}

Revision as of 13:28, 2 July 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