Showing posts with label relational operators in c. Show all posts
Showing posts with label relational operators in c. Show all posts

Sunday, 6 October 2013

Relational Operators in C++

Relational Operators In C++:

To make decisions in a C++ program, you must be able to compare conditions. So, in this C tutorial for beginners we’re going to discuss relational c operators. For example, if we want to see who has the highest, average and lowest marks in some class then we need to develop such algorithm which would require comparing marks of students so, we are going to need relational c operators for that.

In C++, a condition is represented by a logical expression. It would be either true or false. And true and false are Boolean values.

Consider:

            i>j        // i and j have some int values stored in them
It is a logical expression so, it would either have a true value or a false. “ > ” is a relational c operator. And relational operators allow you to make comparisons in a C++ program.
C++ includes six relational operators.

Operator                Description

  1. ==                      equal to
  2. !=                       not equal to
  3. <                        less than
  4. >                        greater than
  5. <=                      less than or equal to
  6. >=                      greater than or equal to

Note:

 “ ==  “ and “ =  “ are different signs. “ ==  “ is a double equal sign known as equality operator ad used to check equality of operands whereas “ = “ is known as assignment operator.

Expression            Result

  1. 1 < 10                true
  2. 2 == 2                true
  3. 4 > 11                 false
  4. 6.1 <= 7.9           true
  5. 5.8 >= 7.3           false

Relational operators in c can also be used to compare char values.

For example:

‘R’ > ‘T’            False
‘+’ < ‘*’             False
‘A’ <= ‘a’           False

Relational operators in c can also be used to compare strings.
  • string str1 = “Hello”
  • string str2 = “Hi”
  • string str3 = “Hello”
  • string str4 = “Air”

  • str1 < str2         true
  • str1 == str3       true
  • str1 > str 2        false
  • str < “An”          true

Final words:

 Relational operators are very important in C++ programming. They are the fundamental tools for comparing different things. In this C tutorial for beginners they have been discussed in detail with examples.