57
Views
7
Comments
Solved
which one is faster using switch or using multple if coditions

which of these faster using switch or using multiple if conditions

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP
Solution

Hi Priya,

This doesn't matter at all. Regardless of what everyone else says here, OutSystems Switches are basically a list of Ifs. It is true that in some high-code languages like C# or Java, a Switch can be more efficient than an If. But in these languages, switches can only be done on one variable, and the various branches are taken based on the variable's value.

However, in OutSystems, the conditions for each Switch branch can contain any condition, so is very much like an If. I haven't checked the actual generated code, but I'm pretty sure that it can't use the optimizations that languages have for actual switches.

So, the only matter that is of concern, is code readibility. I find that have more than a few switch branches can make the code become pretty cluttered, vs. a series of Ifs that look a lot cleaner. Example:


2019-01-07 16-04-16
Siya
 
MVP
Solution

I agree with @Kilian Hekhuis . I have checked the JavaScript generated for a Client Action, and it is a chain of if statements.

OutSystems Switch constructs can’t use the same optimizations as switch statements in C# or Java because they handle complex, multi-variable conditions instead of single-variable comparisons. This flexibility makes them more like if-else chains, which prevents the use of efficient optimizations like jump tables.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Thanks Siya for the technical confirmation.

2019-01-07 16-04-16
Siya
 
MVP
Solution

I agree with @Kilian Hekhuis . I have checked the JavaScript generated for a Client Action, and it is a chain of if statements.

OutSystems Switch constructs can’t use the same optimizations as switch statements in C# or Java because they handle complex, multi-variable conditions instead of single-variable comparisons. This flexibility makes them more like if-else chains, which prevents the use of efficient optimizations like jump tables.

2024-06-24 04-49-49
Princi

Hello @Priya Naveen 

This is completely depend on the number of condition you are checking. 

If you have small number of conditions like less than 5 then you can go for the multiple if else. Or if the conditions are more than 5 then I suggest you to use switch.

Switch statement are generally faster as compare to the multiple if-else.

Regards

Prince

2025-07-28 06-45-20
Rupesh Patil
AI Generated

Hello @Priya Naveen 

A switch statement is typically considered better than multiple if statements in situations where you have a single expression (or variable) that you want to compare against multiple possible values. Here are some scenarios where using a switch statement might be preferable over multiple if statements:

  1. Readability: Switch statements can make your code more readable and easier to understand when you have a long list of conditions to check against a single variable. This is because switch statements are specifically designed for this type of scenario, making the code more concise and structured.
  2. Performance: In some cases, a switch statement can be more efficient than multiple if statements. Switch statements are often implemented using jump tables (especially when the cases are contiguous integers), which can lead to faster execution compared to multiple if-else checks, especially when there are many cases.
  3. Maintainability: Switch statements can make your code more maintainable, especially when you need to add or remove cases. It's easier to add a new case to a switch statement than to add a new if-else block, especially if the conditions are related to the same variable.
  4. Code Clarity: Switch statements can improve code clarity in situations where the logic involves checking a single variable against multiple values. This can make the code more organized and easier to follow, particularly when the different cases are related to the same variable.

I hope this helps

Thanks

This answer was AI-generated. Please read it carefully and use the forums for clarifications
2024-03-22 09-17-23
Chandra Vikas Sharma

Hi Priya,

A switch block is generally more readable and maintainable than chained if-else statements. It is easier to extend a switch case block than an if-else block because with if-else statements, you must evaluate all previous conditions to correctly insert a new else block. 

thanks

cv

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

This is also true for Switches! OutSystems switches allow any condition, so if your first condition is A < 5, and your second one A = 2, the second one will never run!

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP
Solution

Hi Priya,

This doesn't matter at all. Regardless of what everyone else says here, OutSystems Switches are basically a list of Ifs. It is true that in some high-code languages like C# or Java, a Switch can be more efficient than an If. But in these languages, switches can only be done on one variable, and the various branches are taken based on the variable's value.

However, in OutSystems, the conditions for each Switch branch can contain any condition, so is very much like an If. I haven't checked the actual generated code, but I'm pretty sure that it can't use the optimizations that languages have for actual switches.

So, the only matter that is of concern, is code readibility. I find that have more than a few switch branches can make the code become pretty cluttered, vs. a series of Ifs that look a lot cleaner. Example:


2019-01-07 16-04-16
Siya
 
MVP
Solution

I agree with @Kilian Hekhuis . I have checked the JavaScript generated for a Client Action, and it is a chain of if statements.

OutSystems Switch constructs can’t use the same optimizations as switch statements in C# or Java because they handle complex, multi-variable conditions instead of single-variable comparisons. This flexibility makes them more like if-else chains, which prevents the use of efficient optimizations like jump tables.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Thanks Siya for the technical confirmation.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.