Load and view the file named compares.c for many examples of compare statements in C.
main( ) /* this file will illustrate logical compares */
{
int x = 11,y = 11,z = 11;
char a = 40,b = 40,c = 40;
float r = 12.987,s = 12.987,t = 12.987;
/* First group of compare statements */
if (x == y) z = -13; /* This will set z = -13 */
if (x > z) a = ‘A’; /* This will set a = 65 */
if (!(x > z)) a = ‘B’; /* This will change nothing */
if (b <= c) r = 0.0; /* This will set r = 0.0 */
if (r != s) t = c/2; /* This will set t = 20 */
/* Second group of compare statements */
if (x = (r != s)) z = 1000; /* This will set x = some positive
number and z = 1000 */
if (x = y) z = 222; /* This sets x = y, and z = 222 */
if (x != 0) z = 333; /* This sets z = 333 */
if (x) z = 444; /* This sets z = 444 */
/* Third group of compare statements */
x = y = z = 77;
if ((x == y) && (x == 77)) z = 33; /* This sets z = 33 */
if ((x >y) || (z > 12)) z = 22; /* This sets z = 22 */
if ((x && y && z) z = 11; /* This sets z = 11 */
if ((x = 1) && (y = 2) && (z = 3)) r = 12.00; /* This sets
x = 1, y = 2, z = 3, r = 12 .00 */
if ((x == 2) && (y = 3) && (z = 4)) r = 14.56; /* This doesn’t
change anything */
/* Fourth group of compares */
if (x == x); z = 27.345; /* z always gets changed */
if (x != x) z = 27.345; /* Nothing gets changed */
if (x = 0) z = 27.345; /* This sets x = 0, z is unchanged */
}
Webegin by defining and initializing nine variables to use in the following compare statements. This initialization is new to you and can be used to initialize variables while they are defined. The first group of compare statements represents the simplest kinds of compares since they simply compare two variables. Either variable could be replaced with a constant and still be valid compare, but two variables is the general case. The first compare checks to see if "x" is equal to "y" and it uses the double equal sign for the comparison. A single equal sign could be used here but it would have a different meaning as we will see shortly. The second comparison checks to see if "x" is greater than "z".
The third introduces the "NOT" operator, the exclamation, which can be used to invert the result of any logical compare. The fourth checks for "b" less than or equal to "c", and the last checks for "r" not equal to "s". As we learned in the last chapter, if the result of the compare is true, the statement following the "if" clause will be executed and the results are given in the comments. Note that "less than" and "greater than or equal to" are also available, but are not illustrated here. It would be well to mention the different format used for the "if" statement in this example program. Acarriage return is not required as a statement separator and by putting the conditional readability of the overall program.
main( ) /* this file will illustrate logical compares */
{
int x = 11,y = 11,z = 11;
char a = 40,b = 40,c = 40;
float r = 12.987,s = 12.987,t = 12.987;
/* First group of compare statements */
if (x == y) z = -13; /* This will set z = -13 */
if (x > z) a = ‘A’; /* This will set a = 65 */
if (!(x > z)) a = ‘B’; /* This will change nothing */
if (b <= c) r = 0.0; /* This will set r = 0.0 */
if (r != s) t = c/2; /* This will set t = 20 */
/* Second group of compare statements */
if (x = (r != s)) z = 1000; /* This will set x = some positive
number and z = 1000 */
if (x = y) z = 222; /* This sets x = y, and z = 222 */
if (x != 0) z = 333; /* This sets z = 333 */
if (x) z = 444; /* This sets z = 444 */
/* Third group of compare statements */
x = y = z = 77;
if ((x == y) && (x == 77)) z = 33; /* This sets z = 33 */
if ((x >y) || (z > 12)) z = 22; /* This sets z = 22 */
if ((x && y && z) z = 11; /* This sets z = 11 */
if ((x = 1) && (y = 2) && (z = 3)) r = 12.00; /* This sets
x = 1, y = 2, z = 3, r = 12 .00 */
if ((x == 2) && (y = 3) && (z = 4)) r = 14.56; /* This doesn’t
change anything */
/* Fourth group of compares */
if (x == x); z = 27.345; /* z always gets changed */
if (x != x) z = 27.345; /* Nothing gets changed */
if (x = 0) z = 27.345; /* This sets x = 0, z is unchanged */
}
Webegin by defining and initializing nine variables to use in the following compare statements. This initialization is new to you and can be used to initialize variables while they are defined. The first group of compare statements represents the simplest kinds of compares since they simply compare two variables. Either variable could be replaced with a constant and still be valid compare, but two variables is the general case. The first compare checks to see if "x" is equal to "y" and it uses the double equal sign for the comparison. A single equal sign could be used here but it would have a different meaning as we will see shortly. The second comparison checks to see if "x" is greater than "z".
The third introduces the "NOT" operator, the exclamation, which can be used to invert the result of any logical compare. The fourth checks for "b" less than or equal to "c", and the last checks for "r" not equal to "s". As we learned in the last chapter, if the result of the compare is true, the statement following the "if" clause will be executed and the results are given in the comments. Note that "less than" and "greater than or equal to" are also available, but are not illustrated here. It would be well to mention the different format used for the "if" statement in this example program. Acarriage return is not required as a statement separator and by putting the conditional readability of the overall program.