Java/C2/Logical-Operations/English
Title of script: Logical Operators in Java
Author: TalentSprint
Keywords: boolean, comparison, logical operator, video tutorial
Visual Cue | Description |
Slide 1
Welcome |
Welcome to the spoken tutorial on Logical Operators in Java. |
Slide 2
Learning Outcomes |
In this tutorial, you will learn about the logical operators in java and
how to check for multiple expressions using logical operators and how to overwrite precedence using parentheses |
Slide 3
Tools Used |
For this tutorial we are using
Ubuntu 11.10, JDK 1.6 and Eclipse 3.7 |
Slide 4
Prerequisites |
For this tutorial, you should have knowledge on relational operators in Java
|
Slide 5
Logical Operators |
Logical operators are used to check multiple conditions.
|
and, or, not. We shall look into each of them in detail.
| |
Minimize Slides and open Eclipse
Eclipse should contain the following code public class LogicalOperators{ public static void main(String[] args){ } } |
We have the Eclipse IDE and skeleton required for the rest of the code.
|
In the main function, type
int age = 11; int weight = 42; |
boolean b;
int weight isequalto 42 semi-colon.
|
Highlight
int age = 11; int weight = 42; |
We have the age and weight of a person.
|
Type after the previous line,
boolean b;
|
Type,
age less than 18 ampersand ampersand weight greater than equal to 40
|
Put the line age... inside the statement
System.out.println(); |
Let us now print the value.
|
Save and Run. | Save and Run the program. |
Point to output
|
As we can see, the output is true because both the conditions are satisfied.
|
Point to output | As we can see, the output is now false.
|
This way, we use a double ampersand to do an and operation. | |
Lets say we have age and weight and it is enough if one of the conditions is satisfied.
| |
Comment out the old condition and type
b = age <= 15 || weight <= 30 |
Let us first remove the earlier condition.
|
Save and Run | We see that the output is True. |
This is because, an or operation, does not need both the conditions to be true like the operation. | |
It needs atleast one condition to be true. | |
So although the condition for weight is not satisfied, the condition for age is satisfied. | |
And we get a true. | |
Now let us change the age such a way that both the conditions are false and see the result. | |
Save and Run. | Save and Run the program. |
Change 11 to 17
Save and Run.
|
Now change 11 to 17
|
This way, we use a double PIPE symbol to do an and operation. | |
Now let us say we need to check for people who are older than 15 and with weight more than 30kilos.
| |
In such situations, we use the not operation | |
Add parentheses around the
age... line |
First enclose the condition in parentheses. |
Add ! before the parentheses. | Add an exclamation mark before the condition. |
By using an exclamation mark, we check for the exact opposite of the condition inside the parentheses. | |
This operation is called the not operation | |
Since the earlier output was false, now it must be the opposite, true. | |
Save and Run. Point to output | Save and Run.
|
Back to code. | Let us say we want people younger than 15.
Or people younger than 18 and lighter than 40kilos.
|
Remove the System... line and type
age < 15 || age < 18 && weight < 40
|
Type,
age less than 15 or age less than 18 and weight less than 40
|
Change age <= 18 && weight <= 40
to (age <= 18 && weight <= 40) |
So let us add the parentheses.
|
Let us run the code.
| |
Although the first condition age less than 15 is not satisfied,
| |
As a rule, use parentheses to avoid ambiguity and make the expressions clear. | |
This how we use the logical operators to check for multiple conditions. | |
Minimize the Eclipse window and switch to slides.
Summary |
We have come to the end of this tutorial.
In this tutorial we have learnt about logical operators, how to check for multiple expressions using logical operators and how to override the precedence using parentheses. |
Slide 7Assignment | As an assignment for this tutorial,
|
Slide 8About the Spoken Tutorial Project
|
To know more about the Spoken Tutorial project, watch the video available at the following link, that summarises the project.
if you do not have good bandwidth, you can download and watch it |
Slide 9Spoken Tutorial WorkshopsThe Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team. Conducts workshops using spoken tutorials and gives certificates for those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org. |
Slide 10Acknowledgement
|
Spoken Tutorial Project is a part of the Talk to a Teacher project and is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro |
Slide 11About the contributor
|
This tutorial has been contributed by TalentSprint. Thanks for joining.
|