This operator is used in c++ to represent equality.

Kenneth Leroy Busbee

Overview

Assignment sets and/or re-sets the value stored in the storage location denoted by a variable name.[1] Equality is a relational operator that tests or defines the relationship between two entities.[2]

Discussion

Most control structures use a test expression that executes either selection (as in the: if then else) or iteration (as in the while; do while; or for loops) based on the truthfulness or falseness of the expression. Thus, we often talk about the Boolean expression that is controlling the structure. Within many programming languages, this expression must be a Boolean expression and is governed by a tight set of rules. However, in many programming languages, each data type can be used as a Boolean expression because each data type can be demoted into a Boolean value by using the rule/concept that zero and nothing represent false and all non-zero values represent true.

Within various languages, we have the potential added confusion of the equals symbol = as an operator that does not represent the normal math meaning of equality that we have used for most of our life. The equals symbol typically means: assignment. To get the equality concept of math we often use two equal symbols to represent the relational operator of equality. Let’s consider:

If (pig = 'y')
    Output "Pigs are good"
Else
    Output "Pigs are bad."

The test expression of the control structure will always be true because the expression is an assignment (not the relational operator of ==). It assigns the ‘y’ to the variable pig, then looks at the value in pig and determines that it is not zero; therefore the expression is true. And it will always be true and the else part will never be executed. This is not what the programmer had intended. The correct syntax for a Boolean expression is:

If (pig == 'y')
    Output "Pigs are good"
Else
    Output "Pigs are bad."

This example reminds you that you must be careful in creating your test expressions so that they are indeed a question, usually involving the relational operators. Some programming languages will generate a warning or an error when an assignment is used in a Boolean expression, and some do not.

Don’t get caught using assignment for equality.

References

  • cnx.org: Programming Fundamentals – A Modular Structured Approach using C++

Improve Article

Save Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    = operator

    The “=” is an assignment operator is used to assign the value on the right to the variable on the left.

    For example:

    a = 10;
    b = 20;
    ch = 'y';
    

    Example:

    #include

    int main()

    {

        int a = 10;

        printf("Value of a is %d\n", a);

        return 0;

    }

    == operator

    The ‘==’ operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false.
    For example:

    5==5
    
    This will return true.
    

    Example:

    #include

    int main()

    {

        int a = 10, b = 4;

        if (a == b)

            printf("a is equal to b\n");

        else

            printf("a and b are not equal\n");

        return 0;

    }

    Output:

    a and b are not equal
    

    The differences can be shown in tabular form as follows:

    ===
    It is an assignment operator. It is a relational or comparison operator.
    It is used for assigning the value to a variable. It is used for comparing two values. It returns 1 if both the values are equal otherwise returns 0.
    Constant term cannot be placed on left hand side.
    Example: 1=x; is invalid.
    Constant term can be placed in the left hand side.
    Example: 1==1 is valid and returns 1.

    What is the equality operator in C?

    Both operands of any relational or equality operator can be pointers to the same type. For the equality ( == ) and inequality ( != ) operators, the result of the comparison indicates whether the two pointers address the same memory location.

    What is == used for in C?

    The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false .

    Which type of operator == is?

    Relational Operators == (Equal to)– This operator is used to check if both operands are equal.

    What is the == operator used for?

    = Equal Sign This operator assigns a value to a variable or property. Comparison operator also used as an equal to; the result of comparison operators is usually a logical value, either true or false.