Java/C2/Logical-Operations/English

From Script | Spoken-Tutorial
Revision as of 13:20, 8 May 2013 by Sneha (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Logical Operators in Java

Author: TalentSprint

Keywords: boolean, comparison, logical operator, video tutorial


Visual Cue Description
Slide 1

Welcome

Welcome to this spoken tutorial on Logical Operators in Java.
Slide 2

Learning Outcomes

In this tutorial, you will learn about the logical operators

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


If not, for relevant tutorials, please visit our website as shown.

Slide 5

Logical Operators

Logical operators are used to chec k multiple conditions.


Here is a list of the available logical operators in Java

and, or, not. We shall look into each of them in detail.


Switch to Eclipse.

Minimize Slides and open Eclipse

Eclipse should contain the following code

public class LogicalOperators{

public static void main(String[] args){

}

}

Here We have the Eclipse IDE and skeleton required for the rest of the code.


We have created a class LogicalOperators and added the main method

Let us create some variables.

In the main function, type

int age = 11;

int weight = 42;

boolean b;


We shall store the result of conditions in b;


 int age is equalto 11   

int weight isequalto 42



Highlight

int age = 11;

int weight = 42;

We have the age and weight of a person.


We shall check if the person is below 18yrs of age and is atleast 40kgs.


Let us see how to do so.

Type after the previous line,

boolean b;


b = age < 18 && weight >= 40


Highlight &&


Highlight expression

b is eual toage less than 18 ampersand ampersand weight greater than equal to 40


This statement has two expressions and a double ampersand symbol in between.


It checks if age is less than 18 and also if weight is greater than or equal to 40.


This operation is called the and operation.

Put the line age... inside the statement

System.out.println();

Let us now print the value.


System dot out dot println(b);

Save and Run. Save and Run
Point to output


Change 42 to 32.


Save and Run.

As we can see, the output is true because both the conditions are satisfied.


Now let us change the weight so that one condition is not satisfied and re run the code.


Change 42 to 32.


Save and Run

Point to output the output now is false.


This is because the condition age less than 18 is satisfied


But the condition the weight is greater than or equal to 40 is not satisfied


The and operation requires both the conditions to be true for the result to be true.


Therefore we get false as our output.

This way, using a double ampersand to do an and operation.
Lets say we have age and weight and it is enough if only one of the conditions is satisfied.


In other words, we need to see if the first condition or the second condition is true.


It is done using the or operation.

Comment out the old condition and type

b = age <= 15 || weight <= 30

Let us first remove the earlier condition.


Type


age less than equal to 15 pipe pipe weight less than equal to 30


There are two conditions and a double pipe symbol in between.


This statement checks if atleast one of the given two conditions is satisfied.


Let us run the code to see the output.

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 and operation.
It needs atleast one condition to be true.
So although the condition for weight is not satisfied, since the condition for age is satisfied.
we get a true.
Now let us change the age in 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.


Point to output

change 11 to 17


Save and Run

Now the output is false because both the conditions are not satisfied.

This way, we use a double PIPE symbol to do an or operation.
Now let us say we need to check for people who are older than 15 and with weight more than 30kilos.


In other words, we need to check the exact opposite of the condition we just did.

In such situations, we use the not operation
Add parentheses around the

age... line

First enclose the condition in parentheses.
Add ! before the parentheses. And Add an exclamation mark before the condition.
By using an exclamation mark, we check for the exact opposite of the condition inside the parentheses.


Since the earlier output was false, now it must be the opposite, true. Let us see
Save and Run. Point to output Save and Run.


As we can see, the output is the opposite of earlier one.

Back to code. This way by using the Exclamation mark we perform not operation. Let us say we want people younger than 15.

Or people younger than 18 and lighter than 40kilos.


Let us see how would we go about this condition.

Remove the System... line and type

age < 15 || age < 18 && weight < 40


Highlight the condition.

Remove the earlier condition and type

age less than 15

or age less than 18

and weight less than 40


As we can see, the condition itself is confusing.


Moreover, we do not know if the or operation will be performed first or the and operation.


It depends on the precedence of operators.


In such situations, we use parentheses to overwrite the precedence and also make the condition clear.

Change age <= 18 && weight <= 40

to (age <= 18 && weight <= 40)

So let us add the parentheses.


Let us run the code save run


Save. Run.

Although the first condition age less than 15 is not satisfied,


the second condition, which is,


age less than 18 and weight less than 40 is satisfied.


Therefore, output is True.

As a rule, use parentheses to avoid ambiguity and make the expressions clear.
And This how we use the logical operators to check for multiple conditions.
Minimize the Eclipse window and switch to slides.


Slide 6

Summary

This bring us 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,


find out if the two expressions, shown are equivalent?

Slide 8About the Spoken Tutorial Project
  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


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
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test


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
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


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
  • www.talentsprint.com
  • Thanks for joining


This tutorial has been contributed by TalentSprint. Thanks for joining.



Contributors and Content Editors

Chandrika, Sneha