Difference between revisions of "Java/C2/Logical-Operations/English"
(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 | + | | 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 | + | | 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 | + | | 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 | + | 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: | ||
− | + | '''''int age''' is equalto '''11 ''' | |
− | '''''int weight '''isequalto '''42 ' | + | '''''int weight '''isequalto '''42 ''' |
Line 128: | Line 127: | ||
'''Highlight expression''' | '''Highlight expression''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| | + | | 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: | ||
− | + | 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 ''' | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save '''and '''Run ''' |
|- | |- | ||
Line 171: | Line 170: | ||
− | '''Save''' and '''Run''' | + | '''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;"| | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| the output now is false. |
− | This is because the condition age | + | This is because the condition age less than 18 is satisfied |
− | But the condition the weight is | + | 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, | + | | 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;"| | + | | 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;"| | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| change '''11''' to '''17''' |
− | '''Save''' and '''Run ''' | + | '''Save''' and '''Run ''' |
− | + | Now the output is false because both the conditions are not satisfied. | |
− | Now the output is false | + | |
|- | |- | ||
| 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 ''' | + | | 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.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;"| | + | | 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. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |||
+ | |||
|- | |- | ||
| 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;"| | + | | 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
|
Slide 5
Logical Operators |
Logical operators are used to chec k 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){ } } |
Here We have the Eclipse IDE and skeleton required for the rest of the code.
Let us create some variables. |
In the main function, type
int age = 11; int weight = 42; |
boolean 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.
|
Type after the previous line,
boolean b;
|
b is eual toage 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 |
Point to output
|
As we can see, the output is true because both the conditions are satisfied.
|
Point to output | the output now is false.
|
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.
| |
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 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.
|
change 11 to 17
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 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.
|
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.
|
Remove the System... line and type
age < 15 || age < 18 && weight < 40
|
Remove the earlier condition and 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 save run
| |
Although the first condition age less than 15 is not satisfied,
| |
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.
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,
|
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.
|