The difference between while and if

Source: Internet
Author: User

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

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.