. Net4 Expression Tree (3): loop

Source: Internet
Author: User
Preface

When I first started learning programming, I thoughtProgramThe value is far less than what I wrote for it.CodeMany, because it is faster than the average person, until I know the "loop ". One day later, I found that the program could be "endless loop", and the method I used to cope with the "endless loop" was to shut down (-.-!) Crazy sweat!

Loop

This articleArticleThis section describes the cyclic statements of Expression Tree: For, do, while . Cough, it should be loop! Yes, there is no for, do, while in the expreession tree. These familiar friends called their ancestor loop to cope with it. Just like Lu Xun's classic quote-there is no such thing as for, do, and while in the world. It's annoying for programmers to write code.

Actually, loop is similar to do. It is equivalent

Do {

If (not suitable)

Break;

}

The following statements may help you understand the above pseudo code 【Minor children's shoes jump directly to the next section]: Do it first, and break up if your character is inappropriate. From this we can judge that loop is not a specific guy.

Example

Now let's take a factorial (for example, n! = N * (n-1 )*... * 2*1.

Generally, the C # syntax is used as follows:

IntResult = 1;Do{Result * = N; n --;}While(N> 1 );

 

To facilitate the construction of the expreession tree, change the preceding statement to the pseudo-code format:

 
IntResult = 1;Do{If(N> 1) {result * = N; n --;}Else{Break;}}

 

Corresponding Expression Tree construction:

 Parameterexpression Value = Expression . Parameter ( Typeof (Int ), "Value" ); Parameterexpression Result = Expression . Parameter ( Typeof ( Int ), "Result" ); Labeltarget Label = Expression . Label ( Typeof ( Int )); Blockexpression Block = Expression . Block ( New [] {Result }, Expression . Assign (result, Expression . Constant (1 )), Expression . Loop ( Expression . Ifthenelse ( Expression . Greaterthan (value, Expression . Constant (1 )), Expression . Multiplyassign (result, Expression . Postdecrementassign (value )), Expression . Break (Label, result), label )); Expression < Func < Int , Int > Lambda = Expression . Lambda < Func < Int , Int >>( Block, value ); Func < Int , Int > Func = lambda. Compile (); Console . Writeline (func (5 ));

 

In general, the cycle of Expression Tree isExpression. Loop,Expression. Break andLabeltargetCommon components, as shown in the following format:

 
Expression. Loop (...Expression. Break (Label )..., Label)

To ensure that the "endless loop" runs out, a condition must be set to "Jump out" (for exampleBreakAndLabeltarget).

In addition,Expression. Multiplyassign (result,Expression. Postdecrementassign (value) is expressed as result * = n --; that is, result * = N; n-; and then merged into a statement.

Summary

Why is there no such common method as for in expression?

I have already written this article.ForIn my customFluentexpressionThis is probably the way it is written. We will introduce it in detail later.

 
Fluentexpression. For (N, N. Assign (2), n <= To, N. postincrementassign ()...)

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.