0

Soccer Robot C - Basic lessons - For

By now we took advantage of the loop() function's repeating nature. There will be times when that will be not enough and we will be forced to perform repeating tasks in some other way. Repeating tasks are almost the same, but (most often) a little different. An example of the task of this kind is to print first 100 natural numbers. Printing process is always the same, but there is a difference: each time the printed number is increased by 1. C++ will help as, as almost any other programming language, with "for" loop:
void RobotSoccer::loop() {
	for (int i = 1; i < 101; i++)
		print("%i\n\r");
	end();
}
Everything is understandable here, except the "for" line. Let's check each part of this instruction:
  • "for (" - this is mandatory start of the instruction.
  • "int i = 1; " - this is the first thing "for" will do: declare an integer variable, named "i", and will store 1 in it.
  • "i < 101; " - this is end condition. As long as it evaluates true, the loop will continue to repeat, executing first instruction after it.
  • "i++" - this is step part, instruction that will be performed after executing the next instruction below "for".
So, in the first pass, "for" will produce variable "i", set it to value 1, and check the end condition. Because i is 1, and 1 < 101, "for" will execute the next instruction, print(), printing 1.

After the first pass, "i" will be subject of "i++" instruction, will increase by 1, and will be now 2. "for" will again check end condition, being "1 < 101", still true, so the print() will be executed one more time, printing 2. Etc. After i becomes 101, "101 < 101" will return false and the "for" loop will exit.

Task: make a program that display all the capital letters.

Use ML-R 8x8 bicolor display, CAN Bus,UART, 4 switches (mrm-8x8a) for this task.