Difference between revisions of "Java/C2/Logical-Operations/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 9: | Line 9: | ||
|- | |- | ||
| 00:07 | | 00:07 | ||
− | | In this tutorial, you will learn about the logical operators | + | | In this tutorial, you will learn about the '''logical operators'''. |
|- | |- | ||
| 00:11 | | 00:11 | ||
− | |How to check for multiple expressions using '''logical operators''' and how to '''override''' '''precedence''' using parentheses | + | |How to check for multiple '''expressions''' using '''logical operators''' and how to '''override''' '''precedence''' using parentheses. |
|- | |- | ||
Line 27: | Line 27: | ||
|- | |- | ||
| 00:30 | | 00:30 | ||
− | | To follow this tutorial you should have knowledge on '''relational operators | + | | To follow this tutorial, you should have knowledge on '''relational operators''' in Java. |
|- | |- | ||
Line 39: | Line 39: | ||
|- | |- | ||
| 00:48 | | 00:48 | ||
− | | Here is a list of the available logical operators in Java | + | | Here is a list of the available '''logical operators''' in Java. |
|- | |- | ||
Line 47: | Line 47: | ||
|- | |- | ||
| 01:04 | | 01:04 | ||
− | | Here We have the Eclipse IDE and the skeleton required for the rest of the code. | + | | Here We have the 'Eclipse IDE' and the skeleton required for the rest of the code. |
|- | |- | ||
| 01:10 | | 01:10 | ||
− | | We have created a class '''LogicalOperators''' and added the main method | + | | We have created a class '''LogicalOperators''' and added the '''main method'''. |
|- | |- | ||
|01:15 | |01:15 | ||
− | | Let us create some variables. | + | | Let us '''create''' some '''variables'''. |
|- | |- | ||
| 01:20 | | 01:20 | ||
− | | '''boolean b ; ''' | + | | '''boolean b ;''' |
|- | |- | ||
| 01:23 | | 01:23 | ||
− | | We shall store the result of conditions in b; | + | | We shall store the result of conditions in 'b'; |
|- | |- | ||
| 01:29 | | 01:29 | ||
− | | | + | | '''int age''' is equal to '''11 ''' |
|- | |- | ||
| 01:35 | | 01:35 | ||
− | | | + | | '''int weight ''' is equal to '''42 ''' |
|- | |- | ||
Line 79: | Line 79: | ||
|- | |- | ||
| 01:46 | | 01:46 | ||
− | | We shall check if the person is below 18yrs of age and is | + | | We shall check if the person is below 18yrs of age and is at least 40kgs. |
|- | |- | ||
Line 87: | Line 87: | ||
|- | |- | ||
| 01:57 | | 01:57 | ||
− | | '''b''' is equal to'''age | + | | '''b''' is equal to '''age''' less than '''18''' ampersand ampersand '''weight''' greater than equal to '''40''' |
|- | |- | ||
| 02:19 | | 02:19 | ||
− | | This statement has two expressions and a double ampersand symbol in between. | + | | This statement has two '''expressions''' and a double ampersand symbol in between. |
|- | |- | ||
| 02:24 | | 02:24 | ||
− | | It checks if '''age''' is less than 18 and also if '''weight''' is greater than or equal to 40. | + | | It checks if '''age''' is less than '''18''' and also if '''weight''' is greater than or equal to '''40'''. |
|- | |- | ||
Line 103: | Line 103: | ||
|- | |- | ||
| 02:35 | | 02:35 | ||
− | | Let us now print the value of b. | + | | Let us now print the value of 'b'. |
|- | |- | ||
| 02:40 | | 02:40 | ||
− | | '''System '''dot '''out '''dot '''println(b);''' | + | | '''System''' dot '''out''' dot '''println(b);''' |
|- | |- | ||
| 02:48 | | 02:48 | ||
− | |'''Save '''and '''Run ''' | + | |'''Save '''and '''Run '''. |
|- | |- | ||
| 02:56 | | 02:56 | ||
| As we can see, the output is '''true''' because both the conditions are satisfied. | | As we can see, the output is '''true''' because both the conditions are satisfied. | ||
− | |||
|- | |- | ||
| 03:02 | | 03:02 | ||
− | | Now let us change the '''weight''' so that one condition is not satisfied and re run the code. | + | | Now, let us change the '''weight''' so that one condition is not satisfied and ''re-run'' the code. |
|- | |- | ||
Line 128: | Line 127: | ||
|- | |- | ||
| 03:14 | | 03:14 | ||
− | | '''Save''' and '''Run''' | + | | '''Save''' and '''Run'''. |
|- | |- | ||
| 03:21 | | 03:21 | ||
− | | | + | | The output now is '''false.''' |
|- | |- | ||
| 03:24 | | 03:24 | ||
− | | This is because the condition age | + | | This is because the condition '''age''' less than '''18''' is satisfied. |
− | + | ||
|- | |- | ||
| 03:29 | | 03:29 | ||
− | | But the condition the weight | + | | But the condition the '''weight''' greater than or equal to '''40''' is not satisfied. |
− | + | ||
|- | |- | ||
| 03:34 | | 03:34 | ||
| The '''and''' operation requires both the conditions to be '''true''' for the result to be '''true.''' | | The '''and''' operation requires both the conditions to be '''true''' for the result to be '''true.''' | ||
− | |||
|- | |- | ||
Line 159: | Line 155: | ||
|- | |- | ||
| 03:53 | | 03:53 | ||
− | | | + | | Let's say we have '''age''' and '''weight''' and it is enough if only one of the conditions is satisfied. |
− | + | ||
|- | |- | ||
| 03:59 | | 03:59 | ||
| In other words, we need to see if the first condition or the second condition is '''true.''' | | In other words, we need to see if the first condition or the second condition is '''true.''' | ||
− | |||
|- | |- | ||
Line 174: | Line 168: | ||
| 04:09 | | 04:09 | ||
| Let us first remove the earlier condition. | | Let us first remove the earlier condition. | ||
− | |||
|- | |- | ||
| 04:15 | | 04:15 | ||
− | | And | + | | And type: |
− | + | ||
|- | |- | ||
| 04:17 | | 04:17 | ||
− | | '''age | + | | '''age''' less than equal to '''15''' pipe pipe '''weight'''less than equal to '''30''' |
− | + | ||
|- | |- | ||
| 04:35 | | 04:35 | ||
− | | There are two conditions and a double pipe symbol in between. | + | | There are two conditions and a '''double pipe''' symbol in between. |
− | + | ||
|- | |- | ||
| 04:40 | | 04:40 | ||
− | | This statement checks if | + | | This statement checks if at least one of the given two conditions is satisfied. |
− | + | ||
|- | |- | ||
| 04:46 | | 04:46 | ||
− | | Let us run the code to see the output. Save and run | + | | Let us run the code to see the output. '''Save''' and '''run'''. |
|- | |- | ||
Line 206: | Line 195: | ||
|- | |- | ||
| 04:57 | | 04:57 | ||
− | | This is because | + | | This is because an '''or''' operation does not need both the conditions to be true like the '''and''' operation. |
|- | |- | ||
| 05:03 | | 05:03 | ||
− | | It needs | + | | It needs at least one condition to be '''true.''' |
|- | |- | ||
| 05:06 | | 05:06 | ||
− | | | + | | So, although the condition for weight is not satisfied, since the condition for age is satisfied. |
|- | |- | ||
| 05:13 | | 05:13 | ||
− | | | + | | We get the output as '''true.''' |
|- | |- | ||
| 05:18 | | 05:18 | ||
− | | Now let us change the age in such a way that both the conditions are false and see the result. | + | | Now, let us change the age in such a way that both the conditions are false and see the result. |
− | + | ||
|- | |- | ||
| 05:25 | | 05:25 | ||
− | | change '''11''' to '''17''' | + | | change '''11''' to '''17'''. |
− | + | ||
|- | |- | ||
| 05:30 | | 05:30 | ||
− | | '''Save''' and '''Run ''' | + | | '''Save''' and '''Run '''. |
|- | |- | ||
Line 245: | Line 232: | ||
| 05:50 | | 05:50 | ||
| Now let us say we need to check for people who are older than 15 and with weight more than 30kilos. | | Now let us say we need to check for people who are older than 15 and with weight more than 30kilos. | ||
− | |||
|- | |- | ||
Line 253: | Line 239: | ||
|- | |- | ||
| 06:03 | | 06:03 | ||
− | | In such situations, we use the '''not''' operation | + | | In such situations, we use the '''not''' operation. |
|- | |- | ||
| 06:07 | | 06:07 | ||
− | | First enclose the condition in parentheses. | + | | First, enclose the condition in parentheses. |
|- | |- | ||
| 06:17 | | 06:17 | ||
− | | And | + | | And add an exclamation mark before the condition. |
|- | |- | ||
Line 269: | Line 255: | ||
|- | |- | ||
| 06:32 | | 06:32 | ||
− | | Since the earlier output was '''false''', now it must '''true'''. Let us see | + | | Since the earlier output was '''false''', now it must be '''true'''. Let us see. |
|- | |- | ||
Line 281: | Line 267: | ||
|- | |- | ||
| 06:48 | | 06:48 | ||
− | | This way by using the | + | | This way, by using the exclamation mark we perform '''not''' operation. Now let us say we want people younger than 15. |
|- | |- | ||
Line 293: | Line 279: | ||
|- | |- | ||
| 07:07 | | 07:07 | ||
− | | Remove the earlier condition and type | + | | Remove the earlier condition and type: |
|- | |- | ||
| 07:12 | | 07:12 | ||
− | | '''age | + | | '''age''' less than '''15''' |
|- | |- | ||
| 07:15 | | 07:15 | ||
− | | ''or''''' age | + | | '''or''' '''age''' less than '''18 ''' |
|- | |- | ||
| 07:24 | | 07:24 | ||
− | | ''and''''' weight | + | | '''and''' '''weight''' less than '''40''' |
− | + | ||
|- | |- | ||
| 07:33 | | 07:33 | ||
| As we can see, the condition itself is confusing. | | As we can see, the condition itself is confusing. | ||
− | |||
|- | |- | ||
Line 319: | Line 303: | ||
|- | |- | ||
| 07:42 | | 07:42 | ||
− | | It depends on the precedence of | + | | It depends on the precedence of '''operator'''s. |
|- | |- | ||
Line 331: | Line 315: | ||
|- | |- | ||
| 08:06 | | 08:06 | ||
− | | Let us run the code, save run | + | | Let us '''run''' the code, '''save''', '''run'''. |
|- | |- | ||
| 08:13 | | 08:13 | ||
| Now Although the first condition which is age less than 15 is not satisfied, | | Now Although the first condition which is age less than 15 is not satisfied, | ||
− | |||
|- | |- | ||
| 08:20 | | 08:20 | ||
| the second condition, which is, | | the second condition, which is, | ||
− | |||
|- | |- | ||
| 08:22 | | 08:22 | ||
− | | age less than 18 and weight less than 40 is satisfied. | + | | '''age''' less than 18 and '''weight''' less than 40 is satisfied. |
− | + | ||
|- | |- | ||
| 08:27 | | 08:27 | ||
− | | Therefore, output is True. | + | | Therefore, output is '''True'''. |
|- | |- | ||
| 08:30 | | 08:30 | ||
− | | As a rule, use parentheses to avoid ambiguity and make the expressions clear. | + | | As a rule, use parentheses to avoid ambiguity and make the '''expressions''' clear. |
|- | |- | ||
| 08:36 | | 08:36 | ||
− | | | And | + | | | And this is how we use the logical operators to check for multiple conditions. |
|- | |- | ||
| 08:44 | | 08:44 | ||
− | | This | + | | This brings us to the end of this tutorial. |
|- | |- | ||
| 08:47 | | 08:47 | ||
− | | | + | | We have learnt about '''logical operators''', how to check for multiple '''expressions''' using '''logical operators '''and |
|- | |- | ||
| 08:54 | | 08:54 | ||
− | | ''' | + | | how to '''override''' the precedence using parentheses. |
|- | |- | ||
|09:00 | |09:00 | ||
|As an assignment for this tutorial, | |As an assignment for this tutorial, | ||
− | |||
|- | |- | ||
| 09:02 | | 09:02 | ||
− | | find out if the two expressions | + | | find out if the two expressions shown are equivalent? |
|- | |- | ||
| 09:10 | | 09:10 | ||
− | | To know more about the | + | | To know more about the Spoken Tutorial project, watch the video available at the following link, It summarizes the Spoken Tutorial project. |
|- | |- | ||
| 09:18 | | 09:18 | ||
− | | | + | | If you do not have good bandwidth, you can download and watch it. |
|- | |- | ||
| 09:23 | | 09:23 | ||
− | | The Spoken Tutorial Project Team | + | | The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials and gives certificates for those who pass an online test. |
|- | |- | ||
Line 399: | Line 379: | ||
|- | |- | ||
| 09:36 | | 09:36 | ||
− | | | + | | Spoken Tutorial Project is a part of the 'Talk to a Teacher' project. |
|- | |- | ||
| 09:40 | | 09:40 | ||
− | | It is supported by the | + | | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
|- | |- | ||
| 09:46 | | 09:46 | ||
− | | | + | | More information on this mission is available at the following link: '''spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro'''. |
|- | |- | ||
| 09:52 | | 09:52 | ||
|This tutorial has been contributed by '''TalentSprint'''. Thanks for joining. | |This tutorial has been contributed by '''TalentSprint'''. Thanks for joining. | ||
− | |||
− | |||
− | |||
|} | |} |
Revision as of 23:48, 22 March 2015
Time | Narration |
00:02 | Welcome to this spoken tutorial on Logical Operators in Java. |
00:07 | In this tutorial, you will learn about the logical operators. |
00:11 | How to check for multiple expressions using logical operators and how to override precedence using parentheses. |
00:20 | For this tutorial we are using
Ubuntu 11.10, JDK 1.6 and Eclipse 3.7 |
00:30 | To follow this tutorial, you should have knowledge on relational operators in Java. |
00:35 | If not, for relevant tutorials, please visit our website as shown. |
00:42 | Logical operators are used to check multiple conditions. |
00:48 | Here is a list of the available logical operators in Java. |
00:54 | and, or, not. We shall look into each of them in detail. Switch to Eclipse. |
01:04 | Here We have the 'Eclipse IDE' and the skeleton required for the rest of the code. |
01:10 | We have created a class LogicalOperators and added the main method. |
01:15 | Let us create some variables. |
01:20 | boolean b ; |
01:23 | We shall store the result of conditions in 'b'; |
01:29 | int age is equal to 11 |
01:35 | int weight is equal to 42 |
01:42 | We have the age and weight of a person. |
01:46 | We shall check if the person is below 18yrs of age and is at least 40kgs. |
01:52 | Let us see how to do so. |
01:57 | b is equal to age less than 18 ampersand ampersand weight greater than equal to 40 |
02:19 | This statement has two expressions and a double ampersand symbol in between. |
02:24 | It checks if age is less than 18 and also if weight is greater than or equal to 40. |
02:31 | This operation is called the and operation. |
02:35 | Let us now print the value of 'b'. |
02:40 | System dot out dot println(b); |
02:48 | Save and Run . |
02:56 | As we can see, the output is true because both the conditions are satisfied. |
03:02 | Now, let us change the weight so that one condition is not satisfied and re-run the code. |
03:08 | Change 42 to 32. |
03:14 | Save and Run. |
03:21 | The output now is false. |
03:24 | This is because the condition age less than 18 is satisfied. |
03:29 | But the condition the weight greater than or equal to 40 is not satisfied. |
03:34 | The and operation requires both the conditions to be true for the result to be true. |
03:39 | Therefore we get false as our output. |
03:43 | This way, using a double ampersand symbol we can perform and operation. |
03:53 | Let's say we have age and weight and it is enough if only one of the conditions is satisfied. |
03:59 | In other words, we need to see if the first condition or the second condition is true. |
04:05 | It is done using the or operation. |
04:09 | Let us first remove the earlier condition. |
04:15 | And type: |
04:17 | age less than equal to 15 pipe pipe weightless than equal to 30 |
04:35 | There are two conditions and a double pipe symbol in between. |
04:40 | This statement checks if at least one of the given two conditions is satisfied. |
04:46 | Let us run the code to see the output. Save and run. |
04:54 | We see that the output is True. |
04:57 | This is because an or operation does not need both the conditions to be true like the and operation. |
05:03 | It needs at least one condition to be true. |
05:06 | So, although the condition for weight is not satisfied, since the condition for age is satisfied. |
05:13 | We get the output as true. |
05:18 | Now, let us change the age in such a way that both the conditions are false and see the result. |
05:25 | change 11 to 17. |
05:30 | Save and Run . |
05:36 | Now the output is false because both the conditions are not satisfied. |
05:41 | This way, we use a double PIPE symbol to do an or operation. |
05:50 | Now let us say we need to check for people who are older than 15 and with weight more than 30kilos. |
05:57 | In other words, we need to check the exact opposite of the condition we just did. |
06:03 | In such situations, we use the not operation. |
06:07 | First, enclose the condition in parentheses. |
06:17 | And add an exclamation mark before the condition. |
06:25 | By using an exclamation mark, we check for the exact opposite of the condition inside the parentheses. |
06:32 | Since the earlier output was false, now it must be true. Let us see. |
06:38 | Save and Run. |
06:44 | As we can see, the output is the opposite of earlier one. |
06:48 | This way, by using the exclamation mark we perform not operation. Now let us say we want people younger than 15. |
06:58 | Or people younger than 18 and lighter than 40kilos. |
07:04 | Let us see how would we go about this condition. |
07:07 | Remove the earlier condition and type: |
07:12 | age less than 15 |
07:15 | or age less than 18 |
07:24 | and weight less than 40 |
07:33 | As we can see, the condition itself is confusing. |
07:36 | Moreover, we do not know if the or operation will be performed first or the and operation. |
07:42 | It depends on the precedence of operators. |
07:46 | In such situations, we use parentheses to overwrite the precedence and also make the condition clear. |
07:53 | So let us add the parentheses. |
08:06 | Let us run the code, save, run. |
08:13 | Now Although the first condition which is age less than 15 is not satisfied, |
08:20 | the second condition, which is, |
08:22 | age less than 18 and weight less than 40 is satisfied. |
08:27 | Therefore, output is True. |
08:30 | As a rule, use parentheses to avoid ambiguity and make the expressions clear. |
08:36 | And this is how we use the logical operators to check for multiple conditions. |
08:44 | This brings us to the end of this tutorial. |
08:47 | We have learnt about logical operators, how to check for multiple expressions using logical operators and |
08:54 | how to override the precedence using parentheses. |
09:00 | As an assignment for this tutorial, |
09:02 | find out if the two expressions shown are equivalent? |
09:10 | To know more about the Spoken Tutorial project, watch the video available at the following link, It summarizes the Spoken Tutorial project. |
09:18 | If you do not have good bandwidth, you can download and watch it. |
09:23 | The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials and gives certificates for those who pass an online test. |
09:30 | For more details, please write to contact AT spoken HYPHEN tutorial DOT org. |
09:36 | Spoken Tutorial Project is a part of the 'Talk to a Teacher' project. |
09:40 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
09:46 | More information on this mission is available at the following link: spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro. |
09:52 | This tutorial has been contributed by TalentSprint. Thanks for joining. |
Contributors and Content Editors
Gaurav, Kavita salve, PoojaMoolya, Priyacst, Sandhya.np14, Sneha