user3759123
user3759123

Reputation: 21

Where should the most frequenctly executed C-code be placed in if-else statement.(Under if or else) And Why?

I need answer for one of the most debated C-question.

  1. Where should the most frequenctly executed C-code be placed in if-else statement.(Under if or else) ?
  2. Why?

Upvotes: 1

Views: 105

Answers (2)

Alexandru Cimpanu
Alexandru Cimpanu

Reputation: 1069

1 If you have only an if else structure it does not matter because the condition is checked anyway.

2 if you have if followed by some else ifs and the conditions inside them require function calls and/or many computations you should think of ordering your cases from most frequent to least to avoid some of the computations. Unless the priority of the checks matters!

Upvotes: 1

Thilo
Thilo

Reputation: 262534

I would 100% go for clarity of the code here. For example by avoiding double negatives (which you could get by swapping the conditions for the two branches).

Any performance difference will be totally negligable.

Upvotes: 3

Related Questions