A little difference and discussion of cycle structure in C ++Object Pascal

zhaozj2021-02-11  214

A little difference and discussion of cycle structure in C / Object Pascal

Although in all programming languages, the execution of the program is not a few structures such as sequence, branches and loops, such as if ... Else, Switch ... case, while and do ... while, and IF in Pascal ... Then ... else, case ... of, while ... do, repeat ... Until, almost one, the corresponding relationship, only have a small difference in grammar. This kind of similarity makes us more prone to learning other languages ​​after mastering a language. However, the difference between the language is tasty trivial, if you don't pay attention to these differences, they will never know which corner will be jumped soon, let the programmer caught the nightmare of Debug.

Let's take an example. We know that in a general loop structure, the FO loop and the While cycle can be converted, for example, in Pascal

For i: = startvalue to endValue do ...;

Can be converted into a corresponding While loop:

I: = startValue;

While i <= endvalue do begin

...;

INC (I);

END;

It seems that these two cycles are exactly the same. Don't be busy, is it true? Here is a Delphi code, you can first count, what is the result of being seen:

Procedure TFORM1.FormCreate (Sender: TOBJECT);

VAR

I, J: Integer;

Index: Integer;

Str: string;

Begin

I: = 0;

J: = 3;

For index: = 0 to j do begin

INC (I);

IF j <5 kiln inc;

END;

Str: = INTOSTR (i);

I: = 0;

J: = 3;

INDEX: = 0;

While Index <= j do begin

INC (I);

IF (j <5) THEN INC (J);

INC;

END;

Caption: = Str ',' INTOSTR (i);

END;

Although the two cycles look completely different, the results after running are completely different: one is 4, one is 6.

We are not busy discussing the reasons, then see how the situation is in the C language.

Void ctestdlg :: ONLBUTTONDOWN (uint nflags, cpoint point)

{

INT I, J;

Int index;

i = 0;

J = 3;

For (INDEX = 0; index <= j; index )

{

i ;

IF (j <5) j ;

}

CString Str;

Str.Format ("% d", i);

SetwindowText (STR);

CDIALOG :: ONLBUTTONDOWN (NFLAGS, POINT);

}

Void ctestdlg :: OnRButtondown (uint nflags, cpoint point)

{

INT I, J;

Int index;

i = 0;

J = 3;

INDEX = 0;

While (INDEX <= j)

{

i ;

IF (j <5) J ; Index ;

}

CString Str;

Str.Format ("% d", i);

SetwindowText (STR);

CDIALOG :: OnRbuttondown (NFLAGS, POIN);

}

What is the answer? In Visual C , both results are 6.

In order to exclude the impact of optimization on the results of the program, I run an unopened program and optimized programs in Delphi and Visual C . The result is the same: the results of the two cycles in Delphi are 4 and 6, while Visual In C , the two cycles are 6.

I was tested in the environment of Win98 SE, Delphi 6.0 and Visual C 6.0. If you are interested, you can also change the VB, Java or C Builder or other environments to test the result.

It seems that I can get the answer now. The reason why two results in Delphi will be different because we change the value of the variable J in the cycle, and J is used as a condition for the test cycle. We can draw this conclusion:

1. In Object Pascal, the FOR cycle and the WHILE loop are different from how the cycles are terminated. The FOR cycle will not re-evaluate the termination condition after each cycle, that is, any for loop has begun, the total number of cycles has been determined, even if you want to change. And the number of executions of the While cycle is uncertain, you can change the number of cycles at any time in the cyclic body.

2. Since the processing method is different, it is also different in performance efficiency. This has been specified in Object Pascal Language Guide. Below is a paragraph of the fourth chapter "Syntax Elements" in this book:

The difference between this construction and the for ... to statement is that the while loop re-evaluates finalValue before each iteration. This can result in noticeably slower performance if finalValue is a complex expression, and it also means that changes to the value of FinalValue Withnin Statement CAN Affect Execution of The Loop.

It can be seen that since each cycle re-evaluates the cycle condition, the execution efficiency of the While cycle is significantly lower than for. Therefore, how can it be possible, try to use the for loop as much as possible. In order to improve efficiency, the cycle conditions should be as simple as possible, and constants can be used in a constant.

3. In C language (strictly said, the above situation is only verified in Visual C ; in other C environments such as C Builder and GCC, the processing method of the For and While loops is consistent: at each cycle Before you re-ask.

In terms of efficiency, if the variable is used in the cycle condition, it is very quite, especially the cycle condition is a more complex expression, then the FOR cycle in C should be lower than the for cycle efficiency than the for loop in Object Pascal. (It is only theoretical).

4. I know the performance of the for and the while loop in different languages, we know how to avoid some errors when writing cycles. For example, the following code will not have problems in Delphi: VAR

SL: TSTRINGLIST;

I: integer;

Begin

SL: = TSTRINGLIST.CREATE;

sl.Add ('1');

Sl.Add ('2');

For i: = 0 to sl.count do sl .add ('temp string');

In VC, such code will make the program entered the dead cycle:

CStringArray SA;

Sa.Add ("1 ');

Sa.Add ("2");

For (int i = 0; i

Sa.Add ("Temp String");

On my machine, this program finally popped up a "insufficient" dialog, and almost all the running programs is slow, it seems to be turtle. I hope that this unpleasant phenomenon will not be seen in your program.

Postscript: Although this tiny difference in the for loop and While cycle is not interested, a rigorous programmer should know these details. Some of the programmers are obsessed with small TIPs that make their own programs, but many things on the programming language itself are ignored. With many non-class fans join the programming team, some bad and unregulated program habits and the top-ended practice are spread, and there are very few people who are not affected. If I am not being 啃 Object Pascal Language Guide, it will not be noted in such a small place, and I will not think that I have hidden some things that are not known for the Pascal language that I am familiar with. This is true of C , and people who care about How to Put A Icon Into The System Tray seem to be more than people who care about why the class shouth a Virtual Destructor. Basic skills are not afraid of being deep, just afraid too shallow. My hope is that for the so-called XX days, I am going to be my proficiency, and everyone still looks like a wonderful, spend some time, calm down to read some real technical information, more Strong, although there is a lot of time to read the scalp, it is, but as long as it is serious, your level is absolutely different.

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

New Post(0)