Instructional Material: Statements

Statements are the work horse construct of any text based programming language. Statements are where action happens and where you encode your algorithm into instructions for your robot. Statements fall into two categories, actions with assignment of a resulting value to a variable and actions that do not assign a result:

Note there is always only one item, a variable, on the left side of the assignment operator. You cannot have operations on the left side of the = sign: think ‘assignment’ not equals.

Operators are used to manipulate variables or constants (called operands) in a statement. Operators have an order of evaluation called precdence (more), at the same parenthetic level. You can control the order of operations by enclosing them within parenthesis which can be nested. In the above example, in the first statment, 2 * 3 is done first (due to multiplication having a higher precedence than addition), resulting in 5 + 2 + 6. These are then added up and the result is 13. In the second statement, 2 + 2 is done first due to the parenthesis resulting in 4. Now you have 5 + 4 * 3. Multiplication is done before (precedes) addition so 4 is multiplied by 3 resulting in 12. Now you have 5 + 12. Finally the 5 and 12 are added to give the result 17. Watch this video up to comparison operators (discussed later). The various math operators available are discussed here and here (arithmetic and assignment sections).

Statements are also used to make decisions about what your program should do next (Flow Control) and to call Methods. More on both of these topics later. Statements can span more than one line and continue until terminated with a semicolon. Statements, except for variable declarations, only appear inside Methods. Variable declaration statements can appear inside or outside of Methods, but always inside of a Class. We will discuss all of this a bit later.

Click the next arrow for a quiz on variable assignment statements.

 

Material Type: 
Lecture/Presentation
Education Level: 
Middle School
High School
Focus Subject: 
Computing / Computer Science
Robotics Software
HW Platform: 
EV3
RoboRIO
Tetrix
SW Platform: 
Java
Interactivity Style: 
Mixed
Audience: 
Learner