Differences between for and while

Source: Internet
Author: User
Which of the following statements about the for loop and while loop is true? ()
A. operations that can be implemented by the while loop can also be implemented by the for loop.
B. While loop judgment conditions are generally program results, and for loop judgment conditions are generally non-program results.
C. Both loops can be replaced at any time
D. Both cycle structures must have a cycle body. The cycle body cannot be blank.

 

My understanding is:
When you know the number of executions, the
When a conditional loop is used
For example:
For I = 0, I <n, I ++
{}
Here N is a known number, which can be recycled n times.

While I <n
{}
I do not know how many cycles are required, but when I> = N, the loop is stopped.

The do while loop is always executed once, for is not necessarily,

In general, for is used more for beginners than while, but in fact the while function is greater than for. In the future, you will gradually understand that all for loops can be expressed by while, however, not all while loops can be expressed using the for loop!

The use of while is sometimes more clever than
Because while has the do... While variant, it executes a loop body unconditionally and then determines whether it meets or exceeds the cycle termination condition.
But the specific purpose is to look at the personal programming style.
For example:
If it is a regular loop, for example, simple and exhaustive, we use
If it is used to process complex loops and does not apply to the loops where single-loop variables are steadily incrementing

In fact, we can also say that the for loop is used to represent the obvious loop structure, that is, to use it to indicate the beginning and end of the loop easily, so this is the case for beginners. While while is used to indicate some relatively complex loop structures. Generally, while is not easy to see the loop header and tail. When there are many complicated loops that cannot be expressed using the for loop, it can only be expressed by a while loop.

A For Loop is usually used to determine the number of cycles, while a while loop is used to determine the number of times.
For example, the function that calculates the character below cannot use:

Int strcount (char * sz)
{
Int COUNT = 0;
While (* (SZ ++ ))
Count ++;
Return count;
}

No obvious difference
If you know the obvious regional boundaries

If you do not know the boundary, you can determine whether the condition is true or not.
While (XXX. Next ())
{
}
At this time, I don't know how many objects exist, but can return true or false.

1. Differences between the two loops in the construction of an infinite loop
When an infinite loop is constructed using the while clause, the while (true) clause is generally used to construct an infinite loop. When the for clause is used to construct an infinite loop, the for (;) clause is used to construct an infinite loop. The difference between the two endless loops is that the conditions in the while loop are regarded as expressions. Therefore, when the while loop is used to construct an endless loop, true is actually regarded as a true expression forever, this situation is prone to confusion. Some tool software such as PC-Lint will think that an error has occurred. Therefore, it is best to use for (;) to construct an endless loop.
2. Differences between the two types of loops in normal Loops
When looping an array, it is generally easier to use the for loop if the variable increases only after the loop is processed; if you want to increase the number of loop variables in the Process of loop processing, it is more convenient to use the while loop. Also, when using the for loop statement, if the loop condition inside is very long, you can consider using the while loop to make the code layout look better.

 

For statements that require unconditional loops, The for statement is preferred.

While in variable loop, the for statement has a jump operation after initialization of the variable, while does not, this should be related to the VC Compiler

It can be said that there should be no significant difference in efficiency, and further operations on large data volumes are required

At the same time, I am not very clear about the timing Association of the Operation Sequence of various statements.

2. batch data operation experiment-Self-added times, and then reduced by times, depending on the execution time

It is found that there is no difference in execution time between tens of millions of data records.

 

Summary:

The main consideration is the readability and beauty of the program. For efficiency issues, you do not need to consider differences.

So I highly recommend that you use for when the number of cycles is determined, while ~~

 

This is some of the differences between for and while extracted from the Internet. you can express your own views, preferably with a basis. (Welcome to discuss)

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.