Java/C2/Relational-Operations/English

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

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

Title of script: Relational Operators in Java

Author: TalentSprint

Keywords: boolean, comparison, relational operator, video tutorial


Visual Cue Description
Slide 1

Welcome

Welcome to the spoken tutorial on Relational Operators in Java.
Slide 2

Learning Outcomes

In this tutorial, we will learn about the


  • the boolean data type
  • Relational operators and
  • how to compare data using Relational operators.


Slide 3

Tools Used

For this tutorial we are using

Ubuntu 11.10,

JDK 1.6 and

Eclipse 3.7

Slide 4

Prerequisites

To follow this tutorial, you must have knowledge of data types in Java


If not, for relevant tutorials, please visit our

website as shown.

Slide 5

Boolean data type

Relational operators are used to check for conditions.


Their output is a variable of boolean data type


A boolean data type is of size 1 bit


It stores only two values.


True or False.


True is the output when the condition is true.


False is the output if the condition is not true.

Slide 6

Relational Operators

Here is a list of the Relational operators available.


  • greater than
  • less than
  • equal to
  • greater than or equal to
  • less than or equal to
  • not equal to

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 BooleanDemo{

public static void main(String[] args){

}

}

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


I have created a class BooleanDemo and added the Main method.


Now let us add some expressions.

In the main method, type

boolean b;

Highlight boolean b


Type boolean b;


The keyword boolean declares the data type of the variable b as boolean.


We shall store the result of our condition in b.

In the next line, type


int weight = 45;

b = weight > 40;

System.out.println(b);


Highlight whatever you are explaining.


Highlight weight=45.


Highlight the expression weight>40.


Save and Run

We shall define a variable weight and check for a condition using that variable.


int weight equal to 45;


We shall check if the value in weight is greater than 40.


b equal to weight greater than 40;


This statement says check if the value of variable is greater than 40 and store the result in b;


Now Let us print the value of b.


System dot out dot println(b);


Save and Run.

Point to output As we can see, the output is True.
Change 45 to 30 Let us see what happens if the value is less than 40.


Change weight to 30.


Save and run

We can see that the output is False as expected.
Highlight > symbol. This way, the greater than symbol is used to check if one value is greater than the other.


Similarly, less than symbol is used to check if one value is less than the other.

Change weight > 40 to weight < 40

Highlight the condition.


Save and Run

let us change greater than to less than symbol.


So We are checking if the value of weight is less than 40.


 Save  Run  
Point to output


Change 30 to 45

Save and Run

As we can see, the output is True as expected.


Let us change the value of weight to 45 and see the output.


Save and Run.

We see that we get a False because the condition,


weight less than 40 is not true.


Now let us see how to check if a value is equal to another.

Change weight < 40 to weight == 40


Save and Run.

To do that, we use two equal to symbols.


change less than symbol to double equal to.


Save and Run

As we can see, the output is False because the value of weight is not equal to 40.
Now let us change the weight to 40 and see the output.



Save and Run.

Point to output


Highlight ==

As we can see, the output is True


This way, Double equal to is used for checking equality.


Please be careful because, often people use a single equal to symbol for checking equality.


And this gives unnecessary errors.

Change weight == 40 to weight <= 40


Highlight condition

Save and Run.

Next we'll see how to check for less than or equal to.


To do that, we will use a less than symbol followed by an equal to symbol.


 change the double equal to to less than equal to.


Save and Run.

Point to output the output is True as expected.


Now let us change the value of weight to see if the less than check is performed.

Change weight = 40 to weight = 30

Save and Run. Point to output

Change 40 to 30.

Save and Run.


We see, that although the weight is not equal to 40 we get the output as True because it is less than 40.


Let us see what happens if the value of weight is greater than 40.

Change 40 to 50. Let say 50

Save and Run.

As we can see, the output is False because the value of weight is not equal to 40.


And it also not less than 40.

Similarly we use a greater than symbol followed by an equal to symbol for checking greater than or equal to.


Let us try it.

Change weight <= 40 to weight >= 40 Change less than equal to to greater than equal to


Save and Run.


As we can see, the output is true because weight is greater than 40

Change 45 to 30 Let us change weight to a value less than 40. Lets say 30.


Save and Run.

We get a false because the value of weight is not greater than 40 and also not equal to 40.
Change weight >= 40 to weight != 40

Save and Run.


Highlight b = ...

Next, we’ll see how to check for not equal to


It is done by using an exclamation mark followed by an equal to symbol.


Change greater than to exclamation mark


So this statement says check if the value of weight is not equal to 40 and store the result in b


Save and Run

Point to output As we can see, the output is true because the values of weight is not equal to 40.
Let us change the weight to 40 and see the output.

Change 30to 40.

Save. Run.

We get a false because the condition weight not equal to 40 is false.
The not equal to condition can be thought of as opposite of equal to condition.
This is how we use the various relational operators to compare data in Java.
Minimize the Eclipse window and switch to slides.


Slide 6

Summary

This brings us to the end of this tutorial.


In this tutorial we have learnt,

about the boolean data type

The relational operators and

how to use relational operators to compare two values

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,


  • It summarises the Spoken Tutorial 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.


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.


It 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