Hi, I'm new here. I know Switch and If are conditional paths. But what is the different between them? When do we use them?
Hello @Toumtam Phiphack ,
If
Simple True/False check. Used in UI screens to show/hide stuff and also in server/client actions. Can also be used in expressions.
Use if when you just need a yes/no decision like validating a form.
Used when there are multiple options. Works in server/client actions, not in UI. You can use switch when you have many possible cases.
Example: Run different logic based on status (Completed, In-Progress, etc.).
In OutSystems, "if" is a basic conditional statement that executes a block of code only if a specified condition is true, while "switch" is a more complex conditional tool that allows you to choose one block of code to execute based on the value of a variable, essentially acting like a series of "if-else if" checks for different possible values.
Also One Switch is used for toggle between true/false.
Hi @Toumtam Phiphack ,
If Condition
Switch Condition
Thanks,
sivasakthi
Hi @Toumtam Phiphack ,The "If" widget is used to conditionally display content based on a boolean expression or condition being true or false.
The "Switch" widget works like a multi-conditional statement allowing different content to be displayed based on the value of a given expression. And also please refer this documenthttps://success.outsystems.com/documentation/11/reference/outsystems_language/logic/implementing_logic/logic_tools/switch/https://success.outsystems.com/documentation/11/reference/outsystems_language/logic/implementing_logic/logic_tools/if/Thanks,Sahana
Hi Toumtam,Please refer this Documentation Switch and take look on demo switch statement video Video link
For If statement If statemet and demo of If statement video link
In OutSystems, the primary difference between a "switch" and an "if" statement is that a "switch" allows you to check a single variable against multiple possible values, while an "if" statement only checks a single condition against a Boolean value, making "switch" more efficient when dealing with multiple distinct options for a single variable.
If you have lots of values to check switch is better and cleaner way.
This is wrong, a Switch allows you to combine multiple conditions in a single statement, it is not (like in C or Java) used "to check a single variable against multiple possible values". You can very well check totally unrelated conditions.
Also, an If statement produces a boolean value, it is not used to check against a boolean value.
As for Switch being "cleaner", that's a discussion to have. I personally don't like Switches as they easily make your flow impossible to layout in a satisfactory manner.
It is a basic question, that when searched in the OutSystems documentation, internet, or even asked to ChatGPT is answered correctly within a second. Yet we have 7 people doing the search for you.
No one can complain that the OutSystems community is not active ;)
In IF it evaluates a single condition and executes one of two branches like (True/False).
In Switch it Selects one case from multiple possible conditions.
For simple example based on outsytem
In IF condition, i want to show the username based on userid in expression widget,if the userid is not created it will show Create user .For this we can enclose the container in if,in condition we can give Userid=nullidentifier .in true branch we can give the create User , in Flase we can show the user Id.
In swich condition,
I want to show the Daytype of the week means,we can user the Dayofweek() builtinfunction it returns the day in number 1 for Sunday, 2 for Monday, etc .
In Switch Widget,
Case 1: If DayOfWeek = 1, in text Weekend (Sunday)
Case 2: If DayOfWeek = 7, in text Weekend (Saturday)
otherwise it shows the Weekday
the output we can show like
If today is Monday means Weekday. If today is Saturday means Weekend.
Sudha Narayanan
Hi Toumtam,
If you've read all the answers you're up to speed with the differences. However, one thing to understand is that the OutSystems Switch is not, like it is in many other languages, a selection of multiple values of a single variable. Basically there's no difference between a Switch with three conditions and three Ifs with these conditions below each other. So it's up to your personal preference when to use a Switch instead of multiple Ifs.