Typing = instead of == make unwanted assignment inside a condition. For example, consider the scenario below (this example is in C).
CASE A:
int g=1;
if ( g == 3 )
{
printf("g is 3");
}
else
{
printf("g is not 3");
}
//this return: "g is not 3"
CASE B: (typo: missing = inside condition)
int g=1;
if ( g = 3 )
{
printf("g is 3");
}
else
{
printf("g is not 3");
}
//this return: "g is 3"
Both the cases are formally correct, so the code will work but not as we want; and may be hard to debug.
How to prevent this situation?
Aucun commentaire:
Enregistrer un commentaire