Difference between revisions of "Java/C2/Logical-Operations/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 17: | Line 17: | ||
|- | |- | ||
| 00:20 | | 00:20 | ||
− | | For this tutorial we are using | + | | For this tutorial we are using: |
'''Ubuntu 11.10''', | '''Ubuntu 11.10''', | ||
Line 35: | Line 35: | ||
|- | |- | ||
| 00:42 | | 00:42 | ||
− | | Logical operators are used to check '''multiple conditions'''. | + | | Logical operators are used to check for '''multiple conditions'''. |
|- | |- | ||
Line 43: | Line 43: | ||
|- | |- | ||
| 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'''. |
|- | |- | ||
Line 67: | Line 67: | ||
|- | |- | ||
| 01:29 | | 01:29 | ||
− | | | + | | '''int age''' is equal to 11; |
|- | |- | ||
| 01:35 | | 01:35 | ||
− | | '''int weight ''' is equal to | + | | '''int weight''' is equal to 42; |
|- | |- | ||
Line 87: | Line 87: | ||
|- | |- | ||
| 01:57 | | 01:57 | ||
− | | '''b''' is equal to '''age''' less than '''18''' ampersand ampersand '''weight''' greater than equal to '''40''' | + | | '''b''' is equal to '''age''' less than '''18''' ampersand ampersand '''weight''' greater than equal to '''40'''; |
|- | |- | ||
Line 131: | Line 131: | ||
|- | |- | ||
| 03:21 | | 03:21 | ||
− | | The output | + | | The output now is '''false.''' |
|- | |- | ||
| 03:24 | | 03:24 | ||
− | | This is because the condition '''age''' less than '''18''' is satisfied | + | | 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.''' |
|- | |- | ||
Line 151: | Line 151: | ||
|- | |- | ||
| 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. |
|- | |- | ||
Line 163: | Line 163: | ||
|- | |- | ||
| 04:05 | | 04:05 | ||
− | | It is done using the ''' | + | | It is done using the '''OR''' operation. |
|- | |- | ||
Line 175: | Line 175: | ||
|- | |- | ||
| 04:17 | | 04:17 | ||
− | | '''age''' less than equal to '''15''' pipe pipe '''weight'''less than equal to '''30''' | + | | '''age''' less than or equal to '''15''' pipe pipe '''weight''' less than or equal to '''30''' |
|- | |- | ||
Line 191: | Line 191: | ||
|- | |- | ||
| 04:54 | | 04:54 | ||
− | | We see that the output is ''' | + | | We see that the output is '''true.''' |
|- | |- | ||
| 04:57 | | 04:57 | ||
− | | This is because an ''' | + | | This is because an '''OR''' operation does not need both the conditions to be true like the '''AND''' operation. |
|- | |- | ||
Line 203: | Line 203: | ||
|- | |- | ||
| 05:06 | | 05:06 | ||
− | | So, although the condition for weight is not satisfied, since the condition for age is satisfied | + | | 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. |
|- | |- | ||
Line 227: | Line 227: | ||
|- | |- | ||
| 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 239: | Line 239: | ||
|- | |- | ||
| 06:03 | | 06:03 | ||
− | | In such situations, we use the ''' | + | | In such situations, we use the '''NOT''' operation. |
|- | |- | ||
Line 267: | Line 267: | ||
|- | |- | ||
| 06:48 | | 06:48 | ||
− | | This way, by using the exclamation mark we perform ''' | + | | This way, by using the exclamation mark we perform the '''NOT''' operation. Now, let us say we want people younger than 15. |
|- | |- | ||
Line 287: | Line 287: | ||
|- | |- | ||
| 07:15 | | 07:15 | ||
− | | ''' | + | | '''OR''' '''age''' less than '''18 ''' |
|- | |- | ||
| 07:24 | | 07:24 | ||
− | | ''' | + | | '''AND''' '''weight''' less than '''40'''; |
|- | |- | ||
Line 299: | Line 299: | ||
|- | |- | ||
| 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 ''' | + | | It depends on the precedence of '''operators'''. |
|- | |- | ||
Line 311: | Line 311: | ||
|- | |- | ||
| 07:53 | | 07:53 | ||
− | | | + | | So let us add the parentheses. |
|- | |- | ||
Line 319: | Line 319: | ||
|- | |- | ||
| 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 |
|- | |- | ||
Line 331: | Line 331: | ||
|- | |- | ||
| 08:27 | | 08:27 | ||
− | | Therefore, output is ''' | + | | Therefore, the output is '''true'''. |
|- | |- | ||
Line 343: | Line 343: | ||
|- | |- | ||
| 08:44 | | 08:44 | ||
− | | This brings us to the end of | + | | This brings us to the end of the tutorial. |
|- | |- | ||
Line 363: | Line 363: | ||
|- | |- | ||
| 09:10 | | 09:10 | ||
− | | To know more about the Spoken Tutorial project, watch the video available at the following link | + | | To know more about the Spoken Tutorial project, watch the video available at the following link. It summarizes the Spoken Tutorial project. |
|- | |- | ||
Line 371: | Line 371: | ||
|- | |- | ||
| 09:23 | | 09:23 | ||
− | | The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials and gives certificates | + | | The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials and gives certificates to those who pass an online test. |
|- | |- |
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