# How to use Loops in Simulink MATLAB : tutorial 7

In this tutorial, I will explain you the working of conditional loops and how to implement them using Simulink. At the beginning you are provided with a brief introduction of loops, and where we can use them also a basic introduction of loops in other programming languages and their working principles. After that loops are implemented on Simulink’s platform and their working is explained step by step. After simple implementation loops are implemented using examples of counter. A counters named as up counter is implemented on Simulink and its outputs is displayed. At the end I gave a simple exercise to perform it on your own, this will help you strengthen your concepts regarding algebraic loops in Simulink.

Introduction to Algebraic loops

Algebraic loops mean to perform a certain task iteratively up to a certain number of times or un till a specific condition is fulfilled. In Simulink there is no proper block to implement loops, however we can design our own loop in which the output is fed to the input with the delay in between to remove any ambiguity. In order to make the loop error free we must provide a condition which will decide whether the loop will be continued or stopped.

In other programming languages the loops are named as for loop and while loop and at this stage of programming I assume that you are familiar with these loops.

Explanation with a program:

### Designing an up counter with Simulink

Lets’ now move towards the programing part. I will now perform here a simple example which will help you understand the working of loops in simulink. Open MATLAB and then open Simulink. In Simulink click on the library browser icon and open library browser as we have been doing in previous tutorials. From the library browser click on the commonly used blocks section as we have been doing in previous tutorials and then select the constant block from that section as shown in the figure below, Figure 1: Constant block

Figure 2: Math operation

Figure 4: Delay block

Figure 6: Scope block

Figure 7: Placed components

Figure 8: Flipping block

Figure 9: Block diagram

Figure 10: Output

Figure 11: Signal routing

Figure 12: Switch block

Figure 13: Threshold value

Figure 14: False condition

Figure 15: True condition

Figure 16: Conditional up counter

Figure 17: Output of up counter

This is a simple up counter which will start from zero and end to the value provided by us in the switch block.

Exercise:

• Design an down counter using MATLAB’s Simulink.

(Hint: You might need to change the condition of the switch block from > to <)