If it is executed even with the condition being false

6

I have an excerpt of a function with the following code:

printf("caminho: %d\n", t);
printf("min: %d\n", min);
if( min >= t && t != -1);
{
    printf("oi\n");
    min = t;
    printf("min: %d\n",min);
}

and as a result I have:

  

path: 2

     

min: 1

     

oi

     

min: 2

The line that prints "hi" is executed, even with the conditional being false.

Can anyone tell me how this can happen?

    
asked by anonymous 04.04.2016 / 21:07

1 answer

16

You have a semicolon in front of the condition of your if :

if( min >= t && t != -1);

Regardless of the outcome, nothing will happen.

Then you have a loose block:

{
    printf("oi\n");    
    min = t;
    printf("min: %d\n",min);
}

It will run anyway.

    
04.04.2016 / 21:24