top of page

Programming structures 'for'

This structure allows you to block certain times code number can repeat. At the end of repeating the cycle the program continues with the code normally, using the "cycle for ". It is necessary to use variables, comparison operators and decrements / increments. The block "structure for " is found in the section " Control " and has the following structure:

Variable . We will use it to save the value to use in the condition : Condition determines how many times the block will be repeated. increase / decrease : Increase or decrease the variable so that at some point it meets the condition and finishes the structure " for ".

arithmetic operations

Programming can be done in any arithmetic operation, to do the following these commands are used:

Some examples of the use of these operators are:

Remember that you have to be careful with the division, because if we are using a type 'variable int "and the result has a decimal point, it will save only the integer part; if we want to save the whole result with the decimal part must use the variable type " float ".

Example 1

In this example we will make the LEDs light up  in sequence, for this we will use the "structure for " and a variable named " counter ". Upon completion of the " for " LEDs OFF:

In this example we can see that the variable " counter " starts at 1 so first it will turn on the LED 1, then as in the " for " indicated that increase of 1 to 1, will turn on the LED 2, then 3 and so on until 5 leds turn on. After it turns on the LED 5, the counter variable takes the value of 6 and no longer meets the "condition " so it leaves the structure " for " and off the LEDs.

bottom of page