Difference between revisions of "Java/C2/Logical-Operations/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Logical Operators in Java '''Author''': TalentSprint '''Keywords: '''boolean, comparison, logical operator, video tutorial {| style="border-spacing:0;…')
 
 
Line 15: Line 15:
  
 
'''Welcome'''
 
'''Welcome'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Logical Operators''' in Java.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Welcome to this  spoken tutorial on '''Logical Operators''' in Java.
  
 
|-
 
|-
Line 21: Line 21:
  
 
'''Learning Outcomes'''
 
'''Learning Outcomes'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| In this tutorial, you will learn about the''' logical operators in java''' and
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| 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'''
 
'''how to check for multiple expressions using logical operators''' and '''how to overwrite precedence using parentheses'''
Line 50: Line 50:
  
 
'''Logical Operators'''
 
'''Logical Operators'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Logical operators are used to check '''multiple conditions'''.  
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Logical operators are used to chec k '''multiple conditions'''.  
  
  
Line 74: Line 74:
  
 
'''}'''
 
'''}'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| We have the Eclipse IDE and skeleton required for the rest of the code.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| 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 to it.
+
We have created a class '''LogicalOperators''' and added the main method  
 
+
  
 
Let us create some variables.
 
Let us create some variables.
Line 94: Line 93:
  
  
Type '''''int age''' is equalto '''11 '''semi-colon.''
+
  '''''int age''' is equalto '''11 '''
  
'''''int weight '''isequalto '''42 '''semi-colon.''
+
'''''int weight '''isequalto '''42 '''
  
  
Line 128: Line 127:
  
 
'''Highlight expression'''
 
'''Highlight expression'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Type,
+
| style="border:0.035cm solid #000000;padding:0.097cm;"|  
  
'''age''' ''less than '''''18''' ''ampersand ampersand'' '''weight''' ''greater than equal to'' '''40'''
+
'''b''' is eual to'''age''' ''less than '''''18''' ''ampersand ampersand'' '''weight''' ''greater than equal to'' '''40'''
  
  
Line 136: Line 135:
  
  
This condition 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 152: Line 151:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run'''.
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run'''.
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save '''and '''Run '''the program.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save '''and '''Run '''  
  
 
|-
 
|-
Line 171: Line 170:
  
  
'''Save''' and '''Run''' the program.
+
'''Save''' and '''Run'''
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to output
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to output
| style="border:0.035cm solid #000000;padding:0.176cm;"| As we can see, the output is now false.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| the output now is false.
  
  
This is because the condition age is less than 18 is true.
+
This is because the condition age   less than 18 is satisfied
  
  
But the condition the weight is not greater than or equal to 40 is false.
+
But the condition the weight is   greater than or equal to 40 is not satisfied
  
  
Line 191: Line 190:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
| style="border:0.035cm solid #000000;padding:0.176cm;"| This way, we use a double ampersand to do an '''and''' operation.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| This way, using a double ampersand to do an '''and''' operation.
  
 
|-
 
|-
Line 199: Line 198:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"|  
 
| style="border:0.035cm solid #000000;padding:0.097cm;"|  
| style="border:0.035cm solid #000000;padding:0.097cm;"| Lets say we have age and weight and it is enough if one of the conditions is satisfied.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Lets say we have age and weight and it is enough if only one of the conditions is satisfied.
  
  
Line 234: Line 233:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
| style="border:0.035cm solid #000000;padding:0.176cm;"| This is because, an '''or''' operation, does not need both the conditions to be true like the operation.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| This is because, an '''or''' operation, does not need both the conditions to be true like the and operation.
  
 
|-
 
|-
Line 242: Line 241:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
| style="border:0.035cm solid #000000;padding:0.176cm;"| So although the condition for weight is not satisfied, the condition for age is satisfied.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| So although the condition for weight is not satisfied, since the condition for age is satisfied.
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
| style="border:0.035cm solid #000000;padding:0.176cm;"| And we get a true.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"|   we get a true.
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"|  
 
| style="border:0.035cm solid #000000;padding:0.097cm;"|  
| style="border:0.035cm solid #000000;padding:0.097cm;"| Now let us change the age such a way that both the conditions are false and see the result.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Now let us change the age in  such a way that both the conditions are false and see the result.
  
 
|-
 
|-
Line 263: Line 262:
  
 
Point to output
 
Point to output
| style="border:0.035cm solid #000000;padding:0.097cm;"| Now change '''11''' to '''17'''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"|   change '''11''' to '''17'''
  
  
'''Save''' and '''Run '''the program.
+
'''Save''' and '''Run '''  
  
 
+
Now the output is false because both the conditions are not satisfied.
Now the output is false since both the conditions are not satisfied.
+
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
| style="border:0.035cm solid #000000;padding:0.176cm;"| This way, we use a double PIPE symbol to do an '''and''' operation.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| This way, we use a double PIPE symbol to do an '''or''' operation.
  
 
|-
 
|-
Line 294: Line 292:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Add '''!''' before the parentheses.
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Add '''!''' before the parentheses.
| style="border:0.035cm solid #000000;padding:0.176cm;"| Add an exclamation mark before the condition.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"|And  Add an exclamation mark before the condition.
  
 
|-
 
|-
Line 300: Line 298:
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| By using an exclamation mark, we check for the exact opposite of the condition inside the parentheses.
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| By using an exclamation mark, we check for the exact opposite of the condition inside the parentheses.
  
|-
+
| style="border:0.035cm solid #000000;padding:0.176cm;"|
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| This operation is called the '''not''' operation
+
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"|  
 
| style="border:0.035cm solid #000000;padding:0.097cm;"|  
| style="border:0.035cm solid #000000;padding:0.097cm;"| Since the earlier output was '''false''', now it must be the opposite, '''true'''.  
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Since the earlier output was '''false''', now it must be the opposite, '''true'''. Let us see
  
 
|-
 
|-
Line 317: Line 313:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Back to code.
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| Back to code.
| style="border:0.035cm solid #000000;padding:0.097cm;"| Let us say we want people '''younger than 15.'''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| 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'''.
 
Or people '''younger than 18''' and '''lighter than 40kilos'''.
Line 331: Line 327:
  
 
'''Highlight the condition.'''
 
'''Highlight the condition.'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Type,
+
| style="border:0.035cm solid #000000;padding:0.097cm;"|Remove the earlier condition and type 
  
 
'''age '''''less than '''''15 '''
 
'''age '''''less than '''''15 '''
Line 355: Line 351:
  
 
to '''(age<nowiki> <= 18 && </nowiki>weight<nowiki> <= 40)</nowiki>'''
 
to '''(age<nowiki> <= 18 && </nowiki>weight<nowiki> <= 40)</nowiki>'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| So let us add the parentheses.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| So let us add the parentheses.  
 
+
 
+
Now the condition is clear.
+
 
+
  
We also make sure that the '''and''' operation is performed first and then the '''or''' operation.
 
  
 +
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
| style="border:0.035cm solid #000000;padding:0.176cm;"| Let us run the code.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| Let us run the code save run
  
  
Line 389: Line 381:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
| style="border:0.035cm solid #000000;padding:0.176cm;"| This how we use the logical operators to check for multiple conditions.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| And  This how we use the logical operators to check for multiple conditions.
  
 
|-
 
|-
Line 398: Line 390:
  
 
'''Summary'''
 
'''Summary'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| We have come to the end of this tutorial.  
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| 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  
 
In this tutorial we have learnt '''about logical operators''', how to '''check for multiple expressions''' using '''logical operators '''and  

Latest revision as of 13:20, 8 May 2013

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