while语句中i--与++i问题的讨论

来源:互联网 发布:php常用插件 编辑:程序博客网 时间:2024/05/17 08:31

起初,在看到while ((lo < hi--) && (e != _elem[hi]));此语句时,不解后面 _elem[hi] 中的hi比前面lo<hi-- 中的hi小1,经过后面代码的实验,终于弄清楚了:lo<hi--中先比较,然后hi减1,再赋给后面_elem[hi]中的hi。
下面我通过几个相关代码实验来解释此问题:

#include<iostream>using namespace std;int main(){    int i = 4;    while ((i--== 4)&&(i == 3))    {        cout << i << endl;    }    system("pause");    return 0;}

运行结果为:3

#include<iostream>using namespace std;int main(){    int i = 4;    while ((i--== 4)&&(i == 4))    {        cout << i << endl;    }    system("pause");    return 0;}

没有结果输出

#include<iostream>using namespace std;int main(){    int i = 4;    while ((i == 4) && (--i==3))    {        cout << i << endl;    }    system("pause");    return 0;}

运行结果为:3

#include<iostream>using namespace std;int main(){    int i = 4;    while ((i == 4) && (i--==3))    {        cout << i << endl;    }    system("pause");    return 0;}

没有结果输出

0 0