Tetrix

WHILE Statement

Another form of flow control allows us to have a statement (or block of statements) repeatedly executed while a comparison result is true. This is done with the while statement. The form of the while statement is:

Here are some examples, each using a comparison of the value of variable z to a constant to arrive at a boolean value. Assume z = 0 at the start of this code:

Comparison

A simple program, like Hello World, just flows from start to finish, one statement after another until done. Solving real problems always involves evaluating data or user input and making decisions about what to do next, when to do it and what to do when things go wrong and much more. Handling these possibilities is called Flow Control.

Reference Variables

Having introduced objects and classes we need to discuss reference variables. Classes, which describe objects, can be thought of as complex Data Types. When we create an object instance with the new operator, we put the return value of new into a variable whose Data Type is the class name. This kind of variable is a reference variable.

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:

Data Types

As discussed in the previous lesson, variables in Java are typed, that is, we specify what kind of data can be contained in the variable. Java has two categories of data type, primitive types and reference types. Java has eight primitive types. Primitive types are also called value types. Reference types allow you define your own data types (objects). The primitive data types are:

Variables

A Variable is a memory location, or 'slot', reserved by your program into which you will store data. It is called a variable because we can change it as needed as the program executes. When we define variables we assign them meaningful names instead of having to work with actual memory addresses. Here are some examples:

Basic Syntax

As we move forward, we will start to acquire new vocabulary at an increasing rate. It is normal for vocabulary to take a while to stick, so don’t give up! Additionally, syntax is something you will learn by doing (and messing up!). Syntax is a challenge that you will always be getting better at, and is something nobody is perfect at.

Hello World!

The most common example of a first Java program is the famous Hello World! program. Before we get into the details of Hello World, I am going to introduce some of the supplemental resources we will be using.

What Level (scope) are we working at?

Software systems are typically made up of several layers of programs, each providing services to the layer above. Robots are no different. In a robot, there will be a layer below your program that provides services to the programs you will write. This layer below (an API) hides complexity or details that have already been programmed for you and you can ignore. There may also be a layer above your program that tells your program when to stop and start and may provide input. In that sense your program is providing a service to that higher layer.

What is a Program?

Simply put, a program is a set of instructions that describe the actions we want a computer to take to reach the desired output (result) from a set of input(s). That set of actions, taken as a whole, is also called an algorithm. The instructions to making a peanut butter and jelly sandwich qualify as an algorithm:

Pages