Foreword: I'm writing this particular blog as much for my own education as for yours. I'm sure I've made a mistake or two here and there, and if so, I don't mind you pointing them out. I would appreciate it however, if you at least tried to be civil if not polite when making corrections. I want this to be a friendly atmosphere where education is encouraged, not fear of mistakes. Thanks.
The Purpose of a Program: In general, any computer program is supposed to take a large problem, and solve it by breaking it down into small, bite-sized bits or steps.
Syntax: This is the rules about how to write in a particular programming language. While interpreted languages in general use the same "parts" or structure, how each language is expressed within that structure varies.
Statements: A statement in a program is like any other statement, such as a person making a verbal statement or writing a sentence in an email. In a program, a statement is s step that defines a particular action that's taken in the program. Statements are written, regardless of the language, in a way that the computer, but not necessarily people, can understand.
The statements in the previous examples don't quite do the same thing, but you can get the idea of how statements are similar and different across three separate languages. Comments: Since statements are always understandable by human beings, comments are often written in human-readable language so that people can understand what a statement is supposed to say and do. It's not only a note from to programmer to himself or herself, but to any other programmer who may later need to understand and perhaps modify the code. Using a while statement as an example, a statement in general looks like this:Statement oneStatement two
Python Sample Code:bunnies = 3 while bunnies > 0: print bunnies bunnies -= 1Ruby Sample Code:input = '' while input != 'bye' puts input input = gets.chomp end
Variables Types: Variables can be used to store different types of information. Depending on the programming language involved, the variable types may be either strictly typed, which means you must tell the program specifically, what type of data is to be stored, or loosely typed, which means the language doesn't really care what is contained in the variables. That said, you as the programmer must know what type of data you want stored in a variable. Variable types include:Numbers: Numbers are what you imagine them to be: 1, -2.2 3.14159265, and so on. Actually, there are two different ways of expressing a number within a program: Integers and Floats. Both Integers and Floats can be expressed as positive or negative values.
- An Integer or int is a whole number such as 1, 5, -123, 345698, and so on.
- A Float or Floating point number is a number expressed with a decimal such as 1.3, -33.7, 8.5678906556, and so on.
- Mathematical Operations are what you might have learned as "signs" as a child when studying basic math. The most commonly used mathematical operators are addition (+), subtraction (-), multiplication (*) and division (/).
String Operations: I mentioned that strings couldn't be added, subtracted, etc... but that's not entirely true. If 2 + 2 is expressed as integers, the result will be 4, but if "2" + "2" is expressed as strings, the result is "22". Confused? When you add two strings, performing "2" + "2" is no different than performing "a" + "8". The operation doesn't perform mathematical addition but rather concatenation. In this case, "a" + "8" concatenates to "a8".Arrays: While the variable types presented thus far store individual pieces of information, arrays are designed to store groups of values, such as a list of names or a series of numbers. An array provides an ordered structure for storing a group of values. When my kids were in elementary school, as they entered their classroom from outside, they could store their backpacks in individual cubbyholes or "cubbies" in storage racks. This is the same principle as an array, with each "cubbyhole" representing a "slot" or element in the array. Each element position is represented by an index. Children always start counting with the number one (1) and go up from there. so the first cubby on the left on the top row would be "one", and next cubby to the right would be "two" and so on. In an array, the index of the first element is "zero" (0), the next is "one" (1) and so on. So an element's position in an array is represented by an index, but the element isn't very useful unless it contains a value. (example of how to write an array) So, for example, the element in this array with the index of "one" (1) contains the value "second". Arrays can contain numbers, strings, or a combination as follows: