Home » Programming Tips & Tricks » C - Tips & Tricks

Why we should use switch instead of if else?

By: IncludeHelp, on 06 FEB 2017

This is a common question from the programmers, what should we use, if else or switch statement in C programming language?

If there are few conditions both (if else and switch) may take same time to reach a particular condition. But when there are more conditions to check, we should use switch case, in the switch case statement program's control reach to the particular case and does not take time like if else statement to check the conditions one by one.

switch case is much better, if we are reducing program’s execution time.

Benefits of using switch rather that if else

  • Easy to debug
  • Easy to read and understand -programmer can easily understand the code written with the particular cases as it is divided into separate cases.
  • Easy to maintain
  • Faster execution
  • Easy to verify all switch case values

(Reference: https://en.wikipedia.org/wiki/Switch_statement)

It is faster because of optimized case matching technique which is maintained through index branch table.

Read the paragraph from below image
(Source: https://en.wikipedia.org/wiki/Switch_statement )

why switch is fater than if else in c language

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions

We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.