Difference between revisions of "Ruby/C2/Arithmetic-and-Relational-Operators/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(6 intermediate revisions by 4 users not shown) | |||
Line 2: | Line 2: | ||
{| border=1 | {| border=1 | ||
− | || ''Time''' | + | || '''Time''' |
|| '''Narration''' | || '''Narration''' | ||
|- | |- | ||
− | | 00 | + | | 00:01 |
− | | Welcome to the Spoken Tutorial on Arithmetic & Relational Operators in '''Ruby.''' | + | | Welcome to the '''Spoken Tutorial''' on '''Arithmetic & Relational Operators''' in '''Ruby.''' |
|- | |- | ||
− | | 00 | + | | 00:06 |
− | | In this tutorial we will learn about | + | | In this tutorial, we will learn about: |
|- | |- | ||
− | | 00 | + | | 00:08 |
| Arithmetic Operators | | Arithmetic Operators | ||
|- | |- | ||
− | | 00 | + | | 00:10 |
| Operator Precedence | | Operator Precedence | ||
|- | |- | ||
− | | 00 | + | | 00:12 |
− | | Relational Operators | + | |Relational Operators. |
|- | |- | ||
− | | 00 | + | | 00:14 |
− | | Here we are using ''Ubuntu Linux '''version 12.04 | + | | Here we are using: '''Ubuntu Linux '''version '''12.04''' , '''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:28 |
− | | You must also be familiar with '''irb ''' | + | | You must also be familiar with ''''irb'.''' |
|- | |- | ||
− | | 00 | + | | 00:31 |
− | |If not, for relevant tutorials, please visit our website | + | |If not, for relevant tutorials, please visit our website. |
|- | |- | ||
− | | 00 | + | | 00:34 |
− | |Now let us learn about arithmetic operators. | + | |Now, let us learn about arithmetic operators. |
|- | |- | ||
− | | 00 | + | | 00:38 |
− | | '''Ruby''' has following arithmetic operators. | + | | '''Ruby''' has the following arithmetic operators. |
− | + | ||
|- | |- | ||
− | | 00 | + | | 00:42 |
− | |+ Addition | + | |'+' Addition, e.g. a+b. |
|- | |- | ||
− | | 00 | + | | 00:45 |
− | | - Subtraction | + | | '-' Subtraction, e.g. a-b. |
|- | |- | ||
− | | 00 | + | | 00:48 |
− | | | + | | '/' Division, e.g. a/b. |
|- | |- | ||
− | | 00 | + | | 00:51 |
− | |* Multiplication | + | | '*' Multiplication, e.g. a*b. |
|- | |- | ||
− | | 00 | + | | 00:55 |
− | | % Modulus | + | | '%' Modulus, e.g. a%b. |
|- | |- | ||
− | | 00 | + | | 00:59 |
− | |** Exponent | + | | '**' Exponent, e.g. a**b. |
|- | |- | ||
− | | 01 | + | | 01:04 |
|Let us try these arithmetic operators using '''irb.''' | |Let us try these arithmetic operators using '''irb.''' | ||
|- | |- | ||
− | | 01 | + | | 01:08 |
− | | Open the terminal by pressing ''' Ctrl, Alt''' and ''' T '''keys simultaneously. | + | | Open the terminal by pressing ''' Ctrl, Alt''' and '''T '''keys simultaneously. |
|- | |- | ||
− | | 01 | + | | 01:14 |
− | |A terminal window appears on your screen. | + | |A '''terminal window''' appears on your screen. |
|- | |- | ||
− | | 01 | + | | 01:17 |
− | | Type ''' | + | | Type "irb" and press '''Enter''' to launch the '''interactive Ruby. ''' |
|- | |- | ||
− | | 01 | + | | 01:21 |
− | |Type 10 plus 20 and press '''Enter. ''' | + | |Type: 10 plus 20 and press '''Enter. ''' |
|- | |- | ||
− | | 01 | + | | 01:25 |
|The addition operation is performed and the result 30 is displayed. | |The addition operation is performed and the result 30 is displayed. | ||
|- | |- | ||
− | | 01 | + | | 01:31 |
− | |Similarly the subtraction and multiplication operations can be performed. | + | |Similarly, the subtraction and multiplication operations can be performed. |
|- | |- | ||
− | | 01 | + | | 01:35 |
| Let us try the division operator. | | Let us try the division operator. | ||
|- | |- | ||
− | | 01 | + | | 01:38 |
− | |Type '''10 slash 4''' | + | |Type: '''10 slash 4''' |
|- | |- | ||
− | | 01 | + | | 01:40 |
| and press '''Enter. ''' | | and press '''Enter. ''' | ||
|- | |- | ||
− | | 01 | + | | 01:42 |
| Here you can see the result is truncated to the nearest whole number which is 2. | | Here you can see the result is truncated to the nearest whole number which is 2. | ||
|- | |- | ||
− | | 01 | + | | 01:47 |
− | | To get a more accurate answer, we need to express one number as '''float''' | + | | To get a more accurate answer, we need to express one number as '''float'''. |
|- | |- | ||
− | | 01 | + | | 01:52 |
− | |Type '''10.0 slash 4''' | + | |Type: '''10.0 slash 4''' |
|- | |- | ||
− | | 01 | + | | 01:56 |
− | |and press '''Enter. ''' | + | |and press '''Enter.''' |
− | + | ||
|- | |- | ||
− | | 01 | + | | 01:58 |
− | | Now we get the result as 2.5 | + | | Now we get the result as 2.5. |
|- | |- | ||
− | | 02 | + | | 02:01 |
| Let's now try the ''' modulus''' operator. | | Let's now try the ''' modulus''' operator. | ||
|- | |- | ||
− | | 02 | + | | 02:05 |
| The '''modulus''' operator returns the remainder as output. | | The '''modulus''' operator returns the remainder as output. | ||
− | |||
|- | |- | ||
− | | 02 | + | | 02:09 |
− | | Type '''12 percentage sign 5''' and press '''Enter''' | + | | Type: '''12 percentage sign 5''' and press '''Enter'''. |
|- | |- | ||
− | | 02 | + | | 02:15 |
− | |Here 12 is divided by 5 and the remainder 2 is returned back. | + | |Here, 12 is divided by 5 and the remainder 2 is returned back. |
|- | |- | ||
− | | 02 | + | | 02:21 |
− | | Now let's try the '''exponent''' operator. | + | | Now, let's try the '''exponent''' operator. |
|- | |- | ||
| 02.24 | | 02.24 | ||
− | | Type ''' 2 followed by the asterisk symbol twice and then 5''' and press '''Enter. ''' | + | | Type: ''' 2''' followed by the asterisk symbol twice and then '''5''' and press '''Enter.''' |
|- | |- | ||
− | | 02 | + | | 02:32 |
|This means that 2 is raised to the power of 5. | |This means that 2 is raised to the power of 5. | ||
|- | |- | ||
− | | 02 | + | | 02:36 |
− | | So we get the output as 32. | + | | So, we get the output as 32. |
|- | |- | ||
− | | 02 | + | | 02:39 |
|Next, let us learn about operator precedence. | |Next, let us learn about operator precedence. | ||
|- | |- | ||
− | | 02 | + | | 02:44 |
| When several operations occur in a mathematical expression, | | When several operations occur in a mathematical expression, | ||
|- | |- | ||
− | | 02 | + | | 02:47 |
| each part is evaluated | | each part is evaluated | ||
|- | |- | ||
− | | 02 | + | | 02:50 |
|and resolved in a predetermined order called '''operator precedence'''. | |and resolved in a predetermined order called '''operator precedence'''. | ||
|- | |- | ||
− | | 02 | + | | 02:56 |
− | | This means that the operator which has | + | | This means that the operator which has the highest priority is executed first. |
|- | |- | ||
− | | 03 | + | | 03:01 |
− | |This is then followed by the next operator in the | + | |This is then followed by the next operator in the priority order and so on. |
− | + | ||
|- | |- | ||
− | | 03 | + | | 03:07 |
− | |This slide lists all operators from highest precedence to lowest. | + | |This '''slide''' lists all operators from highest precedence to lowest. |
|- | |- | ||
− | | 03 | + | | 03:13 |
− | | For example ''' 3 + 4 * 5 ''' returns 23 and not 35 | + | | For example, ''' 3 + 4 * 5 ''' returns 23 and not 35. |
|- | |- | ||
− | | 03 | + | | 03:23 |
− | |The multiplication operator (*) has higher precedence than the addition operator (+) | + | |The multiplication operator asterisk (*) has higher precedence than the addition operator plus (+) |
|- | |- | ||
− | | 03 | + | | 03:29 |
| and thus will be evaluated first. | | and thus will be evaluated first. | ||
− | |||
|- | |- | ||
− | | 03 | + | | 03:32 |
− | |Hence four fives are twenty and then three is added to 20 to give the output as 23 | + | |Hence, four fives are twenty and then three is added to 20 to give the output as 23. |
|- | |- | ||
− | | 03 | + | | 03:42 |
− | | | + | |Let's see some more examples based on operator precedence. |
|- | |- | ||
− | | 03 | + | | 03:47 |
− | | Let's go back to the terminal | + | | Let's go back to the terminal and |
|- | |- | ||
− | | 03 | + | | 03:50 |
− | | | + | |press ''' Crtl, L''' keys simultaneously to clear the '''irb''' console. |
|- | |- | ||
− | | 03 | + | | 03:56 |
− | | Now type '''7 minus 2 multiply by 3 ''' | + | | Now, type: '''7 minus 2 multiply by 3 ''' |
|- | |- | ||
− | | 04 | + | | 04:03 |
− | | and press '''Enter ''' | + | | and press '''Enter '''. |
|- | |- | ||
− | | 04 | + | | 04:05 |
| We get the answer as 1. | | We get the answer as 1. | ||
|- | |- | ||
− | | 04 | + | | 04:08 |
− | | Here the | + | | Here, the asterisk symbol has higher priority than the minus sign. |
− | + | ||
|- | |- | ||
− | | 04 | + | | 04:13 |
− | |So the multiplication | + | |So, the multiplication operation is performed first and then subtraction is performed. |
− | + | ||
|- | |- | ||
− | | 04 | + | | 04:20 |
− | | | + | | Let's see another example. |
|- | |- | ||
− | | 04 | + | | 04:22 |
− | | Type | + | | Type: within brackets '''10 plus 2 slash 4 ''' |
|- | |- | ||
− | | 04 | + | | 04:29 |
− | |and | + | |and press '''Enter'''. We get the answer as 3. |
|- | |- | ||
− | | | + | | 04:33 |
− | | | + | |In this case, () bracket has the higher priority than division (slash). |
|- | |- | ||
− | | 04 | + | | 04:39 |
− | | | + | | So, the operation inside the bracket, that is addition, is performed first. |
|- | |- | ||
− | | | + | | 04:44 |
− | | | + | | Then division is performed. |
|- | |- | ||
− | | 04 | + | | 04:47 |
− | | | + | | Now, let us learn about '''Relational Operators'''. |
|- | |- | ||
− | | | + | | 04:51 |
− | | | + | |Let's switch back to the slides. |
|- | |- | ||
− | | 04 | + | | 04:54 |
− | | | + | | '''Relational operator'''s are also known as '''comparison''' operators. |
|- | |- | ||
− | | 04 | + | | 04:59 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
| Expressions using relational operators return '''boolean''' values. | | Expressions using relational operators return '''boolean''' values. | ||
|- | |- | ||
− | | 05 | + | | 05:04 |
− | | | + | |Relational operators in '''Ruby''' are- |
|- | |- | ||
− | | 05 | + | | 05:07 |
− | |''' == Equals to''' | + | | ''' == Equals to''', e.g. a==b |
|- | |- | ||
− | | 05 | + | | 05:14 |
− | | | + | | ''' dot eql question mark''' e.g. a.eql?b |
|- | |- | ||
− | | 05 | + | | 05:21 |
− | | | + | | '''!= Not equals to''' e.g. '''a exclamation equals b''' |
|- | |- | ||
− | | 05 | + | | 05:28 |
− | | ''' Less than | + | | '''< Less than''' e.g. a < b |
|- | |- | ||
− | | 05 | + | | 05:32 |
− | | | + | | '''> Greater than''' e.g. a > b |
|- | |- | ||
− | | 05 | + | | 05:37 |
− | | ''' <= Lesser than or equal to | + | | ''' <= Lesser than or equal to e.g. a less than arrow equals b''' |
|- | |- | ||
− | | 05 | + | | 05:44 |
− | |''' >= Greater than or equal to | + | | ''' >= Greater than or equal to''' e.g. a greater than arrow equals b |
|- | |- | ||
− | | 05 | + | | 05:49 |
− | |''' <=> Combined comparison | + | | ''' <=> Combined comparison''' e.g. a less than arrow equal greater than arrow b. |
|- | |- | ||
− | | 05 | + | | 05:56 |
− | | | + | | Now let us try some of these operators. |
|- | |- | ||
− | | 06 | + | | 06:00 |
|Go to the terminal. | |Go to the terminal. | ||
|- | |- | ||
− | | 06 | + | | 06:02 |
− | | Press ''' ctrl, | + | | Press '''ctrl, L''' keys simultaneously to clear the '''irb''' console. |
|- | |- | ||
− | | 06 | + | | 06:09 |
− | | | + | | Let's try ''' equals to''' operator. |
|- | |- | ||
− | | 06 | + | | 06:11 |
− | |So type ''' 10 equals equals 10 ''' | + | |So, type: ''' 10 equals equals 10 ''' |
− | + | ||
− | + | ||
|- | |- | ||
− | | 06 | + | | 06:16 |
− | |and | + | |and press '''Enter'''. We get the output as "true". |
|- | |- | ||
− | | 06 | + | | 06:20 |
− | | | + | |The '''.eql?''' opeartor is same as ''' equals to''' operator. |
|- | |- | ||
− | | 06 | + | | 06:24 |
− | |''' .eql?''' | + | |Let's try it out. Now, type ''' 10 .eql?10''' and press '''Enter'''. |
|- | |- | ||
− | | 06 | + | |06:33 |
− | | | + | |We get the output as "true". |
|- | |- | ||
− | | 06 | + | | 06:35 |
− | | Now | + | |Now, let's try ''' not equal to''' operator. |
|- | |- | ||
− | |06 | + | | 06:39 |
− | | | + | | Type: ''' 10 not equal 10''' |
|- | |- | ||
− | | 06 | + | |06:44 |
− | | | + | | and press '''Enter'''. |
|- | |- | ||
− | | 06 | + | | 06:46 |
− | | | + | |We get the output as "false". |
|- | |- | ||
− | |06 | + | | 06:48 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|This is because the two numbers are equal. | |This is because the two numbers are equal. | ||
− | |||
|- | |- | ||
− | | 06 | + | | 06:51 |
|Clear the ''' irb''' console by pressing '''Ctrl, L''' simultaneously. | |Clear the ''' irb''' console by pressing '''Ctrl, L''' simultaneously. | ||
|- | |- | ||
− | | 06 | + | | 06:56 |
− | | | + | | Now, let us try ''' less than''' operator. |
|- | |- | ||
− | | 07 | + | | 07:00 |
− | |Type ''' 10 less than 5''' and | + | |Type: '''10 less than 5''' and press '''Enter'''. |
− | + | ||
|- | |- | ||
− | | 07 | + | | 07:05 |
− | | Here if first operand is less than second then it will return | + | | Here, if first operand is less than second then it will return "true". |
|- | |- | ||
− | | 07 | + | | 07:10 |
− | | | + | |Otherwise, it will return "false". |
− | + | ||
|- | |- | ||
− | | 07 | + | | 07:14 |
− | |We get the output as | + | |We get the output as "false" because 10 is not less than 5. |
|- | |- | ||
− | | 07 | + | | 07:19 |
− | | We will now try '''greater than''' operator | + | | We will now try '''greater than''' operator. |
|- | |- | ||
− | | 07 | + | | 07:22 |
− | | Type '''5 greater than 2''' | + | | Type: '''5 greater than 2'''. |
|- | |- | ||
− | | 07 | + | | 07:26 |
− | |Here if first operand is greater than second then it will return | + | |Here, if first operand is greater than second then it will return "true"; |
|- | |- | ||
− | | 07 | + | | 07:31 |
− | |otherwise it will return | + | |otherwise it will return "false". |
|- | |- | ||
− | | 07 | + | | 07:34 |
− | |Press ''' Enter''' | + | |Press ''' Enter'''. |
|- | |- | ||
− | | 07 | + | | 07:36 |
− | | In this case, we get the output as | + | | In this case, we get the output as "true" because 5 is indeed greater than 2. |
|- | |- | ||
− | | 07 | + | | 07:42 |
− | |Clear the '''irb''' console by pressing ''' Ctrl, L''' simultaneously | + | |Clear the '''irb''' console by pressing ''' Ctrl, L''' simultaneously. |
|- | |- | ||
− | | 07 | + | | 07:47 |
− | | We will now try the '''less than equal to''' operator | + | | We will now try the '''less than equal to''' operator. |
|- | |- | ||
− | | 07 | + | | 07:51 |
− | | Type '''12 less than equal | + | | Type: '''12 less than equal 12''' |
− | + | ||
|- | |- | ||
− | | 07 | + | | 07:56 |
− | |and | + | |and press '''Enter'''. |
− | + | ||
|- | |- | ||
− | | 07 | + | | 07:59 |
− | | Here if first operand is less than or equal to second then it returns | + | | Here, if first operand is less than or equal to the second then it returns "true"; |
|- | |- | ||
− | | 08 | + | | 08:04 |
− | | otherwise it returns | + | | otherwise it returns "false". |
|- | |- | ||
− | | 08 | + | | 08:07 |
− | |We get the output as | + | |We get the output as "true" because 12 is equal to 12. |
|- | |- | ||
− | | 08 | + | | 08:11 |
|You can try out the ''' greater than or equal to''' operator likewise. | |You can try out the ''' greater than or equal to''' operator likewise. | ||
|- | |- | ||
− | | 08 | + | | 08:15 |
− | |Now let's try the '''combined | + | |Now, let's try the '''combined comparison''' operator. |
|- | |- | ||
− | | 08 | + | | 08:19 |
− | |The ''' combined | + | |The ''' combined comparison''' operator: |
|- | |- | ||
− | | 08 | + | | 08:21 |
− | |Returns | + | | Returns 0 if first operand equals second |
− | + | ||
|- | |- | ||
− | | 08 | + | | 08:24 |
− | |Returns 1 if first operand is greater than the second and | + | | Returns 1 if first operand is greater than the second and |
− | + | ||
− | + | ||
|- | |- | ||
− | | 08 | + | | 08:29 |
− | |Returns -1 if first operand is less than the second operand | + | |Returns -1 if first operand is less than the second operand. |
− | + | ||
|- | |- | ||
− | | 08 | + | | 08:34 |
− | |Let's see how it works with an example | + | |Let's see how it works, with an example. |
|- | |- | ||
− | | 08 | + | | 08:36 |
− | |Type '''3 less than equals greater than 3 ''' | + | |Type: '''3 less than equals greater than 3 ''' |
|- | |- | ||
− | | 08 | + | | 08:41 |
− | | | + | |and press '''Enter'''. |
− | + | ||
|- | |- | ||
− | | 08 | + | | 08:43 |
|We get the output as 0 | |We get the output as 0 | ||
|- | |- | ||
− | | 08 | + | | 08:45 |
− | |because both the operands are equal i.e. both are three | + | |because both the operands are equal i.e. both are three. |
|- | |- | ||
− | | 08 | + | | 08:50 |
− | |Now, let's change one of the operands to 4 | + | |Now, let's change one of the operands to 4. |
|- | |- | ||
− | | 08 | + | | 08:53 |
− | |Type '''4 less than equals greater than 3 ''' | + | |Type: '''4 less than equals greater than 3 ''' |
− | + | ||
|- | |- | ||
− | | 08 | + | | 08:58 |
− | | | + | |and press '''Enter'''.We get the output as 1 |
|- | |- | ||
− | | | + | | 09:01 |
− | | | + | |since 4 is greater than 3. |
|- | |- | ||
− | | 09 | + | | 09:04 |
− | | | + | |Now, let's change this example again. |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:07 |
− | | | + | |Type: '''4 less than equals greater than 7 ''' |
|- | |- | ||
− | | 09 | + | | 09:11 |
− | | | + | |and press '''Enter '''. |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:13 |
− | | | + | |We get the output as -1. Since 4 is less than 7. |
|- | |- | ||
− | | 09 | + | | 09:17 |
− | | | + | |As an assignment, |
|- | |- | ||
− | | 09 | + | | 09:19 |
− | | | + | |solve the following examples using '''irb''' and check the output: |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:24 |
− | | | + | |''' 10 + bracket 2 asterisk 5 bracket 8 slash 2''' |
|- | |- | ||
− | | 09 | + | | 09:32 |
− | | | + | | '''4 asterisk 5 slash 2 plus 7''' |
|- | |- | ||
− | | 09 | + | | 09:37 |
− | |''' | + | | Also, try arithmetic operators using '''method'''s. |
|- | |- | ||
− | | 09 | + | | 09:42 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|This brings us to the end of this Spoken Tutorial. | |This brings us to the end of this Spoken Tutorial. | ||
− | |||
|- | |- | ||
− | | 09 | + | | 09:45 |
− | |Let's summarize | + | |Let's summarize.. |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:47 |
− | |In this tutorial we have learnt about | + | |In this tutorial, we have learnt about: |
|- | |- | ||
− | | 09 | + | | 09:49 |
− | |Arithmetic Operators plus minus | + | | Arithmetic Operators- plus, minus, asterisk, slash standing for addition, subtraction, multiplication, division. |
|- | |- | ||
− | | 09 | + | | 09:59 |
− | |Operator Precedence | + | | Operator Precedence |
|- | |- | ||
− | | 10 | + | | 10:01 |
− | |Relational Operators | + | | Relational Operators |
|- | |- | ||
− | | 10 | + | | 10:04 |
− | |using many examples | + | | using many examples. |
|- | |- | ||
− | | 10 | + | | 10:06 |
| Watch the video available at the following link. | | Watch the video available at the following link. | ||
|- | |- | ||
− | | 10 | + | | 10:10 |
− | |It | + | |It summarizes the Spoken Tutorial project. |
|- | |- | ||
− | | 10 | + | | 10:14 |
|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. | ||
|- | |- | ||
− | | 10 | + | | 10:18 |
− | | | + | | The Spoken Tutorial project team: |
|- | |- | ||
− | | 10 | + | | 10:20 |
− | |Conducts workshops using spoken tutorials | + | | Conducts workshops using spoken tutorials. |
|- | |- | ||
− | |10 | + | |10:23 |
− | |Gives certificates to those who pass an online test | + | | Gives certificates to those who pass an online test. |
|- | |- | ||
− | | 10 | + | | 10:26 |
− | |For more details, please write to contact@spoken-tutorial.org | + | |For more details, please write to: contact@spoken-tutorial.org |
|- | |- | ||
− | | 10 | + | | 10:32 |
− | | Spoken Tutorial | + | | '''Spoken Tutorial''' project is a part of the '''Talk to a Teacher''' project. |
|- | |- | ||
− | | 10 | + | | 10:36 |
|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. | ||
|- | |- | ||
− | | 10 | + | | 10:43 |
− | |More information on this | + | |More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
|- | |- | ||
− | | 10 | + | | 10:51 |
− | |This script has been contributed by the spoken tutorial team IIT Bombay | + | |This script has been contributed by the spoken tutorial team, '''IIT Bombay'''. |
|- | |- | ||
− | | 10 | + | | 10:57 |
− | |And this is Anjana Nair signing off Thank you | + | |And this is Anjana Nair, signing off. Thank you. |
− | + | ||
|} | |} |
Latest revision as of 12:11, 10 March 2017
Time | Narration |
00:01 | Welcome to the Spoken Tutorial on Arithmetic & Relational Operators in Ruby. |
00:06 | In this tutorial, we will learn about: |
00:08 | Arithmetic Operators |
00:10 | Operator Precedence |
00:12 | Relational Operators. |
00:14 | Here we are using: Ubuntu Linux version 12.04 , Ruby 1.9.3 |
00:23 | To follow this tutorial, you must know how to use Terminal and Text editor in Linux. |
00:28 | You must also be familiar with 'irb'. |
00:31 | If not, for relevant tutorials, please visit our website. |
00:34 | Now, let us learn about arithmetic operators. |
00:38 | Ruby has the following arithmetic operators. |
00:42 | '+' Addition, e.g. a+b. |
00:45 | '-' Subtraction, e.g. a-b. |
00:48 | '/' Division, e.g. a/b. |
00:51 | '*' Multiplication, e.g. a*b. |
00:55 | '%' Modulus, e.g. a%b. |
00:59 | '**' Exponent, e.g. a**b. |
01:04 | Let us try these arithmetic operators using irb. |
01:08 | Open the terminal by pressing Ctrl, Alt and T keys simultaneously. |
01:14 | A terminal window appears on your screen. |
01:17 | Type "irb" and press Enter to launch the interactive Ruby. |
01:21 | Type: 10 plus 20 and press Enter. |
01:25 | The addition operation is performed and the result 30 is displayed. |
01:31 | Similarly, the subtraction and multiplication operations can be performed. |
01:35 | Let us try the division operator. |
01:38 | Type: 10 slash 4 |
01:40 | and press Enter. |
01:42 | Here you can see the result is truncated to the nearest whole number which is 2. |
01:47 | To get a more accurate answer, we need to express one number as float. |
01:52 | Type: 10.0 slash 4 |
01:56 | and press Enter. |
01:58 | Now we get the result as 2.5. |
02:01 | Let's now try the modulus operator. |
02:05 | The modulus operator returns the remainder as output. |
02:09 | Type: 12 percentage sign 5 and press Enter. |
02:15 | Here, 12 is divided by 5 and the remainder 2 is returned back. |
02:21 | Now, let's try the exponent operator. |
02.24 | Type: 2 followed by the asterisk symbol twice and then 5 and press Enter. |
02:32 | This means that 2 is raised to the power of 5. |
02:36 | So, we get the output as 32. |
02:39 | Next, let us learn about operator precedence. |
02:44 | When several operations occur in a mathematical expression, |
02:47 | each part is evaluated |
02:50 | and resolved in a predetermined order called operator precedence. |
02:56 | This means that the operator which has the highest priority is executed first. |
03:01 | This is then followed by the next operator in the priority order and so on. |
03:07 | This slide lists all operators from highest precedence to lowest. |
03:13 | For example, 3 + 4 * 5 returns 23 and not 35. |
03:23 | The multiplication operator asterisk (*) has higher precedence than the addition operator plus (+) |
03:29 | and thus will be evaluated first. |
03:32 | Hence, four fives are twenty and then three is added to 20 to give the output as 23. |
03:42 | Let's see some more examples based on operator precedence. |
03:47 | Let's go back to the terminal and |
03:50 | press Crtl, L keys simultaneously to clear the irb console. |
03:56 | Now, type: 7 minus 2 multiply by 3 |
04:03 | and press Enter . |
04:05 | We get the answer as 1. |
04:08 | Here, the asterisk symbol has higher priority than the minus sign. |
04:13 | So, the multiplication operation is performed first and then subtraction is performed. |
04:20 | Let's see another example. |
04:22 | Type: within brackets 10 plus 2 slash 4 |
04:29 | and press Enter. We get the answer as 3. |
04:33 | In this case, () bracket has the higher priority than division (slash). |
04:39 | So, the operation inside the bracket, that is addition, is performed first. |
04:44 | Then division is performed. |
04:47 | Now, let us learn about Relational Operators. |
04:51 | Let's switch back to the slides. |
04:54 | Relational operators are also known as comparison operators. |
04:59 | Expressions using relational operators return boolean values. |
05:04 | Relational operators in Ruby are- |
05:07 | == Equals to, e.g. a==b |
05:14 | dot eql question mark e.g. a.eql?b |
05:21 | != Not equals to e.g. a exclamation equals b |
05:28 | < Less than e.g. a < b |
05:32 | > Greater than e.g. a > b |
05:37 | <= Lesser than or equal to e.g. a less than arrow equals b |
05:44 | >= Greater than or equal to e.g. a greater than arrow equals b |
05:49 | <=> Combined comparison e.g. a less than arrow equal greater than arrow b. |
05:56 | Now let us try some of these operators. |
06:00 | Go to the terminal. |
06:02 | Press ctrl, L keys simultaneously to clear the irb console. |
06:09 | Let's try equals to operator. |
06:11 | So, type: 10 equals equals 10 |
06:16 | and press Enter. We get the output as "true". |
06:20 | The .eql? opeartor is same as equals to operator. |
06:24 | Let's try it out. Now, type 10 .eql?10 and press Enter. |
06:33 | We get the output as "true". |
06:35 | Now, let's try not equal to operator. |
06:39 | Type: 10 not equal 10 |
06:44 | and press Enter. |
06:46 | We get the output as "false". |
06:48 | This is because the two numbers are equal. |
06:51 | Clear the irb console by pressing Ctrl, L simultaneously. |
06:56 | Now, let us try less than operator. |
07:00 | Type: 10 less than 5 and press Enter. |
07:05 | Here, if first operand is less than second then it will return "true". |
07:10 | Otherwise, it will return "false". |
07:14 | We get the output as "false" because 10 is not less than 5. |
07:19 | We will now try greater than operator. |
07:22 | Type: 5 greater than 2. |
07:26 | Here, if first operand is greater than second then it will return "true"; |
07:31 | otherwise it will return "false". |
07:34 | Press Enter. |
07:36 | In this case, we get the output as "true" because 5 is indeed greater than 2. |
07:42 | Clear the irb console by pressing Ctrl, L simultaneously. |
07:47 | We will now try the less than equal to operator. |
07:51 | Type: 12 less than equal 12 |
07:56 | and press Enter. |
07:59 | Here, if first operand is less than or equal to the second then it returns "true"; |
08:04 | otherwise it returns "false". |
08:07 | We get the output as "true" because 12 is equal to 12. |
08:11 | You can try out the greater than or equal to operator likewise. |
08:15 | Now, let's try the combined comparison operator. |
08:19 | The combined comparison operator: |
08:21 | Returns 0 if first operand equals second |
08:24 | Returns 1 if first operand is greater than the second and |
08:29 | Returns -1 if first operand is less than the second operand. |
08:34 | Let's see how it works, with an example. |
08:36 | Type: 3 less than equals greater than 3 |
08:41 | and press Enter. |
08:43 | We get the output as 0 |
08:45 | because both the operands are equal i.e. both are three. |
08:50 | Now, let's change one of the operands to 4. |
08:53 | Type: 4 less than equals greater than 3 |
08:58 | and press Enter.We get the output as 1 |
09:01 | since 4 is greater than 3. |
09:04 | Now, let's change this example again. |
09:07 | Type: 4 less than equals greater than 7 |
09:11 | and press Enter . |
09:13 | We get the output as -1. Since 4 is less than 7. |
09:17 | As an assignment, |
09:19 | solve the following examples using irb and check the output: |
09:24 | 10 + bracket 2 asterisk 5 bracket 8 slash 2 |
09:32 | 4 asterisk 5 slash 2 plus 7 |
09:37 | Also, try arithmetic operators using methods. |
09:42 | This brings us to the end of this Spoken Tutorial. |
09:45 | Let's summarize.. |
09:47 | In this tutorial, we have learnt about: |
09:49 | Arithmetic Operators- plus, minus, asterisk, slash standing for addition, subtraction, multiplication, division. |
09:59 | Operator Precedence |
10:01 | Relational Operators |
10:04 | using many examples. |
10:06 | Watch the video available at the following link. |
10:10 | It summarizes the Spoken Tutorial project. |
10:14 | If you do not have good bandwidth, you can download and watch it. |
10:18 | The Spoken Tutorial project team: |
10:20 | Conducts workshops using spoken tutorials. |
10:23 | Gives certificates to those who pass an online test. |
10:26 | For more details, please write to: contact@spoken-tutorial.org |
10:32 | Spoken Tutorial project is a part of the Talk to a Teacher project. |
10:36 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
10:43 | More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
10:51 | This script has been contributed by the spoken tutorial team, IIT Bombay. |
10:57 | And this is Anjana Nair, signing off. Thank you. |
Contributors and Content Editors
Devraj, PoojaMoolya, Pratik kamble, Sandhya.np14, Shruti arya