Difference between revisions of "Ruby/C2/Logical-and-other-Operators/English"
Nancyvarkey (Talk | contribs) (Created page with ''''Title of script''':''' Logical & Other Operators ''' '''Author:Spoken Tutorial Team, IIT Bombay''' '''Keywords:logical operator, range operator, video tutorial''' {| borde…') |
|||
(7 intermediate revisions by 3 users not shown) | |||
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 12: | Line 14: | ||
|- | |- | ||
| Slide 1 | | Slide 1 | ||
− | | Welcome to this spoken tutorial on Logical & Other Operators | + | | Welcome to this spoken tutorial on '''Logical & Other Operators'''. |
|- | |- | ||
Line 21: | Line 23: | ||
* Logical Operators | * Logical Operators | ||
− | * Parallel assignment | + | * Parallel assignment and |
* Range Operators | * Range Operators | ||
Line 47: | Line 49: | ||
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. | ||
Logical Operators are, | Logical Operators are, | ||
− | * '''&& (and)''' | + | * '''&& that is (and)''' |
− | * '''|| (or)''' | + | * '''|| that is (or)''' |
− | * '''! (not)''' | + | * '''! that is (not)''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| Highlight''' “&&” '''and''' “and”''' | | 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'''<Expression2>''' and then highlight '''<Expression1>''' | ||
+ | | Second expression is evaluated only if the first is '''true'''. | ||
|- | |- | ||
Line 78: | Line 75: | ||
| Difference in the two forms is, '''precedence''' | | Difference in the two forms is, '''precedence''' | ||
− | Symbolic '''and (&&) | + | Symbolic '''and that is(&&) '''has higher '''precedence'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| | | | ||
− | | | + | | Let's us see some examples now. |
We will use '''irb '''for this. | We will use '''irb '''for this. | ||
Line 147: | Line 101: | ||
| Type | | Type | ||
− | '''3 greater than 2 space | + | '''3 greater than 2 space double ampersand space 4 less than 5''' |
Press''' Enter''' | Press''' Enter''' | ||
Line 168: | Line 122: | ||
| 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''' | ||
+ | | And '''replace the double '''ampersand '''symbol''' '''with the word '''and.''' | ||
+ | |||
+ | Press''' Enter''' | ||
+ | |||
+ | |- | ||
+ | | | ||
+ | |We get the same result. | ||
|- | |- | ||
Line 175: | Line 141: | ||
press '''Enter''' | 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''' | Press''' Enter''' | ||
Line 187: | Line 151: | ||
|- | |- | ||
| Highlight '''<nowiki>3<2</nowiki>''' | | Highlight '''<nowiki>3<2</nowiki>''' | ||
− | | | + | | 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'''. | ||
+ | |||
+ | |||
+ | |- | ||
+ | |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'''i.e ('''<nowiki>"||"</nowiki>''') has higher '''precedence'''. | ||
|- | |- | ||
| | | | ||
− | | Now, let's try | + | | Now, let's try some examples. |
|- | |- | ||
Line 227: | Line 207: | ||
press '''Enter''' | press '''Enter''' | ||
− | | In '''expression 1 '''replace '''greater than''' sign with '''less than.''' | + | | In '''expression 1 '''replace '''greater than''' sign with '''less than sign.''' |
− | And replace '''pipe '''symbol with word '''or.''' | + | And replace '''pipe '''symbol with the word '''or.''' |
Press '''Enter.''' | Press '''Enter.''' | ||
Line 243: | Line 223: | ||
Since both the expressions are '''false,''' we get output as '''false'''. | Since both the expressions are '''false,''' we get output as '''false'''. | ||
+ | |||
+ | |||
+ | |- | ||
+ | | Highlight '''<nowiki>!</nowiki>''' and '''not''' | ||
+ | | <nowiki>!</nowiki>''' ''' and ''' not ''' operator 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 that is <nowiki>(!)</nowiki>''' has higher '''precedence'''. | ||
|- | |- | ||
Line 287: | Line 282: | ||
|- | |- | ||
− | | Press''' ctrl + | + | | Press''' ctrl +L''' |
− | | Press '''Ctrl+ | + | | Press '''Ctrl+L '''simultaneously to clear the''' irb''' console. |
|- | |- | ||
Line 297: | Line 292: | ||
|- | |- | ||
− | | | + | | 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'''. | ||
|- | |- | ||
| | | | ||
− | | | + | | Let's switch to the '''terminal.''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 326: | Line 320: | ||
press '''Enter''' | press '''Enter''' | ||
− | | Here , | + | | Here, |
'''10''' will be assigned to variable '''a''' | '''10''' will be assigned to variable '''a''' | ||
Line 333: | Line 327: | ||
'''30''' will be assigned to variable '''c''' | '''30''' will be assigned to variable '''c''' | ||
− | |||
− | |||
|- | |- | ||
Line 344: | Line 336: | ||
| If we list multiple variables on the left hand side, | | If we list multiple variables on the left hand side, | ||
− | then the array is unpacked and assigned into the respective variables. | + | then the array is unpacked |
+ | |||
+ | |||
+ | and assigned into the respective variables. | ||
|- | |- | ||
Line 350: | Line 345: | ||
| We will learn about '''arrays '''in detail in the upcoming tutorials. | | We will learn about '''arrays '''in detail in the upcoming tutorials. | ||
− | + | For now, let's check whether the '''assignment''' is done properly. | |
|- | |- | ||
Line 365: | Line 360: | ||
|- | |- | ||
− | | Highlight | + | | Highlight '''20 ''' |
− | | | + | | We get 20 |
|- | |- | ||
Line 374: | Line 369: | ||
|- | |- | ||
| Highlight '''30 ''' | | Highlight '''30 ''' | ||
− | | | + | | '''30''' is displayed. |
− | + | ||
|- | |- | ||
| | | | ||
Line 383: | Line 377: | ||
| | | | ||
| Let us swap the values of variables '''a''' and '''b.''' | | Let us swap the values of variables '''a''' and '''b.''' | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 394: | Line 384: | ||
| Type | | 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 ''' | + | '''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.''' | Press '''Enter.''' | ||
Line 410: | Line 400: | ||
|- | |- | ||
| | | | ||
− | | | + | | Now let's swap '''a''' and '''b.''' |
|- | |- | ||
Line 437: | Line 427: | ||
|- | |- | ||
− | | Slide | + | | Slide '''Range''' |
− | + | ||
− | '''Range''' | + | |
| We will now learn about '''range''' in '''Ruby.''' | | We will now learn about '''range''' in '''Ruby.''' | ||
Line 447: | Line 435: | ||
|- | |- | ||
− | | 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. | ||
It consists of an start value, range of values and an end value. | It consists of an start value, range of values and an end value. | ||
− | '''(..) '''two dot operator creates | + | '''(..) '''two dot operator creates '''inclusive range.''' |
'''(...) '''three dot operator creates an '''exclusive range.''' | '''(...) '''three dot operator creates an '''exclusive range.''' | ||
|- | |- | ||
− | | 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''' ( | + | We do this using''' (<nowiki>===</nowiki>) '''the '''equality''' operator. |
|- | |- | ||
Line 468: | Line 456: | ||
Let's switch to '''terminal.''' | Let's switch to '''terminal.''' | ||
− | |||
− | |||
|- | |- | ||
| Type '''(1..10).to_a''' | | Type '''(1..10).to_a''' | ||
− | | | + | | Type |
'''Within brackets 1 two dots 10 then dot to underscore a''' | '''Within brackets 1 two dots 10 then dot to underscore a''' | ||
|- | |- | ||
− | | Highlight ''' | + | | Highlight '''..''' |
| '''Two dot''' operator creates '''inclusive range. ''' | | '''Two dot''' operator creates '''inclusive range. ''' | ||
− | '''Inclusive operator''' includes both begin and end values in a range. | + | |- |
+ | | Highlight '''1''' and '''10''' | ||
+ | | '''Inclusive operator''' includes both begin and end values in a range. | ||
− | Here '''to_a '''method is used to convert a '''range''' to a list. | + | |- |
+ | | Highlight '''to_a''' | ||
+ | | Here '''to_a '''method is used to convert a '''range''' to a list. | ||
Press '''Enter.''' | Press '''Enter.''' | ||
Line 494: | Line 484: | ||
|- | |- | ||
| | | | ||
− | | Now we will see | + | | Now we will see an '''exclusive range''' operator. |
|- | |- | ||
Line 503: | Line 493: | ||
|- | |- | ||
− | | Highlight '''(1...10). | + | | Highlight '''(1...10).to_a''' |
| '''Three dot '''operator creates an '''exclusive range. ''' | | '''Three dot '''operator creates an '''exclusive range. ''' | ||
Line 512: | Line 502: | ||
|- | |- | ||
| Highlight '''<nowiki>[1 ,2, 3, 4, 5, 6, 7, 8, 9]</nowiki>''' | | Highlight '''<nowiki>[1 ,2, 3, 4, 5, 6, 7, 8, 9]</nowiki>''' | ||
− | | Here end value '''10''' is not included in the range. | + | | Here the end value '''10''' is not included in the range. |
|- | |- | ||
| | | | ||
− | | Now | + | | Now let's check whether '''5''' lies in the range of '''1''' to '''10'''. |
|- | |- | ||
Line 535: | Line 525: | ||
|- | |- | ||
| Highlight''' true''' | | Highlight''' true''' | ||
− | | We get the output as '''true | + | | We get the output as '''true''' since '''5 '''lies in the range '''1 '''to '''10. ''' |
|- | |- | ||
Line 547: | Line 537: | ||
Summary | Summary | ||
− | | In this tutorial we have learnt | + | | In this tutorial, we have learnt |
* Logical operator<br/> Ex: '''&&,||, !''' | * Logical operator<br/> Ex: '''&&,||, !''' | ||
* Parallel assignment<br/> Ex: a,b,c=10,20,30 | * Parallel assignment<br/> Ex: a,b,c=10,20,30 | ||
− | * Range Operator<br/> Inclusive operator '''(..)'''<br/> Exclusive operator'''(...)''' | + | * Range Operator<br/> Inclusive operator '''(..) and '''<br/> Exclusive operator'''(...)''' |
|- | |- | ||
Line 571: | Line 561: | ||
− | It | + | It summaries 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. | ||
Line 597: | Line 587: | ||
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 | + | More information on this Mission is available at |
spoken-tutorial.org/NMEICT-Intro. | 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. | Thank you. | ||
|} | |} |
Latest revision as of 00:33, 29 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
|
Slide 3
System Requirement |
Here we are using
|
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,
|
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.
|
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
|
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
|
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.
|
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
|
Slide 13
Assignment |
As an assignment
using irb |
Slide 14
About the Spoken Tutorial Project |
Watch the video available at the following link.
If you do not have good bandwidth, you can download and watch it. |
Slide 15 | The Spoken Tutorial Project Team :
Gives certificates to those who pass an online test For more details, please write to contact@spoken-tutorial.org |
Slide 16
|
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. |