MakeCode Variables

Reading, remembering and calculating

Variables can be used to store values and information. They are in principle containers for changeable values. Stored variables can be recalled at a later time to read out their value or to calculate with it. Variables always consist of a unique name. This name is arbitrary and is assigned by the user himself.

The following example first initializes the variable counter with the value 0. In the "forever" loop the value of the "counter" variable is then alternately printed and then increased by 1.

In the next example we combine a variable with an "if" condition. As before, we initialize the variable counter with the value 0 and increase it step by step by 1. Additionally, we use the "if" condition to check if the variable has the value 1. If this is the case, we activate the headlights of the joy car. If this is not true, we switch off the headlights.

In the next step, we will add an additional component to our previous example. We additionally check if the value of our variable is below 4. If this is the case, we increase our "counter" variable by 1 again. If this is not the case, and thus the value of our variable is 4 or even higher, we set our variable back to 0.

In this way, we run through our instructions again and again, even without the use of a loop.

Of course a variable does not have to be increased by 1 all the time. Variables can also be decreased and can also be used for more complex mathematical calculations. As the variables represent only one container, they can be used for practically every value, every cache and every calculation. In the next example, we show once again how our previous example works with a subtraction instead of an addition.

Achtung!

Ihr Webbrowser ist veraltet. Wechseln Sie jetzt zu einem aktuellen Browser für mehr Sicherheit, Geschwindigkeit und den besten Komfort auf dieser Seite.