Contact FutureLearn for Support
Skip main navigation
We use cookies to give you a better experience, if that’s ok you can close this message and carry on browsing. For more info read our cookies policy.
We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.

Equality, conditional and relational operators

These operators are used in conditional statements to test values against certain conditions. We’ll see more examples of their use in Week 3.

Operator Symbol
Equal to ==
Not equal to !=
Greater than >
Greater than or equal >=
Less than <
Less than or equal to <=

Function: Equal to

Symbol: ==

Example:

boolean myValue1;
boolean myValue2;

int mBallX = 100;
int mBallY = 100;  
if (mBallX == mBallY)
  myValue1 = true;
else
  myValue1 = false;

if (mBallX == 0)
  myValue2 = true;
  

Notes: mBallX == mBallY compares mBallX to mBallY and checks if they are equal. Similarly, the statement mBallX == 0 compares to see whether mBallX is equal to 0.


Function: Not equal to

Symbol: !=

Example:

boolean myValue1;

int mBallX = 100;
int mBallY = 100;  
if (mBallX != mBallY)
  myValue1 = true;
else
  myValue1 = false;

Notes: Similar to checking equality, this operator checks for inequality. Here because mBallX and mBallY are both 100 and equal, the condition fails; that is the result of mBallX != mBallY is false.


Function: Greater than

Symbol: >

Example:

int mBallX = 100;
int mBallY = 100;  
if (mBallX > mBallY)
  myValue1 = true;
else
  myValue1 = false;

Notes: mBallX > mBallY will return false because mBallX is not greater than mBallY. That is 100 is not greater than 100 (it is equal). If we checked mBallX > 0 this will be true because we are checking 100 > 0 in the statement.


Function: Greater than or equal to

Symbol: >=

Example:

int mBallX = 100;
int mBallY = 100;  
if (mBallX >= mBallY)
  myValue1 = true;
else
  myValue1 = false;

Notes: mBallX >= mBallY will be true because 100 >= 100


Function: Less than

Symbol: <

Example:

int mBallX = 0;
int mBallY = 100;  
if (mBallX < mBallY)
  myValue1 = true;
else
  myValue1 = false;

Notes: mBallX < mBallY will be true because we are checking 0 < 100.


Function: Less than or equal to

Symbol: <=

Example:

 
int mBallX = 0;
int mBallY = 1;  
if (mBallX <= mBallY)
   myValue1 = true;
else
   myValue1 = false;

Notes: Here mBallX <= mBallY will be true because we are comparing 0 <= 1.

Share this article:

This article is from the free online course:

Begin Programming: Build Your First Mobile Game

University of Reading