Ruby/C2/Logical-and-other-Operators/English

From Script | Spoken-Tutorial
Revision as of 23:00, 9 April 2013 by Nancyvarkey (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Logical & Other Operators

Author:Spoken Tutorial Team, IIT Bombay

Keywords:logical operator, range operator, video tutorial


Visual Cue Narration
Slide 1 Welcome to this spoken tutorial on Logical & Other Operators
Slide 2

Learning Objectives

In this tutorial, we will learn
  • Logical Operators
  • Parallel assignment
  • Range Operators
Slide 3

System Requirement

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

Logical Operators

Logical Operators are also known as Boolean Operators .

Because they evaluate parts of an expression and return a true' or false value.

Logical Operators are,

  • && (and)
  • || (or)
  • ! (not)
Slide 6

Logical operators

&& and and

Eg : <Expresion1> &&<Expression2>

<Expresion1> and<Expression2>

Highlight “&&” and “and” and and && evaluate to true only if both the expressions are true.

Second expression is evaluated only if the first is true.

Highlight “&&” Difference in the two forms is, precedence

Symbolic and (&&) has higher precedence.

Slide 7 || and or

Eg : <Expresion1> || <Expression2>

<Expresion1> or <Expression2>

Highlight || and or || and or evaluate to true, if either operand is true.

Second expression is evaluated only if first is false.

Highlight || Difference in the two forms is precedence.

Symbolic or ("||") has higher precedence.

Slide 8 ! and not

Eg: !<Expression>

not<Expression>

Highlight ! and not not and ! return the opposite value of the expression

If the expression is true, ! operator will return a false value.

It will return true if the expression is false.

Highlight ! Difference in the two forms is precedence.

Symbolic not (!) has higher precedence.

Lets us see some examples now.

We will use irb for this.

Press Ctrl+Alt+t to switch to the terminal Open the terminal by pressing Ctrl, Alt and T keys simultaneously.
Type

irb and press Enter

Type irb and press Enter to launch interactive Ruby
Type

3>2 && 4<5

Press Enter

Type

3 greater than 2 space ampersand ampersand space 4 less than 5

Press Enter

Highlight true We get the output as true.
Highlight 3>2

Highlight 4<5

Here, expression1 that is 3>2 is true.

Expression 2 that is 4<5 is also true.

Since both the expressions are true, we get output as true.

Press the up Arrow key Now press Up Arrow key to get the previous command.
Replace

3<2 and 4<5

press Enter

In expression 1 replace greater than sign with less than

And replace the double ampersand symbol with the word and.

Press Enter

Highlight false We get the output as false.
Highlight 3<2 Here expression 1 that is 3<2 is false.

Since the first expression is false, the second expression will not be evaluated.

So, we get output as false.

Now, let's try the or operator.
Type

10>6 || 12 <7

10 greater than 6 space double pipe space 12 less than 7

Press Enter.

Highlight true We get output as true.
Highlight 10<6 Here expression 1 that is 10<6 is true.

Since the first expression is true , second expression will not be evaluated.

So, we get the output as true.

Press the up Arrow key Now press the Up Arrow key to get the previous command.
Replace

10<6 or 12<7

press Enter

In expression 1 replace greater than sign with less than.

And replace pipe symbol with word or.

Press Enter.

Highlight 10<6

Highlight 12<7

Here, expression1 that is 10<6 is false.

Expression 2 that is 12<7 is also false.


Since both the expressions are false, we get output as false.

Let's try out the not operator.
Type

10==10

press Enter

First type

10 double equal to 10

Press Enter.

Highlight true We get the output as true.
To invert the result of above expression,

let's add the not operator before the expression.

Type

!(10==10)

press Enter

Type

Exclamation mark within brackets 10 double equal to 10

Press Enter.

Highlight false We get the output as false.
Press ctrl +l Press Ctrl+l simultaneously to clear the irb console.
<Pause>

Next, let us learn about parallel assignment.

Multiple variables can be initialized with a single line of Ruby code, through parallel assignment.
Lets us try this out using irb.

Go to the terminal.

Let's declare three variables a, b, c using parallel assignment.
Type

a,b,c =10,20,30

Type

a comma b comma c equal to 10 comma 20 comma 30

and press Enter.

Highlight a,b,c =10,20,30


press Enter

Here ,

10 will be assigned to variable a

20 will be assigned to variable b

30 will be assigned to variable c

Press Enter.

Highlight 10,20,30 The right hand side acts as an array.
Highlight a,b,c=10,20,30 If we list multiple variables on the left hand side,

then the array is unpacked and assigned into the respective variables.

We will learn about arrays in detail in the upcoming tutorials.

Now, let's check whether the assignment is done properly.

Type a << press Enter Type a and press Enter.
Highlight 10 Value 10 stored in variable a is displayed.
Type b<< press Enter Type b and press Enter.
Highlight 20 Value 20 stored in variable b is displayed.
Type c << press Enter Type c and press Enter.
Highlight 30 Value 30 stored in variable c is displayed.
Parallel assignment is also useful for swapping the values stored in two variables.
Let us swap the values of variables a and b.
First, let's us see what are the values of variables a and b before swapping.
Type puts “a=#{a}”,”b=#{b}”

press Enter

Type

puts space within double quotes a equal to hash within curly brackets a comma within double quotes b equal to hash within curly brackets b

Press Enter.

Highlight a=10

b=20

We get the output as

a=10

b=20

Lets swap a and b.
Type

a,b=b,a

To do so type

a comma b equal to b comma a

Press Enter.

Press Up Arrow key twice Press Up Arrow key twice to get the puts command and press Enter.
Highlight a=20

b=10

We get the output as

a=20

b=10

Slide

Range

We will now learn about range in Ruby.

The values in a range can be numbers, characters, strings or objects.

Ranges are used to express a sequence.

Sequence Range Sequence range is used to create a range of successive values.

It consists of an start value, range of values and an end value.

(..) two dot operator creates an inclusive range.

(...) three dot operator creates an exclusive range.

Range Interval Ranges are used to identify whether a value falls within a particular range, too.


We do this using (===) the equality operator.

Let us try out some examples on ranges.

Let's switch to terminal.

Let's create a range between 1 to 10.

Type (1..10).to_a Type

Within brackets 1 two dots 10 then dot to underscore a

Highlight (1..10).to _a Two dot operator creates inclusive range.

Inclusive operator includes both begin and end values in a range.


Here to_a method is used to convert a range to a list.

Press Enter.

Highlight [1 ,2, 3, 4, 5, 6, 7, 8, 9, 10] Here you can see the values 1 and 10 are included in the range.
Now we will see the exclusive range operator.
Type (1...10).to_a Type

Within brackets 1 three dots 10 then dot to underscore a

Highlight (1...10).to _a Three dot operator creates an exclusive range.

Exclusive range operator excludes the end value from the sequence.

Press Enter.

Highlight [1 ,2, 3, 4, 5, 6, 7, 8, 9] Here end value 10 is not included in the range.
Now lets check whether 5 lies in the range of 1 to 10.
Type

(1..10)===5

Type

Within brackets 1 two dots 10 three times equal to and then 5

Press Enter.

Highlight === Equality operator is used to check whether a value lies in the range.
Highlight true We get the output as true since 5 lies in the range 1 to 10.
<<Pause>>

This brings us to the end of this Spoken Tutorial.

Slide 11

Summary

In this tutorial we have learnt
  • Logical operator
    Ex: &&,||, !
  • Parallel assignment
    Ex: a,b,c=10,20,30
  • Range Operator
    Inclusive operator (..)
    Exclusive operator(...)
Slide 13

Assignment

As an assignment
  • Declare two variables using parallel assignment and
  • Check whether their sum lies between 20 and 50

using irb

Slide 14

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 15 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@spoken-tutorial.org

Slide 16


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 below link:

spoken-tutorial.org/NMEICT-Intro.

About the contributor Slide This script has been contributed by the Spoken Tutorial Team, IIT Bombay.


And the video has been contributed by Ruby Software Pvt. Ltd


http://www.ruby-software.com


Thank you.

Contributors and Content Editors

Nancyvarkey, Sanmugam, Sneha