Difference between revisions of "Ruby/C2/Logical-and-other-Operators/English"
Nancyvarkey (Talk | contribs) |
|||
(4 intermediate revisions by 2 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 | + | |
+ | '''Video: Shalini Nair, SNDT, Summer Intern 2013''' | ||
'''Keywords:logical operator, range operator, video tutorial''' | '''Keywords:logical operator, range operator, video tutorial''' | ||
Line 13: | 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 22: | Line 23: | ||
* Logical Operators | * Logical Operators | ||
− | * Parallel assignment | + | * Parallel assignment and |
* Range Operators | * Range Operators | ||
Line 56: | Line 57: | ||
Logical Operators are, | Logical Operators are, | ||
− | * '''&& (and)''' | + | * '''&& that is (and)''' |
− | * '''|| (or)''' | + | * '''|| that is (or)''' |
− | * '''! (not)''' | + | * '''! that is (not)''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 83: | Line 75: | ||
| Difference in the two forms is, '''precedence''' | | Difference in the two forms is, '''precedence''' | ||
− | Symbolic '''and (&&) '''has higher '''precedence'''. | + | Symbolic '''and that is(&&) '''has higher '''precedence'''. |
|- | |- | ||
Line 109: | 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 135: | Line 127: | ||
press '''Enter''' | press '''Enter''' | ||
− | | | + | | And '''replace the double '''ampersand '''symbol''' '''with the word '''and.''' |
Press''' Enter''' | Press''' Enter''' | ||
Line 149: | 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 169: | Line 161: | ||
|So, we get output as '''false'''. | |So, we get output as '''false'''. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 189: | Line 171: | ||
| Difference in the two forms is '''precedence'''. | | Difference in the two forms is '''precedence'''. | ||
− | Symbolic '''or''' ('''<nowiki>"||"</nowiki>''') has higher '''precedence'''. | + | Symbolic '''or'''i.e ('''<nowiki>"||"</nowiki>''') has higher '''precedence'''. |
|- | |- | ||
Line 225: | 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 242: | Line 224: | ||
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''' | | 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. | If the expression is '''true''', '''<nowiki>!</nowiki>''' operator will return a '''false''' value. | ||
Line 264: | Line 237: | ||
| Difference in the two forms is '''precedence'''. | | Difference in the two forms is '''precedence'''. | ||
− | Symbolic '''not <nowiki>(!)</nowiki>''' has higher '''precedence'''. | + | Symbolic '''not that is <nowiki>(!)</nowiki>''' has higher '''precedence'''. |
|- | |- | ||
Line 309: | Line 282: | ||
|- | |- | ||
− | | Press''' ctrl + | + | | Press''' ctrl +L''' |
− | | Press '''Ctrl+ | + | | Press '''Ctrl+L '''simultaneously to clear the''' irb''' console. |
|- | |- | ||
Line 363: | 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 408: | 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 464: | Line 440: | ||
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.''' | ||
Line 526: | 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. |
|- | |- | ||
Line 561: | 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 585: | 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. |
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. |