0

Line Robot - Basic lessons - Short-hand "if"

This kind of "if" is completely avoidable, but is handy. For example, assuming we need to print "Line" if there is a line under transistor 0 and "Nothing" otherwise, we could write the following code fragment:
void RobotLine::loop() {
	...
	if (line(0))
		print("Line\n\r");
	else
		print("Nothing\n\r");
	...
}
Suppose we are too lazy to write 4 lines of code but prefer to condense them to a single line. C++ will help us:
void RobotLine::loop() {
	...
	print(line(0) ? "Line\n\r" : "Nothing\n\r");
	...
}
You can probably understand how the short-hand "if" works by studying the example above. If not, here is the definition:
logicalExpression ? instructionIfTrue : instructionIfFalse
C++ will test logical expression "logicalExpression", as in ordinary "if". If it evaluates "true", it will execute instruction "instructionIfTrue". If not, "instructionIfFalse".

Task: make line following program shorter.

The solution will not improve the program but just teach You how to use short-hand "if". Start with the code below, we already used for line following. Here is a shorter version, You have to make even shorter (smaller number of program lines).

void RobotLine::loop() {
	if (line(5))
		go(10, 80);
	else
		go(80, 10);
}