The do while loop is also one of the C++ loops. Its general form is as follows:
do{loop body statement}while(expression);
Different from the while loop, its execution flow is that when encountering do, it first enters the loop to execute the statements in the loop body once, and then determines whether the expression in the while is true to decide whether to enter the loop for the second time. As you can see, its characteristic is that no matter whether the condition is true or not, the statements in the loop body will be executed at least once.
For example, the question "accumulation and summing within N" is a question that accumulates through a loop. We can use a do while loop to solve it. The code is as follows:
#include<iostream>usingnamespacestd;intmain(){intN,sum=0,i;cin>>N;do{sum+=i;i++;}while(i<=N);cout<<sum<<endl;return0 ;}
Note that there is a semicolon after the parentheses after while. This is different from the while loop. Remember!
You can complete it yourself first and then refer to the code.