循环
while(条件)
{
循环体
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int i,s;
i = 1;
s = 0;
while(i <= 100)
{
s = s + i;
i = i + 1;
}
cout<<s<<endl;
return 0;
}
计算1到100的和,最后运行结果为:5050
#include<bits/stdc++.h>
using namespace std;
int main(){
int i,s;
i = 2;
s = 0;
while(i <= 1000)
{
s = s + i;
i = i + 2;
}
cout<<s<<endl;
return 0;
}
计算1到1000所有偶数的和,最后运行结果为:250500
#include<bits/stdc++.h>
using namespace std;
int main(){
int m,n,i,j;
m = 2;
n = 2;
i = (m++)%3;
j = (++n)%3;
cout<<i<<" "<<m<<endl;
cout<<j<<" "<<n<<endl;
return 0;
}
自加,m++是先运算后自加,++m是先自加再运算,运行结果:
2 3
0 3
标签:includeusing,第二课,int,C++,++,while,自加,cout From: https://www.cnblogs.com/lilyxiaoyy/p/18049753