Recently, when dealing with the positioning of the sequence table in the data structure, I thought of using a for loop, but the textbook wrote a while loop, so I was confused. What should I use?
The following is the specific code for the two specific cycles of positioning:
while loop
locateWhile(SeqlList L, DataType x ){ Int i=0; While(( i}
First, let’s understand what a while loop is:
When there is one and only one statement in the body of the while loop, you can put the braces Omit. There is only one judgment condition in the while loop statement, and it can be any expression. When the value of the judgment condition is true, the loop will be executed once, and the judgment condition will be tested again and the main body of the loop will be executed. Only when the judgment condition is false (false) will it jump out of the while loop.
The flow of the while loop is listed below:
(1) Before entering the while loop for the first time, an initial value must be assigned to the loop control variable (or expression).
(2) Determine whether to continue the loop according to the content of the judgment condition, if the judgment value of the condition is true (true), continue to execute the loop Body; if the conditional judgment value is false (false), it will jump out of the loop and execute other statements
(3) After executing the loop body After the statement, re-assign (increase or decrease) the loop control variable (or expression). Since the while loop does not change the content of the loop control variable (or expression) by itself, the assignment of the loop control variable in the while loop requires It is up to the designer to do it, and then return to step (2) to re-judge whether to continue the loop after completion.
for loop
locate(SeqlList L,DataType x ){ For(int i=0;ispan style="font-size:18px"> can achieve the same effect as the while loop.
Circulation difference
Through searching on the Internet, I found some unified opinions:< /p>
The for loop is used when the number of loops has been repeated, and when the loop rule is an integer, it is more convenient.
The while loop is arbitrary, as long as it is a loop, it can be solved.