The difference between while and if

While is used for loop statements, and if is used for judgment and branch statements.
Since you didn't specify the procedure, I can only talk about it in general terms.
In the if statement, the commonly used format is:
if (judgment condition) {execution statement} The
above structure is just a judgment .
The combination of if and else can form a statement of branch structure, like
if (judgment condition 1) {execution statement 1}
else if (judgment condition 2) {execution statement 2}
else if (judgment condition 3) {execution statement 3 }
else if (...) {...}
else{execution statement n}
Some programs use case, which is actually equivalent to the above else if.
While generally has two forms
: do{execution statement} While (judgment condition), the execution effect is to run the execution statement first, and then perform the while condition judgment, if it meets the condition, it will return to continue executing the execution statement after do. Form a cycle.
Form 2: While (judgment condition) do {execution statement}, the judgment is made first, and the execution statement is run. After the execution statement is finished, it will automatically return to judge whether the condition in the while is met ( more than one judgment ) , if it is met , continue to run the execution statement, if it is not met, then exit the loop. The biggest similarity between

while and if statements is that they have at least one step of judgment.
The biggest difference is: IF statementAfter the execution is complete, then run the following statement. After the execution statement in While has finished running, it is necessary to continue to determine whether the condition meets the loop condition. According to the judgment condition, return to the execution statement or continue to run the following program.

Some programs retain the goto statement , put the goto statement in the execution statement of the if statement , and use it before the goto to the if statement, which can also play a loop effect, but this statement destroys the readability of the program, and most people This is not recommended. Therefore, many programs have eliminated the goto statement .

In addition, the execution statement of while should contain a variable. The change of the value of this variable can affect the judgment result in while, so that the loop can exit conditionally. It will not become an endless loop.

Guess you like

Origin blog.csdn.net/qq_37469055/article/details/102732743