Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a technical reason to use > (<) instead of != when incrementing by 1 in a 'for' loop?

Tags:

c++

c

for-loop

People also ask

Should I use i ++ or ++ i in for loop?

Both increment the number, but ++i increments the number before the current expression is evaluted, whereas i++ increments the number after the expression is evaluated. To answer the actual question, however, they're essentially identical within the context of typical for loop usage.

How do you stop a loop from incrementing?

If so, it's an easy fix: just remove the incrementing statement that runs at the end of every loop iteration. That way you're left with only the one increment line in the if statement. By the way, do note that there's no point in using a for loop that only ever has one iteration.

Can a for loop work without an Incrementer?

Also for loops have an option of initializing the variable. Thus no need to write any incrementing/decrementing step inside the loop body like we do in while and do… while loops.

Can we use for loop without increment or decrement?

Yes, one can do that. However, for such a use case, the additional syntactic elements of the for loop serve no purpose at all. A while loop will be cleaner.


while (time != 6:30pm) {
    Work();
}

It is 6:31pm... Damn, now my next chance to go home is tomorrow! :)

This to show that the stronger restriction mitigates risks and is probably more intuitive to understand.


There is no technical reason. But there is mitigation of risk, maintainability and better understanding of code.

< or > are stronger restrictions than != and fulfill the exact same purpose in most cases (I'd even say in all practical cases).

There is duplicate question here; and one interesting answer.


Yes there is a reason. If you write a (plain old index based) for loop like this

for (int i = a; i < b; ++i){}

then it works as expected for any values of a and b (ie zero iterations when a > b instead of infinite if you had used i == b;).

On the other hand, for iterators you'd write

for (auto it = begin; it != end; ++it) 

because any iterator should implement an operator!=, but not for every iterator it is possible to provide an operator<.

Also range-based for loops

for (auto e : v)

are not just fancy sugar, but they measurably reduce the chances to write wrong code.


You can have something like

for(int i = 0; i<5; ++i){
    ...
    if(...) i++;
    ...
}

If your loop variable is written by the inner code, the i!=5 might not break that loop. This is safer to check for inequality.

Edit about readability. The inequality form is way more frequently used. Therefore, this is very fast to read as there is nothing special to understand (brain load is reduced because the task is common). So it's cool for the readers to make use of these habits.


And last but not least, this is called defensive programming, meaning to always take the strongest case to avoid current and future errors influencing the program.

The only case where defensive programming is not needed is where states have been proven by pre- and post-conditions (but then, proving this is the most defensive of all programming).