hello, i*m planning to use my arduino to power ALL my lights and a few other things in my car,
and there will be alot of if button XX and/or button XX is pressed turn on light XX and / or XX.
so alot of inout form buttons and switches and alot of output on lights.
It may depend upon what you mean by "best". Size, speed, readability, maintainability, what?
When there are a lot of possibilities, people tend to use switch/case. When there are only a few possibilities, people tend to use if/else.
Each has pros and cons. Forgetting to use "break" is a con of switch/case. Unintended grouping (especially, using a superfluous semicolon) is a con of if/else.
I suggest that you make the code work correctly first, then worry about optimization.
“Premature optimization is the root of all evil.”
― Donald Ervin Knuth, The Art of Computer Programming, Volume 1: Fundamental Algorithms
Use which ever you prefer. I don't think it will make any difference to the compiled code.
IF/ELSE is a lot more versatile.
With SWITCH/CASE the values to be tested for (i.e. the CASE values) must be known when the program is being written. IF/ELSE can test one variable against another.
I also depends upon the nature of what you're trying to do. I find a cascading if-else statement block much more difficult to read than a switch-case, especially if it is fairly long. I saw a piece of banking software that had 31 if tests, one for each day of the month and an activity targeted for that day. On average, that means 15 false tests a month. A switch-case would at least have produced a jump table and saved all but one test. An even better solution is an array of pointers-to-function for the activities and using the day of the month as the index. Again, which better solves the problem and which is easier for you to maintain?
mikki1211:
hello, i*m planning to use my arduino to power ALL my lights and a few other things in my car,
and there will be alot of if button XX and/or button XX is pressed turn on light XX and / or XX.
so alot of inout form buttons and switches and alot of output on lights.
what would be best, if else or switch case?
Classes.
If you have a lot of mostly identical things with behaviour, then what you really want is classes. Or at the very least, structs. They let you group related variables together
There's more stuff you can do - using constructors to assign the pins and so on. Se the link in my sig for an attempt at explaining how to do this kind of thing.