C++ 易混淆细节总结

来源:互联网 发布:php 星期几 编辑:程序博客网 时间:2024/06/10 22:50

1.while (n- -) 与 while (- -n)

while (n- -) 可看成是先判断n,在把n减减后执行循环体中的内容

#include <iostream>using namespace std;int main(){    int n;    cin >> n;    cout << endl;    while (n--) {        cout << n << endl;    }    cout << endl;    cout << n << endl;    return 0;}

运行结果:

6  //输入65  //输出43210-1


while (- -n) 可看成是先把n减减,在判断n,然后让n执行循环体中的内容

#include <iostream>using namespace std;int main(){    int n;    cin >> n;    cout << endl;    while (--n) {        cout << n << endl;    }    cout << endl;    cout << n << endl;    return 0;}

运行结果:

6  //输入65  //输出43210