Saturday, 22 September 2012

C++ Relational and Equality Operators

Today will be talking about relational and equality operators, how they work.

Okay so let's get started. first will will run throught the different  relational and equality operators.

So relational and equality operators are symbol that are use to do comparisons and when comparison are done it evaluates as a true or a false for example 1<2 will be true because indeed 1 is less than 2. well that how it works basically not hard really. so here are some more


  • == operator means equivalent or equal to
    • (1==1) means that 1 is the equivalent of 1 or what I would say 1 is equal to 1 and this is true
    • (1==2) this will return false because 1 is not equal to 2
    • (7==6) false as well 
  • < operator means less than
    • (2 < 4) 2 is less than 4 so this is true
    • (5 < 4) this is false because 5 is not less than 4
    • (10 < 10) this is also false because 10 not less than 10
  • > operator means greater than
    • (2 > 1) is true because 2 is greater than 1
    • (1 > 2) is false because 1 in not greater than 2
    • (5 > 5) is also false because 5 is not greater than 5
  • != means not equal to or not equivalent to
    • (1 != 2) is true because 1 is not equal to 2
    • (1 != 1) this is false because 1 is equal to 1
    • (3 != 4) is also true
Here are some more complex ones
  • ((2 *2) < 2) is false because 2 x 2 = 4 and 4 is not less than 2 
  • ((2*2) == (2*2)) this is true because 2 x 2 = 4 and 4 is indeed equal to 4
  • (2 > (1*1)) this is true because 2 is greater than 1 (1 x 1 = 1)
  • (10%2 != 0) this is false because 10 mod 2 give a remainder of 0 and 0 is equal to 0
Okay I think I have bored you enough so lets use the complier now to do it.

Please note that 1 is true and 0 is false in the complier.


#include<iostream>

using namespace std;

int main ()
{
    //a simple example of how it work
    //this display 1 if true or 0 if false
    cout << (2==2)<<endl; // this is true
    cout << (1==2)<<endl<<endl<<endl; // this is false
    
    
    //some true examples 
    cout << "(2 < 4) = " <<(2<4)<<endl;
    cout << "(6 > 5) = " <<(6>5)<<endl;
    cout << "(4 == 4) = "<<(4==4)<<endl;
    cout << "(2 != 5) = " <<(2!=5)<<endl<<endl;
    
    //some false example
    cout << "(5 < 4) = " <<(5<4)<<endl;
    cout << "(4 > 5) = " <<(4>5)<<endl;
    cout << "(3 == 4) = "<<(3==4)<<endl;
    cout << "(5 != 5) = " <<(5!=5)<<endl<<endl;

    //some more complex examples
    cout << "((4 * 3) < 4) = " <<((4*3)<4)<<endl;
    cout << "((9 - 1) > (4+2)) = " <<((9-1)>(4+2))<<endl;
    cout << "((10 % 2) == 0) = "<<((10%2)==0)<<endl;
    cout << "((2 / 2) != 5) = " <<((2/2)!=5)<<endl;

    system("pause>nul");
}
Here is the output:


Please note that your calculation can be complex as you want for example (((4-1)+(5*6)) < (2*4)) this works. you can also compare things that have been compared as well  example ((4<10) == (4<4)) this works as well. So there is endless possibilities when using relational operators

So no that you guys understand how these operators work we are going to use variables with these operators now to see how we use them in the real world

#include<iostream>

using namespace std;

int main ()
{
    //declaring varibles
    int num1 = 0;
    int num2 = 0;
    
    //simple example
    cout << (num1 < num2)<<endl;
    cout << (num1 < 30)<<endl<<endl;
    
    //assign new varible to num1 and num2
    num1 = 3;
    num2 = 6;
    
    cout <<"("<<num1<<" < "<<num2<<") = "<<(num1<num2)<<endl;//num1 is 3 and 
    //num2 is 6 so 3 < 6 is true
    cout <<"("<<num2<<" > "<<num1<<") = "<<(num2>num1)<<endl;//this is true
    
    num1 = 31;
    
    cout <<"("<<num1<<" == "<<31<<") = "<<(num1==31)<<endl;//this is true
    cout <<"("<<num1<<" != ("<<num2<<"*4)) = "<<(num1!=(num2*4))<<endl;//this is true
    

    system("pause>nul");
}

And the output is shown below:

Pretty simple right? an this is the end of my post see you on at the next post and please remember to keep your mind active playing games here are some:

No comments:

Post a Comment