In this paper, the logic control of C language to do a more in-depth discussion, generally speaking, C language Logic control statements are mainly as follows 7 kinds:
1, Goto is the most powerful, but generally only in special circumstances to use.
2, if Else
3,?:
4. Switch case
5, for
6, while
7, do While
Since Dijkstra's paper went to Statement considered harmful, the C language code rarely sees Goto. (generally used in error handling for multiple resource allocations)
But, from the computer's point of view, the lack of goto (jmp instruction) is really not working. In fact, Goto is the most consistent with our design flow chart.
It is also most intuitive to implement a flowchart with Goto. Goto can really let us do the heart of the move, the sword.
Next, consider converting 2-7 to the corresponding Goto language version (equivalent to converting to a corresponding assembly version).
2,if statement
if (condition 1)
//code block 1
Else
//code block 2
Corresponding Goto:
t = condition 1;
if (T is true) goto true;
//code block 2
goto
true:
//code block 1
finish:
3,?: And if else is equivalent .
Just the IF statement is a block of code? : is an expression.
Variable = (condition 1)? Expression 1: Expression 2;
Goto version:
t = condition 1;
if (T is true)
goto true;
//variable = expression 2
goto;
true:
//variable = expression 1
finish:
Note:?: Inside the expression as simple as possible, too complex words, use if statement to achieve, so convenient debugging.
4,switch-case
switch (conditional value variable) case
element 1:
//Statement block 1; break
, Case
element 2:
//Statement block 2;
Case element N
//statement block N; break
; default
:
//defaults processing. Break
;
Goto version:
Jump table ={tags 1, label 2, ..., tags n}
goto jump Table [element index]
//tags 1:
//Statement block 1
goto Finish
//Tags 2:
//2
goto Finish
...
Label N:
//Statement block n
Goto Finish
Default:
//defaults processing.
Finish:
5, forstatement
for (initialization statement; Judgment statement; Iteration statement)
//Loop statement block
Goto version:
initialization statement;
if (the judgment statement is no)
goto Finish;
loop:
////Loop statement block
//Iteration Statement
if (judgment statement is true)
Goto loop;
Finish:
6, whilestatement
while (condition is true)
//code block
//iteration block
Corresponding Goto version:
Loop:
t = condition
if (not true) goto finish;
//code block
//iteration block
finish:
7,do-while statement
Do {/
/Statement block 1
//Iteration block 1
} while (condition is true)
Goto version:
Loop:
//Statement block 1
//Iteration Block 1
if (condition is true) goto loop;
The C-language Goto is logically consistent with the Assembly statement JMP series instructions.
Attention:
1, on the condition, there is a classical logic algebra formula:
Morgan Formula:
! (A && B) = (! A) | | (! B
Proposed, for complex logic, hand animation algebraic table of operations
A B Results
0 0?
0 1?
1 0?
1 1?
and ensure the full coverage of the monomer test.
2, logical operation and bit operation are 2 groups, need to distinguish.
With or against
Logic: && | | !
Bit: & | ~ ^ (XOR or)
3 strongly recommend before writing code, on paper to draw a complete flowchart, comb their own design ideas.