In-depth understanding of C language-06-Logical Control

Source: Internet
Author: User
Tags switch case

There are seven main types of Logic Control in C language:

1> goto is the most powerful, but it is generally used only in special environments.

2> if else

3>? :

4> switch case

5>

6> while

7> do while


Since dijkstra's article Go To Statement Considered Harmful, the C language code rarely sees goto. (It is generally used for troubleshooting multiple resource allocations)

However, from a computer perspective, the lack of a goto (jmp command) really cannot work. In fact, goto is the most consistent with our design flowchart.

It is also intuitive to use goto to implement a flowchart. Goto can truly let us do what our hearts are, and what the sword is.


Next, we will consider converting 2-7 to the corresponding goto language version (equivalent to converting to the corresponding assembly version ).

2 if statement

If (condition 1)

Code Block 1

Else

Code Block 2

Corresponding to goto:

T = condition 1;

If (t true) goto true;

Code Block 2

Goto finish

True:

Code Block 1

Finish:


3? : Is equivalent to if else.

Except that the if statement is a code block ,? : 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 finish;

True:

Variable = expression 1

Finish:

Note :? : The expressions in it should be as simple as possible. if the expressions are too complex, use the if statement to implement the expressions, which facilitates debugging.

4 switch-case

Switch (Conditional Variable)

Case element 1:

Statement Block 1;

Break;

Case element 2:

Statement Block 2;

Break;

...

Case element N

Statement Block N;

Break;

Default:

Processing by default.

Break;

}

Goto version:

Jump table = {tag 1, tag 2,..., tag N}

Goto jump to the table [element index]

Label 1:

Statement Block 1

Goto Finish

Label 2:

Statement Block 2

Goto Finish

...

Tag N:

Statement Block N

Goto Finish

Default:

Processing by default.

Finish:

5>

For (initialization statement; judgment statement; iteration Statement)

Loop statement Block

Goto version:

Initialization statement;

If (whether the statement is "no)

Goto Finish;

Loop:

Loop statement Block

Iteration statement

If (statement true)

Goto loop;

Finish:

6> while

While (the 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

Do {

Statement Block 1

Iteration Block 1

} While (the condition is true)

Goto version:

Loop:

Statement Block 1

Iteration Block 1

If (condition true) goto loop;

The goto in C language is logically consistent with the jmp command series in Assembly statements.


Note:

1> there is a classical Logical Algebra formula for the condition:

Morgan formula:

! (A & B) = (! A) | (! B)

We recommend that you manually draw an algebra two-dimensional table for complex logic.

Result of A B

0 0?

0 1?

1 0?

1 1?

It also ensures full coverage of the standalone test.

2> logical operations and bit operations are divided into two groups.

And

Logic: & |!

Bit: & | ~ ^ (Exclusive or)

3. We strongly recommend that you draw a complete flowchart on the paper and sort out your own design ideas before writing code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.