From Script | Spoken-Tutorial
Jump to: navigation, search
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