Difference between revisions of "Java/C2/Logical-Operations/English-timed"
From Script | Spoken-Tutorial
Kavita salve (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{| border=1 | {| border=1 | ||
− | || ''Time''' | + | || '''Time''' |
|| '''Narration''' | || '''Narration''' | ||
+ | |||
|- | |- | ||
| 00:02 | | 00:02 | ||
Line 8: | 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. |
|- | |- | ||
| 00:20 | | 00:20 | ||
− | | For this tutorial we are using | + | | For this tutorial we are using: |
'''Ubuntu 11.10''', | '''Ubuntu 11.10''', | ||
Line 26: | 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 33: | Line 34: | ||
|- | |- | ||
− | | 00: | + | | 00:42 |
− | | Logical operators are used to check '''multiple conditions'''. | + | | Logical operators are used to check for '''multiple conditions'''. |
|- | |- | ||
| 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. |
|- | |- | ||
| 00:54 | | 00:54 | ||
− | | and, or, not. We shall look into each of them in detail. Switch to Eclipse. | + | | '''and, or, not'''. We shall look into each of them in detail. Switch to '''Eclipse'''. |
|- | |- | ||
| 01:04 | | 01:04 | ||
− | | Here | + | | 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; |
− | |||
|- | |- | ||
| 01:42 | | 01:42 | ||
Line 78: | Line 79: | ||
|- | |- | ||
| 01:46 | | 01:46 | ||
− | | We shall check if the person | + | | 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 106: | Line 103: | ||
|- | |- | ||
| 02:35 | | 02:35 | ||
− | | Let us now print the value b. | + | | Let us now print the value of 'b'. |
− | + | ||
|- | |- | ||
| 02:40 | | 02:40 | ||
− | | | + | | '''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. |
− | + | ||
|- | |- | ||
| 03:08 | | 03:08 | ||
| Change '''42''' to '''32.''' | | Change '''42''' to '''32.''' | ||
− | |||
|- | |- | ||
| 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 '''weight''' greater than or equal to '''40''' is not satisfied. |
− | + | ||
|- | |- | ||
| 03:34 | | 03:34 | ||
− | | The ''' | + | | The '''AND''' operation requires both the conditions to be '''true''' for the result to be '''true.''' |
− | + | ||
|- | |- | ||
| 03:39 | | 03:39 | ||
− | | Therefore we get false as our output. | + | | Therefore we get '''false''' as our output. |
|- | |- | ||
| 03:43 | | 03:43 | ||
− | | This way, using a double ampersand symbol we can perform ''' | + | | This way, using a double ampersand symbol we can perform an '''AND''' operation. |
|- | |- | ||
| 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.''' | ||
− | |||
|- | |- | ||
| 04:05 | | 04:05 | ||
− | | It is done using the ''' | + | | It is done using the '''OR''' operation. |
|- | |- | ||
| 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 or equal to '''15''' pipe pipe '''weight''' less than or 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'''. |
|- | |- | ||
| 04:54 | | 04:54 | ||
− | | We see that the output is | + | | We see that the output is '''true.''' |
|- | |- | ||
| 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 '''. |
|- | |- | ||
| 05:36 | | 05:36 | ||
− | | Now the output is false because both the conditions are not satisfied. | + | | Now the output is '''false''' because both the conditions are not satisfied. |
|- | |- | ||
| 05:41 | | 05:41 | ||
− | | This way, we use a double PIPE symbol to do an ''' | + | | This way, we use a double PIPE symbol to do an '''OR''' operation. |
|- | |- | ||
| 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 260: | Line 239: | ||
|- | |- | ||
| 06:03 | | 06:03 | ||
− | | In such situations, we use the ''' | + | | 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. |
|- | |- | ||
| 06:25 | | 06:25 | ||
| By using an exclamation mark, we check for the exact opposite of the condition inside the parentheses. | | By using an exclamation mark, we check for the exact opposite of the condition inside the parentheses. | ||
− | |||
− | |||
|- | |- | ||
| 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. |
|- | |- | ||
| 06:38 | | 06:38 | ||
| '''Save''' and '''Run'''. | | '''Save''' and '''Run'''. | ||
− | |||
|- | |- | ||
Line 291: | Line 267: | ||
|- | |- | ||
| 06:48 | | 06:48 | ||
− | | This way by using the | + | | This way, by using the exclamation mark we perform the '''NOT''' operation. Now, let us say we want people younger than 15. |
|- | |- | ||
| 06:58 | | 06:58 | ||
− | | Or people | + | | Or people younger than 18 and lighter than 40kilos. |
− | + | ||
|- | |- | ||
Line 304: | 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''' less than '''18 ''' |
|- | |- | ||
| 07:24 | | 07:24 | ||
− | | '' | + | | '''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. | ||
− | |||
|- | |- | ||
| 07:36 | | 07:36 | ||
− | | Moreover, we do not know if the ''' | + | | Moreover, we do not know if the '''OR''' operation will be performed first or the '''AND''' operation. |
− | + | ||
|- | |- | ||
| 07:42 | | 07:42 | ||
− | | It depends on the precedence of operators. | + | | It depends on the precedence of '''operators'''. |
− | + | ||
|- | |- | ||
Line 340: | Line 311: | ||
|- | |- | ||
| 07:53 | | 07:53 | ||
− | | | + | | So let us add the parentheses. |
− | + | ||
− | + | ||
|- | |- | ||
| 08:06 | | 08:06 | ||
− | | Let us run the code save run | + | | Let us '''run''' the code, '''save''', '''run'''. |
+ | |||
|- | |- | ||
| 08:13 | | 08:13 | ||
− | | Now | + | | Now, although the first condition which is '''age''' less than 15 is not satisfied, |
− | + | ||
|- | |- | ||
| 08:20 | | 08:20 | ||
− | | the second condition | + | | 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 | + | | Therefore, the 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 the 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 to those who pass an online test. |
|- | |- | ||
Line 413: | 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. | ||
− | |||
− | |||
− | |||
|} | |} |
Latest revision as of 23:47, 5 April 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 for 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 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 an 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 or equal to 15 pipe pipe weight less than or 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 the 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, the 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 the 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 to 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