Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我主要只是好奇,使用 if/else 和 switch 语句只是个人喜好问题,还是它们实际上工作方式不同?一个比另一个更有效吗?如果是,为什么?
如果您的案例超过四个,则 switch-case 语句更有效。你的case直接跳了。另一方面,如果您使用 if-else 语句,则所有案例都会进行比较,直到您的案例被交叉为止。