Difference between revisions of "Ruby/C2/Logical-and-other-Operators/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
(9 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | |||
− | |||
{| border=1 | {| border=1 | ||
− | || ''Time''' | + | || '''Time''' |
|| '''Narration''' | || '''Narration''' | ||
|- | |- | ||
− | | 00 | + | | 00:02 |
− | | Welcome to this | + | | Welcome to this '''Spoken Tutorial''' on '''Logical & Other Operators. ''' |
|- | |- | ||
− | | 00 | + | | 00:06 |
− | | In this tutorial we will learn | + | | In this tutorial, we will learn: |
|- | |- | ||
− | | 00 | + | | 00:09 |
| '''Logical Operators ''' | | '''Logical Operators ''' | ||
|- | |- | ||
− | | 00 | + | | 00:11 |
|'''Parallel assignment''' and | |'''Parallel assignment''' and | ||
|- | |- | ||
− | | 00 | + | | 00:13 |
− | | '''Range Operators ''' | + | | '''Range Operators '''. |
|- | |- | ||
− | | 00 | + | | 00:15 |
− | |Here we are using | + | |Here, we are using: |
|- | |- | ||
− | | 00 | + | | 00:17 |
− | | '''Ubuntu Linux''' version 12.04 | + | | '''Ubuntu Linux''' version '''12.04''' |
|- | |- | ||
− | | 00 | + | | 00:20 |
− | | | + | | ''' Ruby 1.9.3 ''' |
|- | |- | ||
− | | 00 | + | | 00:23 |
− | |To follow this tutorial you must know how to use ''' Terminal''' and '''Text editor''' in ''' Linux'''. | + | |To follow this tutorial, you must know how to use ''' Terminal''' and '''Text editor''' in '''Linux'''. |
|- | |- | ||
− | | 00 | + | | 00:29 |
− | |You must also be familiar with '''irb''' | + | |You must also be familiar with ''''irb''''. |
− | |||
|- | |- | ||
− | | 00 | + | | 00:33 |
− | | If not, for relevant tutorials, please visit our website | + | | If not, for relevant tutorials, please visit our website. |
− | + | ||
− | + | ||
|- | |- | ||
− | | 00 | + | | 00:38 |
− | |Logical | + | |'''Logical operators''' are also known as ''' Boolean operators''' |
|- | |- | ||
− | | 00 | + | | 00:42 |
| because they evaluate parts of an expression | | because they evaluate parts of an expression | ||
|- | |- | ||
− | | 00 | + | | 00:45 |
|and return a ''' true''' or ''' false''' value. | |and return a ''' true''' or ''' false''' value. | ||
|- | |- | ||
− | | 00 | + | | 00:48 |
− | |'''Logical Operators''' are | + | |'''Logical Operators''' are- |
|- | |- | ||
− | | 00 | + | | 00:51 |
− | | | + | | double ampersand (&&) that is '''and''' |
|- | |- | ||
− | | 00 | + | | 00:54 |
− | | ''' | + | | double '''pipe''' that is '''or''' |
|- | |- | ||
− | | 00 | + | | 00:56 |
− | | | + | | Exclamation mark (!) that is '''not'''. |
|- | |- | ||
− | | 01 | + | | 01:00 |
− | | | + | | '&&' (double ampersand) and '''and''' evaluate to '''true''' only if both the expressions are '''true.''' |
|- | |- | ||
− | | 01 | + | | 01:07 |
− | |Second expression is evaluated only if the first is '''true. ''' | + | |Second expression is evaluated only if the first is '''true.''' |
|- | |- | ||
− | | 01 | + | | 01:12 |
− | | Difference in the two forms is | + | | Difference in the two forms is precedence. |
|- | |- | ||
− | | 01 | + | | 01:15 |
|Symbolic '''and''' that is'''&&(double ampersand)''' has higher precedence. | |Symbolic '''and''' that is'''&&(double ampersand)''' has higher precedence. | ||
|- | |- | ||
− | | 01 | + | | 01:20 |
− | |Let's | + | |Let's see some examples now. |
|- | |- | ||
− | | 01 | + | | 01:22 |
|We will use '''irb''' for this. | |We will use '''irb''' for this. | ||
− | |||
|- | |- | ||
− | | 01 | + | | 01:25 |
− | | Open the ''' terminal''' by pressing '''Ctrl, Alt and T''' keys simultaneously. | + | | Open the ''' terminal''' by pressing '''Ctrl, Alt''' and '''T''' keys simultaneously. |
|- | |- | ||
− | | 01 | + | | 01:31 |
− | |Type | + | |Type "irb" and press '''Enter''' to launch '''interactive Ruby'''. |
|- | |- | ||
− | | 01 | + | | 01:36 |
− | | Type ''' 3 greater than 2 space double ampersand space 4 less than 5''' | + | | Type: ''' 3 greater than 2 space double ampersand space 4 less than 5'''. |
− | + | ||
|- | |- | ||
− | | 01 | + | | 01:47 |
− | | Press ''' Enter''' | + | | Press ''' Enter'''. |
|- | |- | ||
− | | 01 | + | | 01:49 |
− | | We get the output as | + | | We get the output as "true". |
|- | |- | ||
− | | 01 | + | | 01:53 |
− | |Here, | + | |Here, expression1 that is 3>2 is '''true'''. |
|- | |- | ||
− | | 01 | + | | 01:59 |
− | | | + | |Expression 2 that is 4<5 is also '''true.''' |
|- | |- | ||
− | | | + | |02:03 |
− | | | + | |Since both the expressions are '''true''', we get output as '''true.''' |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:08 |
− | |Now press '''Up Arrow''' key to get the previous command. | + | |Now, press '''Up-Arrow''' key to get the previous command. |
|- | |- | ||
− | | 02 | + | |02:12 |
− | | | + | | And replace the double ampersand symbol with the word "and". |
|- | |- | ||
− | | 02 | + | | 02:17 |
− | | Press '''Enter ''' | + | | Press '''Enter'''. |
|- | |- | ||
− | | 02 | + | | 02:19 |
|We get the same result. | |We get the same result. | ||
|- | |- | ||
− | | 02 | + | | 02:22 |
− | | Now press up arrow key again to get the previous command. | + | | Now, press up-arrow key again to get the previous command. |
|- | |- | ||
− | | 02 | + | | 02:27 |
− | | In | + | | In expression1, replace greater-than sign with less-than, |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:32 |
− | | | + | |press ''' Enter ''' |
|- | |- | ||
− | | 02 | + | | 02:35 |
− | | We get the output as | + | | We get the output as "false". |
|- | |- | ||
− | | 02 | + | | 02:38 |
− | |This is because | + | |This is because 3 < 2 is '''false.''' |
|- | |- | ||
− | | 02 | + | | 02:43 |
| Since the first expression is '''false''', the second expression will not be evaluated. | | Since the first expression is '''false''', the second expression will not be evaluated. | ||
|- | |- | ||
− | | 02 | + | | 02:49 |
− | | So, we get output as | + | | So, we get the output as "false". |
|- | |- | ||
− | | 02 | + | | 02:53 |
− | | ''' | + | | double '''pipe''' and '''or''' evaluate to '''true''', if either '''expression''' is '''true.''' |
|- | |- | ||
− | | 02 | + | | 02:59 |
|Second expression is evaluated only if first is '''false.''' | |Second expression is evaluated only if first is '''false.''' | ||
− | |||
|- | |- | ||
− | | 03 | + | | 03:04 |
|Difference in the two forms is '''precedence.''' | |Difference in the two forms is '''precedence.''' | ||
|- | |- | ||
− | | 03 | + | | 03:07 |
− | | Symbolic '''or''' i.e ''' | + | |Symbolic '''or''' i.e '''double pipe''' has higher '''precedence.''' |
|- | |- | ||
− | | 03 | + | | 03:11 |
| Now, let's try some examples. | | Now, let's try some examples. | ||
|- | |- | ||
− | | 03 | + | | 03:15 |
− | |''' 10 greater than 6 space double pipe space 12 less than 7''' | + | |Type: '''10 greater than 6 space double pipe space 12 less than 7''' |
|- | |- | ||
− | | 03 | + | | 03:23 |
| Press '''Enter.''' | | Press '''Enter.''' | ||
|- | |- | ||
− | | 03 | + | | 03:26 |
− | |We get output as | + | |We get output as "true". |
|- | |- | ||
− | | 03 | + | | 03:29 |
− | |Here | + | |Here expression 1, that is '''10>6''', is '''true'''. |
|- | |- | ||
− | | 03 | + | | 03:35 |
− | | Since the first expression is '''true''' , second expression will not be evaluated. | + | | Since the first expression is '''true''', second expression will not be evaluated. |
|- | |- | ||
− | | 03 | + | | 03:40 |
|So, we get the output as '''true. ''' | |So, we get the output as '''true. ''' | ||
− | |||
|- | |- | ||
− | | 03 | + | | 03:42 |
− | | Now press the '''Up Arrow''' key to get the previous command. | + | | Now, press the '''Up-Arrow''' key to get the previous command. |
|- | |- | ||
− | | 03 | + | | 03:46 |
− | | In '''expression 1''' replace '''greater than | + | | In '''expression 1''' replace '''greater than''' sign with '''less than''' sign |
|- | |- | ||
− | | 03 | + | | 03:52 |
− | | | + | | and replace '''pipe''' symbol with the word '''or.''' |
|- | |- | ||
− | | 03 | + | | 03:57 |
| Press '''Enter.''' | | Press '''Enter.''' | ||
|- | |- | ||
− | | 04 | + | | 04:00 |
|Here, '''expression1''' that is 10<6 is '''false. ''' | |Here, '''expression1''' that is 10<6 is '''false. ''' | ||
|- | |- | ||
− | | 04 | + | | 04:05 |
| '''Expression 2''' that is 12<7 is also '''false. ''' | | '''Expression 2''' that is 12<7 is also '''false. ''' | ||
|- | |- | ||
− | | 04 | + | | 04:10 |
− | | Since both the expressions are '''false''', we get output as | + | | Since both the expressions are '''false''', we get output as "false". |
|- | |- | ||
− | | 04 | + | | 04:15 |
− | |'' | + | |'!' (exclamation mark ) and '''not''' operators return the opposite value of the expression. |
|- | |- | ||
− | | 04 | + | | 04:20 |
− | | If the | + | | If the expression is '''true''', exclamation mark operator will return a '''false''' value. |
|- | |- | ||
− | | 04 | + | | 04:27 |
|It will return '''true''' if the expression is '''false'''. | |It will return '''true''' if the expression is '''false'''. | ||
|- | |- | ||
− | | 04 | + | | 04:30 |
− | | Difference in the two forms is '''precedence. ''' | + | | Difference in the two forms is '''precedence.''' |
|- | |- | ||
− | | 04 | + | | 04:33 |
− | | Symbolic '''not that is | + | | Symbolic '''not''', that is '!', has higher '''precedence.''' |
|- | |- | ||
− | | 04 | + | | 04:37 |
− | | | + | | Let's try out the "not" operator. |
|- | |- | ||
− | | 04 | + | | 04:40 |
− | |First type ''' 10 double equal to 10''' | + | |First, type: '''10 double equal to 10''' |
|- | |- | ||
− | | 04 | + | | 04:45 |
− | | Press '''Enter ''' | + | | Press '''Enter '''. |
|- | |- | ||
− | | 04 | + | | 04:47 |
− | |We get the output as | + | |We get the output as "true". |
|- | |- | ||
− | | 04 | + | | 04:50 |
− | |To invert the result of above expression, | + | |To '''invert''' the result of above expression, |
|- | |- | ||
− | | 04 | + | | 04:53 |
− | | let's add the | + | | let's add the "not" operator before the expression. |
− | + | ||
|- | |- | ||
− | | 04 | + | | 04:57 |
− | |Type ''' | + | |Type: exclamation mark within brackets '''10 double equal to 10 '''. |
− | + | ||
|- | |- | ||
− | | 05 | + | | 05:04 |
| Press '''Enter. ''' | | Press '''Enter. ''' | ||
− | |||
|- | |- | ||
− | | 05 | + | | 05:06 |
− | |We get the output as | + | |We get the output as "false". |
|- | |- | ||
− | | 05 | + | | 05:10 |
| Press '''Ctrl+L''' simultaneously to clear the '''irb''' console. | | Press '''Ctrl+L''' simultaneously to clear the '''irb''' console. | ||
|- | |- | ||
− | | 05 | + | | 05:15 |
− | |Next, let us learn about '''parallel assignment. ''' | + | |Next, let us learn about '''parallel assignment.''' |
|- | |- | ||
− | | 05 | + | | 05:20 |
|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. ''' | ||
|- | |- | ||
− | | 05 | + | | 05:26 |
| Let's switch to the '''terminal.''' | | Let's switch to the '''terminal.''' | ||
|- | |- | ||
− | | 05 | + | | 05:29 |
− | |Let's declare three variables | + | |Let's declare three variables 'a', 'b', 'c' using '''parallel assignment. ''' |
|- | |- | ||
− | | 05 | + | | 05:36 |
− | | Type '''a comma b comma c equal to 10 comma 20 comma 30 ''' | + | | Type: '''a comma b comma c equal to 10 comma 20 comma 30''' |
|- | |- | ||
− | | 05 | + | | 05:45 |
| and press '''Enter. ''' | | and press '''Enter. ''' | ||
|- | |- | ||
− | | 05 | + | | 05:47 |
− | |Here, | + | |Here, 10 will be assigned to variable 'a', |
|- | |- | ||
− | | 05 | + | | 05:52 |
− | | | + | |20 will be assigned to variable 'b' |
|- | |- | ||
− | | 05 | + | | 05:54 |
− | | | + | |30 will be assigned to variable 'c'. |
− | + | ||
|- | |- | ||
− | | 05 | + | | 05:56 |
− | |The right hand side acts as an '''array'''. | + | |The right-hand side acts as an '''array'''. |
|- | |- | ||
− | | 06 | + | | 06:01 |
− | |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. |
|- | |- | ||
− | | 06 | + | | 06:10 |
− | |We will learn about '''arrays''' in detail in the upcoming tutorials. | + | |We will learn about '''arrays''' in detail, in the upcoming tutorials. |
|- | |- | ||
− | | 06 | + | | 06:14 |
− | | For now, let's check whether the | + | | For now, let's check whether the assignment is done properly. |
|- | |- | ||
− | |06 | + | |06:20 |
− | | Type | + | | Type 'a' and press '''Enter.''' |
|- | |- | ||
− | | 06 | + | | 06:23 |
− | |Value | + | |Value 10, stored in variable 'a', is displayed. |
|- | |- | ||
− | | 06 | + | | 06:28 |
− | |Type | + | |Type 'b' and press '''Enter.''' |
|- | |- | ||
− | | 06 | + | | 06:31 |
− | |We get 20 | + | |We get 20. |
|- | |- | ||
− | | 06 | + | | 06:33 |
− | | Type | + | | Type 'c' and press '''Enter.''' |
|- | |- | ||
− | | 06 | + | | 06:37 |
− | |30 is displayed. | + | | 30 is displayed. |
− | + | ||
|- | |- | ||
− | | 06 | + | | 06:40 |
| '''Parallel assignment''' is also useful for swapping the values stored in two variables. | | '''Parallel assignment''' is also useful for swapping the values stored in two variables. | ||
+ | |||
|- | |- | ||
− | | 06 | + | | 06:45 |
− | |Let us swap the values of variables a and b. | + | |Let us swap the values of variables 'a' and 'b'. |
|- | |- | ||
− | | 06 | + | | 06:50 |
− | |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''' | + | |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''' |
|- | |- | ||
− | | 07 | + | | 07:11 |
| Press '''Enter. ''' | | Press '''Enter. ''' | ||
|- | |- | ||
− | | 07 | + | | 07:13 |
− | | We get the output as '''a=10 ''' | + | | We get the output as '''a=10''' |
|- | |- | ||
− | | 07 | + | | 07:16 |
− | |'''b=20 ''' | + | |'''b=20.''' |
|- | |- | ||
− | | 07 | + | | 07:20 |
− | |Now let's swap a and b. | + | |Now, let's swap 'a' and 'b'. |
|- | |- | ||
− | | 07 | + | | 07:23 |
− | |To do so type | + | |To do so, type: |
|- | |- | ||
− | | 07 | + | | 07:25 |
|'''a comma b equal to b comma a ''' | |'''a comma b equal to b comma a ''' | ||
|- | |- | ||
− | | 07 | + | | 07:31 |
|Press '''Enter.''' | |Press '''Enter.''' | ||
|- | |- | ||
− | | 07 | + | | 07:33 |
− | | Press '''Up Arrow''' key twice to get the '''puts''' command and press '''Enter.''' | + | | Press '''Up-Arrow''' key twice to get the '''puts''' command and press '''Enter.''' |
|- | |- | ||
− | | 07 | + | | 07:39 |
− | | We get the output as | + | | We get the output as- |
|- | |- | ||
− | | 07 | + | | 07:41 |
|'''a=20 ''' | |'''a=20 ''' | ||
|- | |- | ||
− | | 07 | + | | 07:44 |
− | | '''b=10 ''' | + | | '''b=10 '''. |
− | + | ||
|- | |- | ||
− | | 07 | + | | 07:47 |
| We will now learn about '''range''' in '''Ruby.''' | | We will now learn about '''range''' in '''Ruby.''' | ||
|- | |- | ||
− | | 07 | + | | 07:50 |
− | |The values in a '''range''' can be numbers, characters, | + | |The values in a '''range''' can be numbers, characters, '''string'''s or '''object'''s. |
|- | |- | ||
− | | 07 | + | | 07:58 |
|'''Ranges''' are used to express a '''sequence. ''' | |'''Ranges''' are used to express a '''sequence. ''' | ||
|- | |- | ||
− | | 08 | + | | 08:02 |
|'''Sequence range''' is used to create a range of successive values. | |'''Sequence range''' is used to create a range of successive values. | ||
|- | |- | ||
− | | 08 | + | | 08:06 |
|It consists of a start value, range of values and an end value. | |It consists of a start value, range of values and an end value. | ||
|- | |- | ||
− | | 08 | + | | 08:13 |
|'''(..) two dot operator''' creates ''' inclusive range. ''' | |'''(..) two dot operator''' creates ''' inclusive range. ''' | ||
|- | |- | ||
− | | 08 | + | | 08:16 |
|'''(...) three dot operator''' creates an '''exclusive range. ''' | |'''(...) three dot operator''' creates an '''exclusive range. ''' | ||
|- | |- | ||
− | | 08 | + | | 08:20 |
− | |'''Ranges''' are used to identify whether a value falls within a particular range | + | |'''Ranges''' are used to identify whether a value falls within a particular range too. |
|- | |- | ||
− | | 08 | + | | 08:26 |
|We do this using (===) the '''equality''' operator. | |We do this using (===) the '''equality''' operator. | ||
|- | |- | ||
− | | 08 | + | | 08:30 |
|Let us try out some examples on '''ranges.''' | |Let us try out some examples on '''ranges.''' | ||
|- | |- | ||
− | | 08 | + | | 08:33 |
|Let's switch to '''terminal. ''' | |Let's switch to '''terminal. ''' | ||
|- | |- | ||
− | | 08 | + | | 08:36 |
− | |Type ''' | + | |Type: within brackets ''' 1 two dots 10''' then '''dot to underscore a''' |
− | + | ||
|- | |- | ||
− | | 08 | + | | 08:46 |
|'''Two dot''' operator creates '''inclusive range. ''' | |'''Two dot''' operator creates '''inclusive range. ''' | ||
|- | |- | ||
− | | 08 | + | | 08:50 |
− | |'''Inclusive operator''' includes both begin and end values in a range. | + | |'''Inclusive operator''' includes both, begin and end values, in a range. |
|- | |- | ||
− | | 08 | + | | 08:57 |
− | |Here '''to_a''' method is used to convert a '''range''' to a list. | + | |Here '''to_a''' '''method''' is used to convert a '''range''' to a list. |
|- | |- | ||
− | | 09 | + | | 09:03 |
|Press '''Enter. ''' | |Press '''Enter. ''' | ||
|- | |- | ||
− | | 09 | + | | 09:05 |
− | |Here you can see the values 1 and 10 are included in the range. | + | |Here, you can see the values 1 and 10 are included in the range. |
|- | |- | ||
− | | 09 | + | | 09:11 |
− | |Now we will see an '''exclusive range''' operator. | + | |Now, we will see an '''exclusive range''' operator. |
|- | |- | ||
− | | 09 | + | | 09:16 |
− | |Type ''' | + | |Type: within brackets '''1 three dots 10''' then '''dot to underscore a'''. |
|- | |- | ||
− | | 09 | + | | 09:27 |
|'''Three dot''' operator creates an '''exclusive range. ''' | |'''Three dot''' operator creates an '''exclusive range. ''' | ||
|- | |- | ||
− | | 09 | + | | 09:31 |
|'''Exclusive range''' operator excludes the end value from the sequence. | |'''Exclusive range''' operator excludes the end value from the sequence. | ||
|- | |- | ||
− | | 09 | + | | 09:37 |
|Press '''Enter. ''' | |Press '''Enter. ''' | ||
|- | |- | ||
− | | 09 | + | | 09:39 |
− | |Here the end value 10 is not included in the range. | + | |Here, the end value 10 is not included in the range. |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:45 |
− | |Now let's 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. |
|- | |- | ||
− | | 09 | + | | 09:50 |
− | |Type ''' | + | |Type: within brackets '''1 two dots 10 three times equal to''' and then '''5'''. |
− | + | ||
|- | |- | ||
− | | 10 | + | | 10:00 |
|Press '''Enter. ''' | |Press '''Enter. ''' | ||
|- | |- | ||
− | | 10 | + | | 10:02 |
− | | | + | |Equality operator is used to check whether a value lies in the range. |
|- | |- | ||
− | | 10 | + | | 10:07 |
− | |We get the output as | + | |We get the output as "true" since 5 lies in the range 1 to 10. |
|- | |- | ||
− | | 10 | + | | 10:14 |
|This brings us to the end of this Spoken Tutorial. | |This brings us to the end of this Spoken Tutorial. | ||
|- | |- | ||
− | | 10 | + | | 10:17 |
− | |In this tutorial, we have learnt | + | |In this tutorial, we have learnt: |
− | + | ||
|- | |- | ||
− | | 10 | + | | 10:20 |
− | | '''Logical operator''' i.e | + | |'''Logical operator''' i.e double ampersand, double '''pipe''' and exclamation mark operators. |
|- | |- | ||
− | | 10 | + | | 10:27 |
− | |'''Parallel assignment''' Ex: a,b,c=10,20,30 | + | |'''Parallel assignment''' Ex: a, b, c = 10, 20, 30 |
|- | |- | ||
− | | 10 | + | | 10:34 |
− | |'''Range Operator | + | |'''Range Operator- Inclusive operator '''(..) and '''Exclusive operator(...)'''. |
|- | |- | ||
− | | 10 | + | | 10:39 |
− | |As an assignment | + | |As an assignment, |
|- | |- | ||
− | | 10 | + | | 10:41 |
− | | | + | |declare two variables using '''parallel assignment''' and |
|- | |- | ||
− | | 10 | + | | 10:45 |
− | | | + | |check whether their sum lies between 20 and 50. |
− | + | ||
|- | |- | ||
− | | 10 | + | | 10:49 |
| Watch the video available at the following link. | | Watch the video available at the following link. | ||
|- | |- | ||
− | | 10 | + | | 10:52 |
− | |It | + | |It summarizes the Spoken Tutorial project. |
|- | |- | ||
− | | 10 | + | | 10:56 |
|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. | ||
|- | |- | ||
− | | 11 | + | | 11:00 |
− | | | + | | The Spoken Tutorial project team: |
|- | |- | ||
− | | 11 | + | | 11:03 |
− | |Conducts workshops using spoken tutorials | + | |Conducts workshops using spoken tutorials. |
|- | |- | ||
− | |11 | + | |11:05 |
− | |Gives certificates to those who pass an online test | + | |Gives certificates to those who pass an online test. |
|- | |- | ||
− | | 11 | + | | 11:09 |
− | |For more details, please write to contact@spoken-tutorial.org | + | |For more details, please write to:contact@spoken-tutorial.org |
|- | |- | ||
− | | 11 | + | | 11:15 |
− | | | + | | '''Spoken Tutorial''' project is a part of the '''Talk to a Teacher''' project. |
|- | |- | ||
− | | 11 | + | | 11:19 |
|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. | ||
|- | |- | ||
− | | 11 | + | | 11:25 |
− | |More information on this | + | |More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
|- | |- | ||
− | | 11 | + | | 11:34 |
− | |This script has been contributed by the spoken tutorial team IIT Bombay | + | |This script has been contributed by the spoken tutorial team, '''IIT Bombay'''. |
|- | |- | ||
− | | 11 | + | | 11:38 |
− | |And this is Shalini Nair signing off Thank you | + | |And this is Shalini Nair, signing off. Thank you. |
− | + | ||
|} | |} |
Latest revision as of 15:14, 27 March 2017
Time | Narration |
00:02 | Welcome to this Spoken Tutorial on Logical & Other Operators. |
00:06 | In this tutorial, we will learn: |
00:09 | Logical Operators |
00:11 | Parallel assignment and |
00:13 | Range Operators . |
00:15 | Here, we are using: |
00:17 | Ubuntu Linux version 12.04 |
00:20 | Ruby 1.9.3 |
00:23 | To follow this tutorial, you must know how to use Terminal and Text editor in Linux. |
00:29 | You must also be familiar with 'irb'. |
00:33 | If not, for relevant tutorials, please visit our website. |
00:38 | Logical operators are also known as Boolean operators |
00:42 | because they evaluate parts of an expression |
00:45 | and return a true or false value. |
00:48 | Logical Operators are- |
00:51 | double ampersand (&&) that is and |
00:54 | double pipe that is or |
00:56 | Exclamation mark (!) that is not. |
01:00 | '&&' (double ampersand) and and evaluate to true only if both the expressions are true. |
01:07 | Second expression is evaluated only if the first is true. |
01:12 | Difference in the two forms is precedence. |
01:15 | Symbolic and that is&&(double ampersand) has higher precedence. |
01:20 | Let's see some examples now. |
01:22 | We will use irb for this. |
01:25 | Open the terminal by pressing Ctrl, Alt and T keys simultaneously. |
01:31 | Type "irb" and press Enter to launch interactive Ruby. |
01:36 | Type: 3 greater than 2 space double ampersand space 4 less than 5. |
01:47 | Press Enter. |
01:49 | We get the output as "true". |
01:53 | Here, expression1 that is 3>2 is true. |
01:59 | Expression 2 that is 4<5 is also true. |
02:03 | Since both the expressions are true, we get output as true. |
02:08 | Now, press Up-Arrow key to get the previous command. |
02:12 | And replace the double ampersand symbol with the word "and". |
02:17 | Press Enter. |
02:19 | We get the same result. |
02:22 | Now, press up-arrow key again to get the previous command. |
02:27 | In expression1, replace greater-than sign with less-than, |
02:32 | press Enter |
02:35 | We get the output as "false". |
02:38 | This is because 3 < 2 is false. |
02:43 | Since the first expression is false, the second expression will not be evaluated. |
02:49 | So, we get the output as "false". |
02:53 | double pipe and or evaluate to true, if either expression is true. |
02:59 | Second expression is evaluated only if first is false. |
03:04 | Difference in the two forms is precedence. |
03:07 | Symbolic or i.e double pipe has higher precedence. |
03:11 | Now, let's try some examples. |
03:15 | Type: 10 greater than 6 space double pipe space 12 less than 7 |
03:23 | Press Enter. |
03:26 | We get output as "true". |
03:29 | Here expression 1, that is 10>6, is true. |
03:35 | Since the first expression is true, second expression will not be evaluated. |
03:40 | So, we get the output as true. |
03:42 | Now, press the Up-Arrow key to get the previous command. |
03:46 | In expression 1 replace greater than sign with less than sign |
03:52 | and replace pipe symbol with the word or. |
03:57 | Press Enter. |
04:00 | Here, expression1 that is 10<6 is false. |
04:05 | Expression 2 that is 12<7 is also false. |
04:10 | Since both the expressions are false, we get output as "false". |
04:15 | '!' (exclamation mark ) and not operators return the opposite value of the expression. |
04:20 | If the expression is true, exclamation mark operator will return a false value. |
04:27 | It will return true if the expression is false. |
04:30 | Difference in the two forms is precedence. |
04:33 | Symbolic not, that is '!', has higher precedence. |
04:37 | Let's try out the "not" operator. |
04:40 | First, type: 10 double equal to 10 |
04:45 | Press Enter . |
04:47 | We get the output as "true". |
04:50 | To invert the result of above expression, |
04:53 | let's add the "not" operator before the expression. |
04:57 | Type: exclamation mark within brackets 10 double equal to 10 . |
05:04 | Press Enter. |
05:06 | We get the output as "false". |
05:10 | Press Ctrl+L simultaneously to clear the irb console. |
05:15 | Next, let us learn about parallel assignment. |
05:20 | Multiple variables can be initialized with a single line of Ruby code, through parallel assignment. |
05:26 | Let's switch to the terminal. |
05:29 | Let's declare three variables 'a', 'b', 'c' using parallel assignment. |
05:36 | Type: a comma b comma c equal to 10 comma 20 comma 30 |
05:45 | and press Enter. |
05:47 | Here, 10 will be assigned to variable 'a', |
05:52 | 20 will be assigned to variable 'b' |
05:54 | 30 will be assigned to variable 'c'. |
05:56 | The right-hand side acts as an array. |
06:01 | If we list multiple variables on the left hand side then the array is unpacked and assigned into the respective variables. |
06:10 | We will learn about arrays in detail, in the upcoming tutorials. |
06:14 | For now, let's check whether the assignment is done properly. |
06:20 | Type 'a' and press Enter. |
06:23 | Value 10, stored in variable 'a', is displayed. |
06:28 | Type 'b' and press Enter. |
06:31 | We get 20. |
06:33 | Type 'c' and press Enter. |
06:37 | 30 is displayed. |
06:40 | Parallel assignment is also useful for swapping the values stored in two variables. |
06:45 | Let us swap the values of variables 'a' and 'b'. |
06:50 | 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 |
07:11 | Press Enter. |
07:13 | We get the output as a=10 |
07:16 | b=20. |
07:20 | Now, let's swap 'a' and 'b'. |
07:23 | To do so, type: |
07:25 | a comma b equal to b comma a |
07:31 | Press Enter. |
07:33 | Press Up-Arrow key twice to get the puts command and press Enter. |
07:39 | We get the output as- |
07:41 | a=20 |
07:44 | b=10 . |
07:47 | We will now learn about range in Ruby. |
07:50 | The values in a range can be numbers, characters, strings or objects. |
07:58 | Ranges are used to express a sequence. |
08:02 | Sequence range is used to create a range of successive values. |
08:06 | It consists of a start value, range of values and an end value. |
08:13 | (..) two dot operator creates inclusive range. |
08:16 | (...) three dot operator creates an exclusive range. |
08:20 | Ranges are used to identify whether a value falls within a particular range too. |
08:26 | We do this using (===) the equality operator. |
08:30 | Let us try out some examples on ranges. |
08:33 | Let's switch to terminal. |
08:36 | Type: within brackets 1 two dots 10 then dot to underscore a |
08:46 | Two dot operator creates inclusive range. |
08:50 | Inclusive operator includes both, begin and end values, in a range. |
08:57 | Here to_a method is used to convert a range to a list. |
09:03 | Press Enter. |
09:05 | Here, you can see the values 1 and 10 are included in the range. |
09:11 | Now, we will see an exclusive range operator. |
09:16 | Type: within brackets 1 three dots 10 then dot to underscore a. |
09:27 | Three dot operator creates an exclusive range. |
09:31 | Exclusive range operator excludes the end value from the sequence. |
09:37 | Press Enter. |
09:39 | Here, the end value 10 is not included in the range. |
09:45 | Now, let's check whether 5 lies in the range of 1 to 10. |
09:50 | Type: within brackets 1 two dots 10 three times equal to and then 5. |
10:00 | Press Enter. |
10:02 | Equality operator is used to check whether a value lies in the range. |
10:07 | We get the output as "true" since 5 lies in the range 1 to 10. |
10:14 | This brings us to the end of this Spoken Tutorial. |
10:17 | In this tutorial, we have learnt: |
10:20 | Logical operator i.e double ampersand, double pipe and exclamation mark operators. |
10:27 | Parallel assignment Ex: a, b, c = 10, 20, 30 |
10:34 | Range Operator- Inclusive operator (..) and Exclusive operator(...). |
10:39 | As an assignment, |
10:41 | declare two variables using parallel assignment and |
10:45 | check whether their sum lies between 20 and 50. |
10:49 | Watch the video available at the following link. |
10:52 | It summarizes the Spoken Tutorial project. |
10:56 | If you do not have good bandwidth, you can download and watch it. |
11:00 | The Spoken Tutorial project team: |
11:03 | Conducts workshops using spoken tutorials. |
11:05 | Gives certificates to those who pass an online test. |
11:09 | For more details, please write to:contact@spoken-tutorial.org |
11:15 | Spoken Tutorial project is a part of the Talk to a Teacher project. |
11:19 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
11:25 | More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
11:34 | This script has been contributed by the spoken tutorial team, IIT Bombay. |
11:38 | And this is Shalini Nair, signing off. Thank you. |