The difference between while loop and do while loop is as follows:
1. The expression of the loop structure is different
while loop The expression of the structure is: while (expression) {loop body};
do while The expression of the loop structure is: do{loop body;}while (conditional expression);.
2. Different judgment methods during execution
When the while loop is executed, it will only enter the loop when the conditions are met. After entering the loop, all statements in the loop body will be executed until When the conditions are not met, jump out of the loop.
The do-while loop will run once first. After the first do loop, it will check whether the value of the conditional expression is true. The loop will exit only when the value is not true.
3. The number of executions is different
The while loop is judged first and then executed. If the judgment condition is not established, the intermediate loop body does not need to be executed.
The do-while loop is executed first and then judged. The number of executions is at least once. After executing once, it is judged whether the condition is true. If it is not true, jump out of the loop. If it is true, continue to run the loop body.
4. The order of executing the final loop body is different
The last loop body of the while loop is also in the middle loop body and is executed in the middle loop body. The loop body The conditions for whether to continue running are also in the loop body.
The do-while loop adds the final loop body to the intermediate loop body, and executes the final loop body when the intermediate loop body is executed. The condition for whether the loop body continues to run is in the final loop body.
Recommended tutorial: c language tutorial
The above is the detailed content of What is the main difference between while and do while loops in c language. For more information, please follow other related articles on the PHP Chinese website!