While is used for looping statements, and if for judging and branching statements.
Since you do not specify what the procedure is, only the generalities.
In the If statement, the common format is:
if (judging condition) {EXECUTE statement}
The structure above,just a judgment .。
If is combined with else, it can form a statement of a branching structure, as
if (judging condition 1) {EXECUTE statement 1}
else if (judge condition 2) {EXECUTE statement 2}
else if (judge condition 3) {EXECUTE Statement 3}
else if (...) {...}
else{EXECUTE statement N}
Some programs use case, which is actually equivalent to the else if.
While there are generally two forms of
Form 1:do{executes the statement} while (judging condition), the execution effect is to run the execution statement first, then the while condition judgment, if the condition is met, then return to execute the execution statement after doing, thus forming a loop.
Form 2:while (judging condition) do{EXECUTE statement}, first judgment, and run execution statement. The execution statement finishes running,Auto-Return continue to determine if the condition in while is compliant ( more than once ), if applicable, continue running execution statement, do not match, then exit Loop.
The most common point of the while and if statements is that there is at least one step in the judgment.
The biggest difference is that after theif statement is finished, run the following statement. When the execution of the statement in the while is complete, it is also to continue to determine whether the condition conforms to the cyclic condition, according to the conditions of judgment, return to execute the statement or continue to run the following program.
Some programs retain the Goto statement, put the goto statement in the EXECUTE statement of the IF statement, and can be used to goto the IF statement before the loop effect, but this statement destroys the readability of the program, most people do not advocate this, so many programs have canceled the GOTO statement.
In addition, in the EXECUTE statement of the while, you should include a variable that changes the value of the variable, which can affect the result of the judgment in the while, so that the loop conditionally exits. Not become a cycle of death.
Baidu knows
The difference between while and if