9 programming habits of successful software developers 3

zhaozj2021-02-08  267

4. Some people who do not mess with the IF statement, I like to use the "if" statement, as follows:

IF (a == 0)

{

A ;

Return (a);

}

IF (a == 1)

{

A = 5;

Return (a);

}

IF (a == 2)

{

A = 10;

Return (a);

}

IF (a == 3)

{

A = 20;

Return (a);

}

IF (a == 4)

Exit (1);

Is there a better way than this? ELSE IF statement? Not. A good way is to write a simple program with the "Switch-Case" statement:

Switch (a)

{

Case 0: A ;

Return (a);

Case 1: A = 5;

Return (a);

Case 2: A = 10;

Return (a);

Case 3: A = 20;

Return (a);

DEFAULT: EXIT (1);

}

If no value consistent with A, the job defined in default is executed, and the above example is to perform the end.

转载请注明原文地址:https://www.9cbs.com/read-3266.html

New Post(0)