Current line 26: (j<ARRAY_SIZE && getline(fin, code[j]))
The code works fine; however, if I change line 26 to ( getline(fin, code[j]) && j<ARRAY_SIZE) the code stop working.
Can someone explain why?
Thank you.
This is the result of short-circuit evaluation. In C++ (and many other languages), the "&&" operator will check the first boolean expression and then if and only if it evaluates to true, check the second boolean expression.
So, if you check that j is in range before using it (ie, j<ARRAY_SIZE && getline(fin, code[j]))), you do not get an out of bounds error. If you do it the other way, on the last iteration of the loop j will be out of bounds and getline(fin, code[j]) will throw an error and cause the program to stop.
EDIT: Short-circuit evaluation does not apply solely to the && operator, but the logic is the same for others. So, in the case of the below, B will only be evaluated if A is false. If A is true, then the || operator will return true regardless of the state of B so there is no reason to evaluate B. if(A || B) { // Do the sterfs }...