mlam13
mlam13

Reputation: 227

If statement without the if?

I'm seeing code that looks like it's trying to do an if statement without the actual if, and I'm confused what it is actually doing. For example,

int* value;
int thisValue = 0;
int checkedValue = 1;
*value = thisValue == checkedValue

Is this actually doing an if statement without the if?

Upvotes: 2

Views: 237

Answers (3)

R Sahu
R Sahu

Reputation: 206567

No, this is not doing an if statement. It's just evalating 'thisValue == checkedValue' and assigning it to the variable on the left hand side of the = operator.

int isTrue = thisValue == checkedVaue;

That could be used later in an if statement.

if ( isTrue )
{
 ...
}

That would be equivalent to

if ( thisValue == checkedVaue )
{
 ...
}

It could also be used in a while statement.

while ( isTrue )
{
 ...
}

Upvotes: 1

Utkan Gezer
Utkan Gezer

Reputation: 3069

You seem to have misunderstood the concept of the if conditioned statements.

Usage of the operators like ==, >, !=, etc. aren't restricted with the brackets after an if or a while. They do an operation, just like any other operator does. For example;

        // evaluates into...
2 + 3;  // 5
9 / 4;  // 2
7;      // 7
5 == 5; // 1
4 != 5; // 1
9 < 1;  // 0

And so on... Boolean operators like equalities and inequalities evaluate into either 0 or 1. They evaluate into 1 if true, and 0 if false.

Every value that is not 0, is interpreted as true by the C language. Every non-zero value is true. Although every non-zero value is true, the result from a boolean operation is strictly either zero or one, not any other number.

Just like you can use these things outside the if parenthesis, you can use other things inside your if parenthesis, like this:

if ( 2 + 3 ) {
    // this block will happen
    // because 2 + 3 is 5
    // and 5 is non-zero
}

if ( 10 - 6 - 4 ) {
    // this block won't happen
    // because 10 - 6 - 4 is zero
}

So, yeah... One last example:

6 && 4; // evaluates to 1
0 && 4; // evaluates to 0

Upvotes: 3

alecbz
alecbz

Reputation: 6488

This is just evaluating a boolean expression. and storing it at whatever memory location value points to (in your specific snippet value isn't allocated so the behavior is undefined).

An if allows you to execute different code based on the value of a boolean expression.

For instance,

if (a == b) {
  printf("a and b are the same\n");
}

Behaves the same as:

int same = (a == b);
if (same) {
  printf("a and b are the same\n");
}

Though the first is generally cleaner unless you have some reason to want the same variable around after the if statement. But the second is closer to the machine instructions that the C code will generate when compiled.

Even though boolean comparison operators like == are often used inside of if statements, there's nothing that forces them to be used only there. You can use the == operator anywhere in your code to generate a boolean expression, just like you can use the + operator anywhere to generate a numeric expression.

Upvotes: 4

Related Questions