I encountered a question in competitive programming and the solution for that include a for loop with a syntax like
for(int i = 0; i < m and n; i++){
//Do Something
}
When I changed the condition from i < m and n to i < m and submitted the solution it was giving TLE whereas in the i < m and n condition the Solution was Accepted.
What is the use of i < m and n condition if the loop runs only m times? And I don't feel like m would exceed n (if the answer is it looks for both the variables in an and comparison). What is the impact in time Complexities?
According to the C++ Standard (for example C++ 14 Standard4,section 12 Boolean conversions)
So this expression
is equivalent to
that is the same as
or if to make it more clear
andin the expressipn is an alternative representation of the logical AND operator&&.It seems that within the body of the loop (or before the loop) the variable
ncan be changed and as a result can be equal to0. In this case the loop will be interrupted even ifiis less thanm.