0 votes
112 views
in Discuss by (98.9k points)
edited
What is more efficient, a switch statement or an if-else chain?

1 Answer

0 votes
by (98.9k points)
selected by
 
Best answer
As far as efficiency is concerned there would hardly be any difference, if at all. If the cases in a switch are sparsely distributed the compiler may internally use the equivalent of an if-else chain instead of a compact jump table. However, one should use switch where one can. It is definitely a cleaner way to program and certainly is not any less efficient than the if-else chain.

Related questions

+1 vote
1 answer 142 views
0 votes
1 answer 92 views
0 votes
1 answer 91 views
asked Jun 11, 2022 in Discuss by nehapatil (3.2k points)
0 votes
1 answer 125 views
0 votes
1 answer 98 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

537 users

...