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

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Logical & Other Operators

Author:Spoken Tutorial Team, IIT Bombay

Video: Shalini Nair, SNDT, Summer Intern 2013

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 and
  • 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,

  • && that is (and)
  • || that is (or)
  • ! that is (not)


Highlight “&&” and “and” && and and evaluate to true only if both the expressions are true.
Highlight<Expression2> and then highlight <Expression1> Second expression is evaluated only if the first is true.
Highlight “&&” Difference in the two forms is, precedence

Symbolic and that is(&&) has higher precedence.

Let's 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 double 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 && with and

press Enter

And replace the double ampersand symbol with the word and.

Press Enter

We get the same result.
Replace

3<2 and 4<5

press Enter

Now press the up arrow key to get the previous command. In expression 1 replace greater than sign with less than

Press Enter

Highlight false We get the output as false.
Highlight 3<2 This is because 3<2 is false.
Point to 3<2 and then to 4<5 Since the first expression is false, the second expression will not be evaluated.
Highlight false So, we get output as false.


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

Second expression is evaluated only if first is false.

Highlight || Difference in the two forms is precedence.

Symbolic ori.e ("||") has higher precedence.

Now, let's try some examples.
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 sign.

And replace pipe symbol with the 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.


Highlight ! and not ! and not operator 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 that is (!) has higher precedence.

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.

Slide 9 Multiple variables can be initialized with a single line of Ruby code, through parallel assignment.
Let's switch 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

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.

For 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 We get 20
Type c << press Enter Type c and press Enter.
Highlight 30 30 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.
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

Now let's 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.

Slide 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 inclusive range.

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

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

Type (1..10).to_a Type

Within brackets 1 two dots 10 then dot to underscore a

Highlight .. Two dot operator creates inclusive range.
Highlight 1 and 10 Inclusive operator includes both begin and end values in a range.


Highlight to_a 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 an 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 the end value 10 is not included in the range.
Now let's 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 (..) and
    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 summaries 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

spoken-tutorial.org/NMEICT-Intro.

Remain on the previous Slide This script has been contributed by the Spoken Tutorial Team, IIT Bombay.

And this is Shalini Nair from SNDT.

Thank you.

Contributors and Content Editors

Nancyvarkey, Sanmugam, Sneha