II运算符问题
j是数组下标 如果j=-1;if(j==-1||t[j]<0);我感觉没有错误 因为如果条件j==-1成立 t【j】<0就不会执行 编译也通过 但是如果执行就会出现错误 请问t【j】可以嘛?我理解有问题嘛?
[解决办法]
你的理解没有问题,比如:
if(j == -1 || t[j] < 0)
{
printf("OK\n");
}
将会输出OK,应该是另外的地方有问题。
[解决办法]
但是如果执行就会出现错误,这里理解不对,或者表达错误。
应该是j == -1 不成立的话,t[j] < 0就执行。如果他们||为真就执行if语句里面的内容,为假就跳出if语句。
[解决办法]
Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; there is
a sequence point after the evaluation of the first operand. If the first operand compares
unequal to 0, the second operand is not evaluated.
[解决办法]
#include <iostream>using namespace std;int main(){ int a[10] = {}; int j = -1; a[0] = -1; if (j == -1 || a[j] < 0) { cout<<"OK!"<<endl; }}