





6. Basics of Programming6.1 Summary
6.2 Contents6.2.1 Data TypesAll data that are handled in a program are declared as some type. Numbers, for instance, can assume data types of integers (whole numbers), doubles (decimals), and some other types. Data types are necessary to define a piece of data, because certain operations are associated to certain data types. Integers can be added, subtracted, multiplied, etc. A series of characters, also called a String (e.g. "Hello"), can and should not be multiplied; after all, what is "Hello" * "World" anyway? Data types have several other properties; most prominently, each data type can assume a smallest and a largest value. When you multiply two very large numbers on a primitive calculator, you may get as an answer "E", for Error. That means the calculator cannot handle the operation on as large numbers as you have entered. After all, computing space is finite  extremely finite as we will find out.An Integer can take values from 2,147,483,648 to +2,147,483,647; a double can take values from 4.94065645841246544e324 to 1.79769313486231570e+308, so extremely large. These sizes actually relate to certain numbers of bits, but that is outside the scope of this class. There are several more data types available, two of which we will discuss as well. Character is one of those; every letter, digit, or symbol on the keyboard and onscreen is part of a list of characters. It turns out that there are 256 defined characters in that list, that are sorted according to a standard. Because of this convenience, each character assumes a numbered id in the list of 256; the ids altogether range from 0 to 255. In effect, characters are handled just like numbers, only that we perceive them as characters instead. This schizophrenic behavior introduces another feature that is present in most programming languages, called Casting. Casting refers to the method of taking a data type and transforming it to another datatype, similar to a conversion. Since characters and integers are related by the previously mentioned relationship, one can be casted as the other. The captital letter 'A', for instance, converts into the integer 65. By the way, lower letters and capital letters have different integers associated to them. The last data type we will be considering at this point is called Boolean. It assumes only two values: True and False. If we assigned a boolean value to something, then this something can either be true or false. E.g. Are you 21 years of age: True or False. Boolean values can be used for Boolean algebra. For example: "Are you 21 years of age?" AND "Did you ask for a cigarette?" would result in different boolean results, depending on how the questions are answered. We won't get into Boolean algebra in this class, but some simple Boolean algebra should be understood. For an exercise, use the following Tool, which has, by the way, been developed in Java, to experiment with data types and casts: Usage: Click on "Generate" below one of the four datatypes boxes in order to generate a random value for the specific data type. If you would like, you can type in a desired value in the box above, which is then generated for you instead of a random value. Each node can be moved around by dragginganddropping it around the window. Move the nodes near the gray base blocks, in order to invoke an operation on them. For 8 out of the 9 operations, you will need 2 nodes. In order to cast a data type to another data type, use the 8 casts at the bottom of the screen. The can be moved around in the same way as regular data nodes. Once a cast snapped to a data node, you can remove it by clicking on the 4 arrows and then dragginganddropping. If you would like to remove a node from the screen altogether, you can move it over the garbage bin. 6.2.2 FunctionsFunctions in programming are similar to functions in Mathematics. For a very simple function, consider f(x) = y, that is for a given value of x, the function returns y. A function in Java, or any other programming language, works the same way: Give the function an x, called an argument, the function returns some value y, called a return value. Note: In objectoriented programming, functions are typically called methods.Unlike the mathematical function that usually return numbers of some sort, the functions we are dealing with return any sorts of types, but always only one type and one value at a time. As for arguments passed to functions, they can take any number of arguments of all different types. The mathematical equivalent to this would be, for example, f(x, y) = z. Arguments x and y are passed to the function and one value z is returned. The following mathematical equivalent is not possible in Java, but some other programming languages: f(a, b) = (x, y). For any type of function, we need to explicitely define the data types for both arguments and return types. We will be considering some examples now:
6.2.3 Basic StatementsStatements are essentially the heart of programming in general. Statements are used to make decisions, and repeat some operation for a certain number of times. When you examine your screen that is filled with colorful windows and moving graphics, millions and millions of these statements are executed to make all of this happen. We will discuss a few very basic statements here that can be found in every programming language, and in almost every function that does some type of computation.IFTHENELSE clause: This clause evaluates an expression, and if the expression is true, it will do something, otherwise, if the expression is false, it will do something else. Let's look at an example:
if (x <10) { Consider this:
if (x < 10) {

