Difference between revisions of "Ruby/C2/Logical-and-other-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: Shalini Nair, SNDT, Summer Intern 2013
  
 
'''Keywords:logical operator, range operator, video tutorial'''
 
'''Keywords:logical operator, range operator, video tutorial'''
Line 47: Line 48:
  
 
Logical Operators
 
Logical Operators
|  Logical Operators are also known as '''Boolean Operators''' .
+
|  Logical Operators are also known as '''Boolean Operators'''  
  
Because they evaluate parts of an expression and return a '''true''' or  ''false''' value.  
+
because they evaluate parts of an expression  
 +
 
 +
and return a '''true''' or  ''false''' value.  
  
 
Logical Operators are,  
 
Logical Operators are,  
Line 70: Line 73:
 
|-
 
|-
 
| Highlight''' “&&” '''and''' “and”'''
 
| Highlight''' “&&” '''and''' “and”'''
|  '''and''' and '''&&''' evaluate to '''true '''only if both the expressions are '''true'''.
+
|  '''&&''' and '''and''' evaluate to '''true '''only if both the expressions are '''true'''.
  
Second expression is evaluated only if the first is '''true'''.
+
|-
 +
| Highlight'''<Expression2>''' and then highlight '''<Expression1>'''
 +
| Second expression is evaluated only if the first is '''true'''.
  
 
|-
 
|-
Line 79: Line 84:
  
 
Symbolic '''and (&&) '''has higher '''precedence'''.
 
Symbolic '''and (&&) '''has higher '''precedence'''.
 
|-
 
| Slide 7
 
 
|  '''<nowiki>||</nowiki>''' and '''or'''
 
 
<nowiki>Eg : <Expresion1></nowiki> '''||''' <nowiki><Expression2></nowiki>
 
 
<nowiki><Expresion1></nowiki> '''or''' <nowiki><Expression2></nowiki>
 
 
|-
 
|Highlight '''<nowiki>||</nowiki>''' and '''or'''
 
|'''<nowiki>||</nowiki>''' and '''or''' evaluate to '''true''', if either '''operand''' is '''true'''.
 
 
Second expression is evaluated only if first is '''false'''.
 
|-
 
| Highlight '''<nowiki>||</nowiki>'''
 
| Difference in the two forms is '''precedence'''.
 
 
Symbolic '''or''' ('''<nowiki>"||"</nowiki>''') has higher '''precedence'''.
 
 
|-
 
| Slide 8
 
 
|  '''<nowiki>!</nowiki>''' and '''not'''
 
 
Eg: '''<nowiki>!</nowiki>'''<Expression>
 
 
'''not'''<Expression>
 
 
|-
 
| Highlight '''<nowiki>!</nowiki>''' and '''not'''
 
|  '''not''' and '''<nowiki>!</nowiki>'''  return the opposite value of the expression
 
 
If the expression is '''true''', '''<nowiki>!</nowiki>''' operator will return a '''false''' value.
 
 
It will return '''true''' if the expression is '''false'''.
 
 
|-
 
| Highlight '''<nowiki>!</nowiki>'''
 
|  Difference in the two forms is '''precedence'''.
 
 
Symbolic '''not <nowiki>(!)</nowiki>''' has higher '''precedence'''.
 
  
 
|-
 
|-
Line 168: Line 130:
 
| Press the up Arrow key
 
| Press the up Arrow key
 
|  Now press '''Up''' '''Arrow''' key to get the previous command.
 
|  Now press '''Up''' '''Arrow''' key to get the previous command.
 +
 +
|-
 +
| Replace '''&&''' with '''and'''
 +
 +
press '''Enter'''
 +
|  In '''expression 1 '''replace the double '''ampersand '''symbol''' '''with the word '''and.'''
 +
 +
Press''' Enter'''
 +
 +
|-
 +
|
 +
|We get the same result.
  
 
|-
 
|-
Line 176: Line 150:
 
press '''Enter'''
 
press '''Enter'''
 
|  In '''expression 1 '''replace '''greater than''' sign with '''less than'''
 
|  In '''expression 1 '''replace '''greater than''' sign with '''less than'''
 
And replace the double '''ampersand '''symbol''' '''with the word '''and.'''
 
  
 
Press''' Enter'''
 
Press''' Enter'''
Line 187: Line 159:
 
|-
 
|-
 
| Highlight '''<nowiki>3<2</nowiki>'''
 
| Highlight '''<nowiki>3<2</nowiki>'''
| Here '''expression 1''' that is '''<nowiki>3<2 </nowiki>'''is '''false.'''
+
| This is because '''<nowiki>3<2 </nowiki>'''is '''false.'''
  
Since the first expression is '''false''', the second expression will not be evaluated.
+
|-
 +
|Point to 3<2 and then to 4<5
 +
|Since the first expression is '''false''', the second expression will not be evaluated.
  
So, we get output as '''false'''.  
+
|-
 +
|Highlight '''false'''
 +
|So, we get output as '''false'''.
 +
 
 +
|-
 +
| Slide 7
 +
 
 +
|  '''<nowiki>||</nowiki>''' and '''or'''
 +
 
 +
<nowiki>Eg : <Expresion1></nowiki> '''||''' <nowiki><Expression2></nowiki>
 +
 
 +
<nowiki><Expresion1></nowiki> '''or''' <nowiki><Expression2></nowiki>
 +
 
 +
|-
 +
|Highlight '''<nowiki>||</nowiki>''' and '''or'''
 +
|'''<nowiki>||</nowiki>''' and '''or''' evaluate to '''true''', if either '''expression''' is '''true'''.
 +
 
 +
Second expression is evaluated only if first is '''false'''.
 +
|-
 +
| Highlight '''<nowiki>||</nowiki>'''
 +
| Difference in the two forms is '''precedence'''.
 +
 
 +
Symbolic '''or''' ('''<nowiki>"||"</nowiki>''') has higher '''precedence'''.
  
 
|-
 
|-
 
|  
 
|  
|  Now, let's try the '''or''' operator.
+
|  Now, let's try some examples.
  
 
|-
 
|-
Line 243: Line 239:
  
 
Since both the expressions are '''false,''' we get output as '''false'''.
 
Since both the expressions are '''false,''' we get output as '''false'''.
 +
 +
|-
 +
| Slide 8
 +
 +
|  '''<nowiki>!</nowiki>''' and '''not'''
 +
 +
Eg: '''<nowiki>!</nowiki>'''<Expression>
 +
 +
'''not'''<Expression>
 +
 +
|-
 +
| Highlight '''<nowiki>!</nowiki>''' and '''not'''
 +
|  '''not''' and '''<nowiki>!</nowiki>'''  return the opposite value of the expression
 +
 +
If the expression is '''true''', '''<nowiki>!</nowiki>''' operator will return a '''false''' value.
 +
 +
It will return '''true''' if the expression is '''false'''.
 +
 +
|-
 +
| Highlight '''<nowiki>!</nowiki>'''
 +
|  Difference in the two forms is '''precedence'''.
 +
 +
Symbolic '''not <nowiki>(!)</nowiki>''' has higher '''precedence'''.
  
 
|-
 
|-
Line 297: Line 316:
  
 
|-
 
|-
|  
+
| Slide 9
 +
 
 
|  Multiple variables can be initialized with a single line of '''Ruby''' code, through '''parallel assignment'''.  
 
|  Multiple variables can be initialized with a single line of '''Ruby''' code, through '''parallel assignment'''.  
  
 
|-
 
|-
 
|  
 
|  
|  Lets us try this out using '''irb.'''
+
|  Lets switch to the '''terminal.'''
 
+
Go to the '''terminal.'''
+
  
 
|-
 
|-
Line 326: Line 344:
  
 
press '''Enter'''
 
press '''Enter'''
|  Here ,
+
|  Here,
  
 
'''10''' will be assigned to variable '''a'''
 
'''10''' will be assigned to variable '''a'''
Line 333: Line 351:
  
 
'''30''' will be assigned to variable '''c'''
 
'''30''' will be assigned to variable '''c'''
 
Press '''Enter.'''
 
  
 
|-
 
|-
Line 350: Line 366:
 
|  We will learn about '''arrays '''in detail in the upcoming tutorials.
 
|  We will learn about '''arrays '''in detail in the upcoming tutorials.
  
Now, let's check whether the '''assignment''' is done properly.
+
For now, let's check whether the '''assignment''' is done properly.
  
 
|-
 
|-
Line 365: Line 381:
  
 
|-
 
|-
| Highlight 2'''0 '''
+
| Highlight '''20 '''
| Value 2'''0''' stored in variable '''b''' is displayed.
+
| We get 20
  
 
|-
 
|-
Line 374: Line 390:
 
|-
 
|-
 
| Highlight '''30 '''
 
| Highlight '''30 '''
| Value '''30''' stored in variable '''c '''is displayed.
+
| '''30''' is displayed.
 
+
 
|-
 
|-
 
|  
 
|  
Line 383: Line 398:
 
|  
 
|  
 
|  Let us swap the values of variables '''a''' and '''b.'''
 
|  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.
 
  
 
|-
 
|-
Line 410: Line 421:
 
|-
 
|-
 
|  
 
|  
Lets swap '''a''' and '''b.'''
+
Now let's swap '''a''' and '''b.'''
  
 
|-
 
|-
Line 447: Line 458:
  
 
|-
 
|-
| Sequence Range
+
| Slide Sequence Range
 
|  '''Sequence range''' is used to create a range of successive values.  
 
|  '''Sequence range''' is used to create a range of successive values.  
  
Line 457: Line 468:
  
 
|-
 
|-
| Range Interval
+
| Slide Range Interval
 
|  Ranges are used to identify whether a value falls within a particular range, too.
 
|  Ranges are used to identify whether a value falls within a particular range, too.
  
  
We do this using''' (==<nowiki>=</nowiki>) '''the '''equality''' operator.
+
We do this using''' (<nowiki>===</nowiki>) '''the '''equality''' operator.
  
 
|-
 
|-
Line 468: Line 479:
  
 
Let's switch to '''terminal.'''
 
Let's switch to '''terminal.'''
 
Let's create a range between '''1''' to '''10.'''
 
  
 
|-
 
|-
Line 494: Line 503:
 
|-
 
|-
 
|  
 
|  
|  Now we will see the '''exclusive range''' operator.
+
|  Now we will see an '''exclusive range''' operator.
  
 
|-
 
|-
Line 516: Line 525:
 
|-
 
|-
 
|  
 
|  
|  Now lets check whether '''5''' lies in the range of '''1''' to '''10'''.
+
|  Now let's check whether '''5''' lies in the range of '''1''' to '''10'''.
  
 
|-
 
|-
Line 535: Line 544:
 
|-
 
|-
 
| Highlight''' true'''
 
| Highlight''' true'''
|  We get the output as '''true s'''ince '''5 '''lies in the range '''1 '''to '''10. '''
+
|  We get the output as '''true''' since '''5 '''lies in the range '''1 '''to '''10. '''
  
 
|-
 
|-
Line 597: Line 606:
 
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.
  
More information on this Mission is available at below link:
+
More information on this Mission is available at  
  
 
spoken-tutorial.org/NMEICT-Intro.
 
spoken-tutorial.org/NMEICT-Intro.
  
 
|-
 
|-
| About the contributor Slide
+
| Remain on the previous Slide
| 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 the video has been contributed by Ruby Software Pvt. Ltd
+
 
+
 
+
[http://www.ruby-software.com/ http://www.ruby-software.com]
+
  
 +
And this is Shalini Nair from SNDT.
  
 
Thank you.
 
Thank you.
  
 
|}
 
|}

Revision as of 20:31, 22 April 2013

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
  • 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.
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 (&&) 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 && with and

press Enter

In expression 1 replace the double ampersand symbol with the word and.

Press Enter

We get the same result.
Replace

3<2 and 4<5

press Enter

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

Eg : <Expresion1> || <Expression2>

<Expresion1> or <Expression2>

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 or ("||") 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.

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.

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.

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.
Lets 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 an 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 (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 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 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 (..)
    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

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