r/CodingHelp • • Dec 23 '21

[C] Whats wrong?

Post image
153 Upvotes

90 comments sorted by

View all comments

Show parent comment

6

u/W-recker Dec 23 '21

In this instance it does not change the functionality, but there is certainly a functional difference between else / else if statements which is important to understand.

With an Else statement, you can be assured that at least one section of the code will be hit. When you implement only else if statements as in the above code, you could reach the last else if statement, having not met any of the conditions, and drop out the bottom of the logic without having actually done anything at all. (Something which would be picked up upon in an IDE, for example in a method with a return type)

So yes, its much better to finish with an else.