I recently wrote an implementation for a simple linked list, and at several points in my code it looks like
Node* current_node = head;
while (current_node != nullptr) {
if (current_node->data == query) {
// perform some action
break;
}
current_node = current_node->next;
}
And I just recently thought I could re-implement this as
for (Node* current_node = head; current_node != nullptr; current_node = current_node->next) {
if (current_node->data == query) {
// perform some action
break;
}
}
I know that both are syntactically correct, and that any performance differences should be negligible, but I was wondering if having an equality condition in the check is generally implemented in a for-loop? Previously I have only used inequalities (ex: >, <, etc.) in for-loops. Which version is more conventional/readable?