Implement If-Else Logic in Simulink Models
R2026bThis example shows how to implement if-else logic in Simulink® models. To implement if-else logic, use a combination of If and If Action Subsystem blocks. The If block evaluates logic conditions, and the corresponding If Action subsystem executes based on the evaluation outcome. In the model sldemo_ifsub, a set of arithmetic operations are performed on a sine wave. The logic conditions defined using the pulse wave determine the specific operation executed at any time.
Open the model.

If-Else Conditions
The If block implements the if-else conditions. These conditions compare a pulse wave signal value to zero and send action signals to the If Action Subsystem blocks. A Pulse Generator block generates the pulse signal.
Arithmetic Operations
If the pulse signal is positive, as specified by the if condition, the If Action Subsystem named Abs is activated, which outputs the absolute value of the sine wave input signal.

If the pulse signal value is zero, as indicated by the else condition, the If Action Subsystem named Saturation is activated. The subsystem either passes the sine wave as-is or clips it whenever the sine wave value exceeds the saturation limits specified in the Saturation block.

The outputs of the two subsystems are never active at the same time and are merged into one signal with a Merge block. The output signal is then biased by +2.
Simulation and Results
The scope shows the input sine wave signal, the pulse signal, and the and output signal. When the pulse is on (indicated by a value of 1 ), the output is the absolute value of the sine wave. When the pulse is off (value 0 ), the subsystem either outputs the sine wave as-is or clips it whenever the sine wave value exceeds the limits specified in the Saturation block. In each case, the output is biased by +2.

See Also
If | If Action Subsystem | Pulse Generator | Merge | Bias