Deeply understand the logic control of C language
- 2020-04-02 02:21:22
- OfStack
In this paper, the logic control of C language is discussed in depth. Generally speaking, the logic control statements of C language mainly include the following 7 types:
1. Goto is the most powerful, but is only used in special environments.
2, if the else
3, & # 63; :
4, the switch case
5, for
6, while
7, do the while
Since dijkstra's paper went To Statement Considered Harmful, the C code has rarely seen goto. (typically used for error handling of multiple resource allocations)
But from a computer's point of view, the lack of a goto (JMP) instruction really doesn't work. In fact, goto is the most consistent with our design flow chart.
Goto is also the most intuitive way to implement a flow chart. Goto can really get us where the heart goes, where the sword goes.
Next, consider converting 2-7 to the corresponding goto language version (equivalent to the corresponding assembly version), respectively.
2, If statement
if ( conditions 1)
//Block 1
else
//Code block 2
Corresponding to goto:
t = conditions 1 ;
if (t Is true) goto true ;
//Code block 2
goto finish
true :
//Block 1
finish :
3, The & # 63; : is equivalent to if else .
Only if statements are code blocks, okay? : is an expression.
variable = ( conditions 1)? expression 1 Expression: 2 ;
Goto version:
t = conditions 1 ;
if (t To be true )
goto true ;
//The variable is equal to expression 2
goto finish ;
true :
//The variable is equal to expression 1
finish :
Note: The & # 63; : inside the expression as simple as possible, if too complex, use if statement to achieve, so convenient debugging.
4, The switch case
switch ( Conditional variable )
case The element 1 :
//Statement block 1;
break ;
case The element 2 :
//Statement block 2;
break ;
...
case The element N
//Block N;
break ;
default :
//Default handling.
break ;
}
Goto version:
The jump table ={ The label 1 , the label 2 . ..., The label N }
goto The jump table [ The element index]
//Label 1:
//Block 1
goto Finish
//Label 2:
//Block 2
goto Finish
...
//Label N:
//Block N
goto Finish
default :
//Default handling.
Finish:
5, For statement
for ( Initialization statement; Judgment statement; Iteration statements )
//Loop block
Goto version:
//Initialization statement;
if ( Determine if the statement is no )
goto Finish ;
loop :
//Loop block
//Iteration statements
if ( Determine if the statement is true )
goto loop ;
Finish :
6, While statement
while( Condition is true )
//The code block
//Iterative block
Corresponding to goto version:
loop :
t = conditions
if ( Is not true for ) goto Finish ;
//The code block
//Iterative block
Finish :
7, Do - while statement
do {
//Block 1
//Iterative block 1
} while ( Condition is true )
Goto version:
loop :
//Block 1
//Iterative block 1
if ( Condition is true ) goto loop ;
C language goto and assembly statement JMP series of instructions are logically identical.
Note:
1. There is a classical logical algebraic formula for the condition:
Morgan formula:
! (A && B) = (! (A) | |! B)
Suggest a table of algebraic operations for hand animation of complex logic
B A result
0 0 & # 63;
0 1 & # 63;
1 0 & # 63;
1 1 & # 63;
And ensure full coverage of unit tests.
2. Logical operations and bitwise operations are two groups that need to be distinguished.
With or against
Logic: && ||!
Bit: & | ~ ^
3. It is highly recommended to draw a complete flow chart on paper before writing code to comb your own design ideas.