Programming in "C": Level 2
By Aaron Schultz
This is level 2 to some of the programming concepts and practices employed in the coding of your robot. It is far from a complete or comprehensive guide, so don't expect to learn everything right here.
NOTE: This is written for the "C" programming language. While many other languages use similar concepts, they often use different syntax and structure. "C++" is a newer version of "C", so students who have learned "C++" will have a leg up on those learning other programming languages.
Contents
More Loops...
So far we have covered loops only in the form of "while(this){do something}". However, there is another form of loop structure, called a "for" loop, that can be utilized effectively under certain conditions. For most normal use, a "for" loop allows for something to be done a set number of times, repeating over and over until a counter variable reaches a specified value. A "for" loop take the form:
for(initializer; condition; incrementer){
statements
}
For example:
int Num = 0;
for(int i = 0; i < 3; i = i + 1){
Num = Num + 2;
}
This code creates a variable Num and assigns it the value 0. The "for" loop creates a new variable, i, and assigns it the value of 0 as well. The loop is set so that it will repeat while i is less than 3. The loop also declares that after each run through the statements, i will be incremented by 1.
Inside the loop, Num is incremented by two each time the loop repeats. In this case, the final value of Num will be 6 (the loop will run three times: when i = 0, i = 1, and i = 2, adding 2 to Num each time.
Note that we used all three parts of the "for": the initializer, the condition, and the incrementer. This doesn't always have to happen though. If you create a variable beforehand to use as a counter, you can leave the initializer section blank (other than the semi-colon) and just use that variable in the condition. For example:
int num = 0;
int count = 0;
for( ; count < 3; count = count + 1){
num = num + 2;
}
This would produce the same output as the previous "for" loop.
Type Casting
At many times during the course of programming, a bit of a hitch concerning variable types will appear when you try to perform mathematical operations on them. Let's say you divide an "integer" variable holding the value 3, by 2. What do you get? Not 1.5! When an "integer" is used in computation, the result specific part of the computation is an integer, which is done by slicing off all decimal places. In this case, 3 / 2 will give you 1! In order to get the desired result, something called typecasting must be used. Essentially, typecasting converts a variable to a different type (but only at the specific place it is typecasted) so that calculation perform differently. To do this, you simply put the desired variable type in parentheses just before the variable name.
double num = 5.0; double result; result = num / 2.0; // result = 2 result = (double)num / 2.0 // result = 2.5 result = num / 2.0 // result = 2 Note that the "double" status // does not stick around result = ((double)num) / 2.0 // Often times it is a good idea to wrap the // typecasting and the variable name in // parentheses, to make sure it applies only // to the desired variable
It may seem unreasonable that you would ever want 3 / 2 to equal 1, but there are many cases (not to be explained here) where you could use the "decimal killing" power of integers for certain calculations.
Be Careful with Decimals!
When using floating point numbers (variable types of "float" or "double"), make sure the hard-coded numbers you use in calculations are also doubles or your calculations might end up with those weird, cut-off integer-like values. In other words:
double Num = 5.0; double result; result = Num / 2; // result = 2 -- Because "2" is written as an integer // (it has no decimals), the computer // will perform the integer-type math result = Num / 2.0 // result = 2.5 -- Using 2.0 fixes the problem