Why do university teachers like to write constants to the left of = = operator

Time:2020-12-12

Of course, writing constants on the left or right of = = operator has no effect on the operation itself. They are only two different writing styles. So I can guarantee that many people will not really change their inherent style just because they see some posts declaring that they should put constants on the left. In the final analysis, they are just habits.

 

 

However, writing a constant to the left of the operator may make it easier to find errors in some cases. Consider the following example:

 

Segment one, if and only if

flag == 5

The contents in the loop body are executed when:

int

flag;

If you want to see more information, please look at the pictures below. There are free open source projects and courses for you to watch!

 

while (flag == 5)

{

/* do something */

}

Fragment two, which knocks out a =, will cause an endless loop because

flag = 5

It is always true. When there is a large amount of code, it is not easy to debug such errors

int

flag;

 

 

while (flag = 5)

{

/* do something */

}

In Section 3, the constant is placed on the left. Even if a = sign is missing, it is safe. The compiler will tell you that there is an error:

int

flag;

 

 

while (5 = flag)

{

/* do something */

}

In one sentence:

 

 

Good writing style allows bugs to show up at compile time rather than at run time.