Why do you use > or < instead of != when increasing by 1 in the for statement?

Asked 2 years ago, Updated 2 years ago, 145 views

Why do you use > or < instead of != when increasing by 1 in the for statement?

In the chords that other people made, I've rarely seen a for statement that increases by 1.

Is there any special reason to use Method 2, not Method 1? Is it slower? Or did you implicitly promise to do so?

c c++ for-loop

2022-09-21 19:28

1 Answers

be different in speed differenceThere's no technical reason. However, writing < and > is !It limits a wider range than writing = or == 2 It's less risky to write together, and it's easier to manage and understand code.

For example, if there is a code to sleep from 11 p.m.,

while(time!= 11:00pm) { // I was busy doing something else, so I ran it at 11:01 -> You can't sleep until 11 tomorrow???
    Wake();
}

Rather than

while (time < 11:00pm) {
    Wake();
}

This code will be executed more purposefully, right?


2022-09-21 19:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.