Home > Back-end >  The while loop can completely replace the do while loop? So, where is the meaning of the do while lo
The while loop can completely replace the do while loop? So, where is the meaning of the do while lo

Time:09-25

While 1:
# the statement
If exit the loop condition:
Break

The above statement is not under any circumstances can replace the do while loop?

CodePudding user response:

While this cycle is doing before, to determine a condition
Do while is finished after the cycle, whether a condition
That is to say the do while anyway once the content
And while not necessarily

If from the structure, both can through writing and logical processing, entirely replace each other
How to choose, the key is to see what kind of writing more helpful in their understanding of this logic, which more convenient method in the current situation

CodePudding user response:

Examples of such as your main building, the reverse is also true

Do {
If exit the loop condition:
break;
# the statement
} the while (true);

Can completely replace the while

CodePudding user response:

Even for loop, in particular can substitute while writing and do while

Corresponds to the do while form:
For (;; ) {
# the statement
If exit the loop condition:
break;
}

While more in the form of:
For (;; ) {
If exit the loop condition:
break;
# the statement
}

CodePudding user response:

Do will perform at least once while
While probably won't be executed at a time

CodePudding user response:

The
reference 4 floor sotondolphin reply:
do while will perform at least one
While may execute a wouldn't

That the fundamental significance is that first execute judgment loop condition again, or judgment cycle conditions to perform, may distinguish according to different things

CodePudding user response:

Meaning: make the couple of higher learning cost,

Several cycle can be mutual transformation, only taste, see the problem scenario will have different choice,

CodePudding user response:

Programming is a lot of writing can be mutual transformation, mainly to see your business logic with which one is more appropriate

CodePudding user response:

The
refer to the original poster baidu_38592776 response:
while 1:
# the statement
If exit the loop condition:
Break

The above statement is not under any circumstances can replace the do while loop?


Mainly do sometimes while program more concise, look for example
 
Int I=0
Do {
i++;
} while (I!=0);

If applying the while to realize, look wrote two i++
 
int i=0;
i++;
While (I!=0) {
i++;
}



CodePudding user response:

Do what you can do while, while also can do, but in some cases, to do while it will be more easy to use, for example, there is such a demand: open a shop, in the customer must eat the apple, at least eat a, to have enough to eat, don't eat not to go (the forward!!!!! ), the code is as follows:
 
Do {
Eat an apple;
} while (not eat);

This code should be able to understand it, came in, whether you satisfied or not satisfied, you must eat a, ask you full not full, full cannot eat more, will then eat is not satisfied, if use the while to realize can also, just some trouble, as follows:
 
Boolean is the first time to eat=true;
While (is the first time to eat | | didn't eat satisfied) {
Eat an apple;
Is the first time to eat=false;
}

See, while also can realize the same function, but the code is more, also not so simple!!
  • Related