Difference between revisions of "Ruby/C2/Logical-and-other-Operators/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 177: | Line 177: | ||
|- | |- | ||
| 02:53 | | 02:53 | ||
− | | ''' double pipe''' and '''or''' evaluate to '''true''', if either '''expression''' is '''true.''' | + | | '''double pipe''' and '''or''' evaluate to '''true''', if either '''expression''' is '''true.''' |
|- | |- | ||
Line 189: | Line 189: | ||
|- | |- | ||
| 03:07 | | 03:07 | ||
− | |Symbolic '''or''' i.e ''' double pipe''' has higher '''precedence.''' | + | |Symbolic '''or''' i.e '''double pipe''' has higher '''precedence.''' |
|- | |- | ||
Line 209: | Line 209: | ||
|- | |- | ||
| 03:29 | | 03:29 | ||
− | |Here '''expression 1''', that is '''10>6''' is '''true'''. | + | |Here '''expression 1''', that is '''10>6''', is '''true'''. |
|- | |- | ||
| 03:35 | | 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. |
|- | |- | ||
Line 221: | Line 221: | ||
|- | |- | ||
| 03:42 | | 03:42 | ||
− | | Now press the '''Up-Arrow''' key to get the previous command. | + | | Now, press the '''Up-Arrow''' key to get the previous command. |
|- | |- | ||
Line 249: | Line 249: | ||
|- | |- | ||
| 04:15 | | 04:15 | ||
− | |'''! (exclamation mark )''' and '''not''' operators return the opposite value of the expression | + | |'''! (exclamation mark )''' and '''not''' operators return the opposite value of the expression. |
|- | |- | ||
| 04:20 | | 04:20 | ||
− | | If the | + | | If the expression is '''true''', exclamation mark operator will return a '''false''' value. |
|- | |- | ||
Line 265: | Line 265: | ||
|- | |- | ||
| 04:33 | | 04:33 | ||
− | | Symbolic '''not that is (!) | + | | Symbolic '''"not"''', that is (!), has higher '''precedence.''' |
|- | |- | ||
| 04:37 | | 04:37 | ||
− | | | + | | Let's try out the "not" operator. |
|- | |- | ||
| 04:40 | | 04:40 | ||
− | |First type ''' 10 double equal to 10''' | + | |First, type: '''10 double equal to 10''' |
|- | |- | ||
| 04:45 | | 04:45 | ||
− | | Press '''Enter ''' | + | | Press '''Enter '''. |
|- | |- | ||
Line 285: | Line 285: | ||
|- | |- | ||
| 04:50 | | 04:50 | ||
− | |To invert the result of above expression, | + | |To '''invert''' the result of above expression, |
|- | |- | ||
| 04:53 | | 04:53 | ||
− | | let's add the | + | | let's add the "not" operator before the expression. |
|- | |- | ||
| 04:57 | | 04:57 | ||
− | |Type | + | |Type: exclamation mark within brackets '''10 double equal to 10 '''. |
|- | |- | ||
Line 309: | Line 309: | ||
|- | |- | ||
| 05:15 | | 05:15 | ||
− | |Next, let us learn about '''parallel assignment. ''' | + | |Next, let us learn about '''parallel assignment.''' |
|- | |- | ||
Line 325: | Line 325: | ||
|- | |- | ||
| 05:36 | | 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''' |
|- | |- | ||
Line 333: | Line 333: | ||
|- | |- | ||
| 05:47 | | 05:47 | ||
− | |Here, '''10''' will be assigned to variable | + | |Here, '''10''' will be assigned to variable 'a', |
|- | |- | ||
| 05:52 | | 05:52 | ||
− | |'''20''' will be assigned to variable | + | |'''20''' will be assigned to variable 'b' |
|- | |- | ||
| 05:54 | | 05:54 | ||
− | |'''30''' will be assigned to variable | + | |'''30''' will be assigned to variable 'c'. |
|- | |- | ||
| 05:56 | | 05:56 | ||
− | |The right hand side acts as an '''array'''. | + | |The right-hand side acts as an '''array'''. |
|- | |- | ||
| 06:01 | | 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. |
|- | |- | ||
Line 357: | Line 357: | ||
|- | |- | ||
| 06:14 | | 06:14 | ||
− | | For now, let's check whether the | + | | For now, let's check whether the assignment is done properly. |
|- | |- | ||
|06:20 | |06:20 | ||
− | | Type | + | | Type 'a' and press '''Enter.''' |
|- | |- | ||
| 06:23 | | 06:23 | ||
− | |Value | + | |Value 10, stored in variable 'a', is displayed. |
|- | |- | ||
| 06:28 | | 06:28 | ||
− | |Type | + | |Type 'b' and press '''Enter.''' |
|- | |- | ||
| 06:31 | | 06:31 | ||
− | |We get 20 | + | |We get 20. |
|- | |- | ||
| 06:33 | | 06:33 | ||
− | | Type | + | | Type 'c' and press '''Enter.''' |
|- | |- | ||
Line 389: | Line 389: | ||
|- | |- | ||
| 06:45 | | 06:45 | ||
− | |Let us swap the values of variables a and b. | + | |Let us swap the values of variables 'a' and 'b'. |
|- | |- | ||
| 06:50 | | 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''' |
|- | |- | ||
Line 401: | Line 401: | ||
|- | |- | ||
| 07:13 | | 07:13 | ||
− | | We get the output as '''a=10 ''' | + | | We get the output as '''a=10''' |
|- | |- | ||
| 07:16 | | 07:16 | ||
− | |'''b=20 ''' | + | |'''b=20.''' |
|- | |- | ||
| 07:20 | | 07:20 | ||
− | |Now let's swap 'a' and 'b'. | + | |Now, let's swap 'a' and 'b'. |
|- | |- |
Revision as of 12:04, 29 October 2015
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 (!) 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 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. |