Re: simplifying multiple 'if' statements
The program works the same way given either option. But as you learn more about C programming, you'll run into the issue of signed vs. unsigned and integer storage sizes quite often. I've found that keeping the code as simple as possible is the most important part of getting it right. A big help in keeping things simple is for a given set of constants and variables that interact, make sure that they're always the same type (signed/unsigned & size). Mixing and matching types will often lead to confusing behavior.
In any RobotC program, SensorValue is always of type "signed short". The makers of RobotC chose that type and you're stuck with it. But as you know, when you read a value from SensorValues and store it into a variable, you can choose whatever type you want for that variable. It's very important to know how C treats the value when converting it from one type to another. I found an explanation here: https://www.securecoding.cert.org/confluence/display/seccode/INT02-C.+Understand+integer+conversion+rules
and as you can see, it's a bit complicated.
To avoid all that complication, I prefer keeping things simple. Since we're stuck with SensorValue as a short, then it makes sense to have all the variables/constants/math/comparisons also be short. That takes care of any size mismatches. For signed/unsigned, you have some flexibility there, but as I explained before, it's easiest to handle the "echo not heard" condition as an unsigned.
That's the reasoning I used to recommend using "unsigned short" as the type of the scansweepdistmax constant.
The behavior between the two coding options is identical, so either will work.
I understand that since you're just learning C, it's easier for you to understand the if/else form. But as you get more comfortable with the language, one of the most powerful features you'll learn is how to use simple expressions in place of larger code blocks. I picked on this as a good example specifically because of the pattern where you're using if/else to assign true/false into a variable.
Think about it this way:
- The expression "range <= scansweepdistmax" is an operation that combines two integer values and produces a boolean result.
- You can use that boolean result in one of two ways:
-1- to control an if statement and choose between assigning true or false into a boolean variable
-2- assign the boolean result of the expression directly into the variable
You are correct. The two forms are identical. In this example, I've labeled the parts so you can see how to convert from one form to the other.
You should choose whichever form fits better with the given situation. If initializer and increment are blank, then while is usually a better choice.
There are a couple really important things to remember about using for() loops:
- If you declare a variable in the initializer, then the variable goes out of scope (doesn't exist) after the for exits. In the while() form, the variable still exists.
- The increment statement *always* executes after the body, even if the condition is now false. This is obvious in the while() form, but also true for the for().
But while that's all good to know, when I suggested you use PIDControl for your motors, I was thinking more along the lines of using nMotorEncoderTarget. You can learn more about this at http://www.education.rec.ri.cmu.edu/previews/robot_c_products/teaching_rc_lego_v2_preview/
Click on the "Movement" button at the top, then "Improved Movement" in the box on the left.