在这里写两个程序、下面打出来的是1-99,和0
#include <iostream>
using namespace std;
int main()
{
int n = 100;
while(--n){
cout<<n;
}
cout<<endl;
cout<<n;
return 0;
}
下面输出的数据是0-99
#include <iostream>
using namespace std;
int main()
{
int n = 100;
while(n--){
cout<<n<<endl;
}
cout<<n;
return 0;
}
归结原因就是--n先执行--,当n此时是1,在循环里面,先执行-- 操作之后就是0,while循环不是真,不会进入循环打印不出0;但是后面直接cout会cout 0 ,因为此时是0;
n--就是后执行--,当n=1的时候,while内部循环满足为真,而后再执行-1操作,会进入循环,打印出0这个数。,此时循环还没跳出,还会执行while n = 0的时候,然而在执行while里面的时候,不满足,n=-1,会cout出来。
#include <iostream>
using namespace std;
int main(){
int x = 0, y = 5, z = 3;
while(++x<5 && z-->0 ){
cout<<z<<endl;
y= y - 1;
}
printf("%d, %d, %d\n", x, y, z);
return 0;
}
/*输出2
1
0
4, 2, -1*/
while里面也是先执行前面后执行后面如果例如
如果while(z-->0&&++x<5) z--不满足大于0了,后面就不会执行,直接跳出来
#include <iostream>
using namespace std;
int main(){
int x = 0, y = 5, z = 3;
while(z-->0 && ++x<5){
cout<<z<<endl;
y= y - 1;
}
printf("%d, %d, %d\n", x, y, z);
return 0;
}
/*输出
2
1
0
3, 2, -1*/