Basic Logic
The tutorial shown below is a syntax refresher for C based languages, but is teared towards RobotC. It is assumed for all tutorials on this sight that the reader has a basic understanding of programming and wishes to learn a new language. The most significant difference for Java users is the declaration of variables in RobotC. Please pay close attention to how one declares variables in C / C++ as the declaration of a variable in RobotC is NOT proceeded by new. An important difference between RobotC and C / C++ is the lack of a heap on the cortex. Only local and global variables may be declared in RobotC.
General Notes:
-When using a loop like a "while" loop or a "method", you must put your code inside brackets
Example:
if (a == b)
brackets---> {
a++;
brackets---> }
-Brackets indicate everything that is happening inside that function
-After a line of code, you must put a semicolon, ";", to indicate that it is the end of that line
-The exceptions to this are loops and methods
Common Language:
-Debugging: testing whether or not there are errors within the code
-Compiling: assembling the code so that it may be run
====================Commenting====================
-Commenting: adding notes to your code so others may understand what it does
-To comment, type "//" followed by the comment. This form of commenting may only comment items on one line. This does not affect your code whatsoever.
Ex: int a = 0; //this declares variable a
-Another form of comments is done by typing the "/* [comment goes here] *\". This form of commenting may take up as many lines as desired. However, the comment must start with "/*" and end with "*\".
Ex: int a = 0; /*this declares
variable a\*
====================Variables====================
-Variables can be called anything. However, variable names CANNOT begin with a number.
-Use variable names that refer to the value that it holds:
Ex: UltrasonicsDepth
-Do not use single digit variables without commenting them first
Ex: Don't use "a" or "b" without //this stands for x
Declaring Variables:
Term Operation Example
========================================================================================================
| bool | [true or false] {bool a = true;}
========================================================================================================
| int | [Holds an Integer] {int a;}
========================================================================================================
| float | [Holds integers with decimals] {float a = .03f;}
========================================================================================================
| double | [Holds integers and decimals] {double a = .056;} (more exact than floats
========================================================================================================
Logic Operators:
Basic Algebra:
The table bellow describes ways of performing calculations in programming.
Symbol Operation Example
========================================================================================================
| + | [Adds two numbers] {a + b or 10 + 20}
========================================================================================================
| - | [Subtracts two variables] {a - b or 10 - 20}
========================================================================================================
| * | [Multiplies two variables] {a * b or 10 * 20}
========================================================================================================
| / | [Divides two variables] {a / b or 10 / 20}
========================================================================================================
| ++ | [Add one to a variable] {i++}
========================================================================================================
| -- | [Subtract one from a variable] {i--}
========================================================================================================
| % | [Finds remainder after division] {a % b or 10 % 20}
========================================================================================================
Setting Variables:
The table bellow describes ways of setting variables in programming.
Symbol Operation Example
========================================================================================================
| = | [Assigns a value to a variable] {a = b; or a = 5;}
========================================================================================================
| += | [Adds to the variable] {a += b; or a+= 4;}
========================================================================================================
| -= | [Subtracts from the variable] {a -= b; or a -= 3;}
========================================================================================================
| *= | [Multiplies numbers] {a *= b; or a *= 2;}
========================================================================================================
| /= | [Divides to the variable] {a /= b; or a /= 8;}
========================================================================================================
| %= | [Finds remainder after division] {a %= b; or a /= 9;}
========================================================================================================
Inequalities:
Inequalities are primarily used in if statements to determine whether or not the conditions within the if statement are met.
Symbol Operation Example
========================================================================================================
| == | [Is equal to] {(a == b) or (a==5) or (5==a)}
========================================================================================================
| > | [Is greater than] {(a > b) or (a > 3) or (3 > a)}
========================================================================================================
| < | [Is less than to] {(a < b) or (a < 7) or (7 < a)}
========================================================================================================
| >= | [Is greater than or equal to] {(a >= b) or (a >= 2) or (2 >= a)}
========================================================================================================
| <= | [Is less than or equal to] {(a <= b) or (a <= 9) or (9 <= a)}
========================================================================================================
| != | [Does not equal to] {(a != b) or (a != 4) or (4 != a)}
========================================================================================================
Logical Operators:
Logical operators, like inequalities, are primarily used in if statements. However,
Symbol Operation Example
========================================================================================================
| && | [If conditions x and y are true] {(a==5 && b < 3)}
========================================================================================================
| || | [If conditions x or y are true] {(a ==5 || b < 3)}
========================================================================================================
Printing Out Variables and Strings:
-These change from program to program, but normally there are some similarities for displaying text.
Term Language Example (How to)
========================================================================================================
| System.out.println | [Java] {(variable); or ("Hello");}
========================================================================================================
| Console.WriteLine | [C#] {(variable); or ("Hello");}
========================================================================================================
| printf | [C++] {("%f", variable);}
========================================================================================================
| cout >> | [C++] {(variable); or ("Hello");}
========================================================================================================
Unique Characters:
Symbol Operation
============================================================
| \\ | [the "\" character]
============================================================
| \' | [the "'" character]
============================================================
| \" | [the """ character]
============================================================
| \? | [the "?" character]
============================================================
| \b | [Backspace]
============================================================
| \n | [Newline, or Enter]
============================================================
| \t | [Tab]
============================================================
Unique Characters:
These characters need to be written in this manner in order to appear in text.
Symbol Operation
============================================================
| \\ | [the "\" character]
============================================================
| \' | [the "'" character]
============================================================
| \" | [the """ character]
============================================================
| \? | [the "?" character]
============================================================
| \b | [Backspace]
============================================================
| \n | [Newline, or Enter]
============================================================
| \t | [Tab]
============================================================
-When using a loop like a "while" loop or a "method", you must put your code inside brackets
Example:
if (a == b)
brackets---> {
a++;
brackets---> }
-Brackets indicate everything that is happening inside that function
-After a line of code, you must put a semicolon, ";", to indicate that it is the end of that line
-The exceptions to this are loops and methods
Common Language:
-Debugging: testing whether or not there are errors within the code
-Compiling: assembling the code so that it may be run
====================Commenting====================
-Commenting: adding notes to your code so others may understand what it does
-To comment, type "//" followed by the comment. This form of commenting may only comment items on one line. This does not affect your code whatsoever.
Ex: int a = 0; //this declares variable a
-Another form of comments is done by typing the "/* [comment goes here] *\". This form of commenting may take up as many lines as desired. However, the comment must start with "/*" and end with "*\".
Ex: int a = 0; /*this declares
variable a\*
====================Variables====================
-Variables can be called anything. However, variable names CANNOT begin with a number.
-Use variable names that refer to the value that it holds:
Ex: UltrasonicsDepth
-Do not use single digit variables without commenting them first
Ex: Don't use "a" or "b" without //this stands for x
Declaring Variables:
Term Operation Example
========================================================================================================
| bool | [true or false] {bool a = true;}
========================================================================================================
| int | [Holds an Integer] {int a;}
========================================================================================================
| float | [Holds integers with decimals] {float a = .03f;}
========================================================================================================
| double | [Holds integers and decimals] {double a = .056;} (more exact than floats
========================================================================================================
Logic Operators:
Basic Algebra:
The table bellow describes ways of performing calculations in programming.
Symbol Operation Example
========================================================================================================
| + | [Adds two numbers] {a + b or 10 + 20}
========================================================================================================
| - | [Subtracts two variables] {a - b or 10 - 20}
========================================================================================================
| * | [Multiplies two variables] {a * b or 10 * 20}
========================================================================================================
| / | [Divides two variables] {a / b or 10 / 20}
========================================================================================================
| ++ | [Add one to a variable] {i++}
========================================================================================================
| -- | [Subtract one from a variable] {i--}
========================================================================================================
| % | [Finds remainder after division] {a % b or 10 % 20}
========================================================================================================
Setting Variables:
The table bellow describes ways of setting variables in programming.
Symbol Operation Example
========================================================================================================
| = | [Assigns a value to a variable] {a = b; or a = 5;}
========================================================================================================
| += | [Adds to the variable] {a += b; or a+= 4;}
========================================================================================================
| -= | [Subtracts from the variable] {a -= b; or a -= 3;}
========================================================================================================
| *= | [Multiplies numbers] {a *= b; or a *= 2;}
========================================================================================================
| /= | [Divides to the variable] {a /= b; or a /= 8;}
========================================================================================================
| %= | [Finds remainder after division] {a %= b; or a /= 9;}
========================================================================================================
Inequalities:
Inequalities are primarily used in if statements to determine whether or not the conditions within the if statement are met.
Symbol Operation Example
========================================================================================================
| == | [Is equal to] {(a == b) or (a==5) or (5==a)}
========================================================================================================
| > | [Is greater than] {(a > b) or (a > 3) or (3 > a)}
========================================================================================================
| < | [Is less than to] {(a < b) or (a < 7) or (7 < a)}
========================================================================================================
| >= | [Is greater than or equal to] {(a >= b) or (a >= 2) or (2 >= a)}
========================================================================================================
| <= | [Is less than or equal to] {(a <= b) or (a <= 9) or (9 <= a)}
========================================================================================================
| != | [Does not equal to] {(a != b) or (a != 4) or (4 != a)}
========================================================================================================
Logical Operators:
Logical operators, like inequalities, are primarily used in if statements. However,
Symbol Operation Example
========================================================================================================
| && | [If conditions x and y are true] {(a==5 && b < 3)}
========================================================================================================
| || | [If conditions x or y are true] {(a ==5 || b < 3)}
========================================================================================================
Printing Out Variables and Strings:
-These change from program to program, but normally there are some similarities for displaying text.
Term Language Example (How to)
========================================================================================================
| System.out.println | [Java] {(variable); or ("Hello");}
========================================================================================================
| Console.WriteLine | [C#] {(variable); or ("Hello");}
========================================================================================================
| printf | [C++] {("%f", variable);}
========================================================================================================
| cout >> | [C++] {(variable); or ("Hello");}
========================================================================================================
Unique Characters:
Symbol Operation
============================================================
| \\ | [the "\" character]
============================================================
| \' | [the "'" character]
============================================================
| \" | [the """ character]
============================================================
| \? | [the "?" character]
============================================================
| \b | [Backspace]
============================================================
| \n | [Newline, or Enter]
============================================================
| \t | [Tab]
============================================================
Unique Characters:
These characters need to be written in this manner in order to appear in text.
Symbol Operation
============================================================
| \\ | [the "\" character]
============================================================
| \' | [the "'" character]
============================================================
| \" | [the """ character]
============================================================
| \? | [the "?" character]
============================================================
| \b | [Backspace]
============================================================
| \n | [Newline, or Enter]
============================================================
| \t | [Tab]
============================================================