Incrementing and decrementing Pro Preview

Two of the most common operations with variables are adding 1 to the value of the variable or subtracting 1. Those are the cases when we have the variable as a counter. The process of adding 1 is called incrementation and the process of subtracting 1 is called decrementation.

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #954
  • 24 Sep 2018

The set up

Let us get back to the example from the tutorial regarding the variables. We want to use a variable to track the amount of lemonade we have instead of piece of paper. 

Incrementation

The first way to achieve that is to count the amount of lemonade we have sold. So basically the variable is a counter and it will increase its' value by one each time a lemonade is ordered. As mentioned that process is called incrementation and can be achieved within the LEGO EV3 Software as follows:

Let us take a closer look at what we are actually doing. So first you need to read the current value of the variable, which is performed by the first block.

Next you have to use a math block to add 1 to the read value

and finally you have to write down the result back into the variable.

Decrementation

Another approach will be to have the variable (the counter) storing how much lemonade is left. That would mean that each time a lemonade is ordered we will need to subtract 1 from the current value or decrement the value. This can be achieved the same way, but this time instead of setting the math block to Add we will set it to Subtract. The final program should look as follow:

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for Instructors Remote Training
  • 136
  • 280:11
  • 156