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.
One of the frustrating things about learning how to program is that you have to start somewhere. I know there's no way of avoiding this, but when you learn your first programming language, you are also learning the basic structure of how to program in general. It's easy to get mixed up at this stage and lost in the details of a particular language, losing sight of the overall goal. I've tried, but there are few, if any, guides available online that present the general structure of an interpreted language (as opposed to a compiled language which we won't be discussing here), without referencing one specific language. With that in mind, I thought I'd try to put together an outline of what interpreted languages seem to have in common as far as a basic structure. My specific examples will be JavaScript, Python, and Ruby, so you can see how different languages are expressed within this structure. But first, some basic definitions:
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
JavaScript Sample Code:
Python Sample Code:bunnies = 3 while bunnies > 0: print bunnies bunnies -= 1Ruby Sample Code:input = '' while input != 'bye' puts input input = gets.chomp end
JavaScript Sample Code:Variables: In a program, variables are used to store information, however as the name implies, the value of the information stored isn't static. It can be changed depending on the requirements of the program. Usually variables are assigned values as in the following examples.Statement one; // This is the first statementPython Sample Code:Statement one #this is the first statementIn Python, you can also create a comment that spans multiple lines like this:Statement one """this is a multi line comment about the first statement"""Ruby Sample Code:Statement one #this is the first statementThere's also a way to make a comment that spans multiple lines in Ruby:Statement one =begin this is a multi line comment about the first statement =end
JavaScript Sample Code:var color = "blue";Python Sample Code:color = "blue"Ruby Sample Code:color = "blue"
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.
- Numbers
- Strings
- Arrays
- Booleans
- 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:
JavaScript Sample Code:Associative Arrays: This is just like any other array except that the indexes are switched out for "keys". In a standard array, you need to remember that a particular index, such as 4, contains the value "Sailor Moon backpack". The computer doesn't have a problem with that, but you might. If you, for example, wanted to associate a child's name with a backpack type in an array, you could create that connection in an associative array as in the following:var cubby = []; cubby[0] = "First"; cubby[1] = "Second"; cubby[2] = "Third"; cubby[3] = "Fourth";Python Sample Code:cubby = ["First", "Second", "Third", Fourth"]Ruby Sample Code:cubby = ["First", "Second", "Third", Fourth"]
JavaScript Sample Code:These are called key-value pairs with, for example "Jamie" being the key (formerly the index) and "Sailor Moon" being the value. So, going back to our elementary school example, we know that Jamie's cubby contains a Sailor Moon backpack (I know I'm dating myself, but it's been awhile since my kids were little). Booleans: Simply put, a boolean is a value that can contain one of two positions: true or false. There are only three operators to work with when using booleans: and, or, and not. Booleans are used to evaluate statements to determine if they're true or false. If true, then one thing happens. If false, then another thing happens. It gets more confusing if one thing happens if A and B are true, and another thing happens if A or B are true. Booleans work more or less the same way in all three of the languages. A common way to use a boolean in any of these languages is to evaluate a loop (we'll get into loops in Part II) in a while statement. While a condition is true, continue going through the loop. When the statement is false, exit the program. For instance, you may have created an array of products your business sells. A customer may be searching your site for products you have in stock. While the customer continues to enter the names of products you have in stock, the search returns a web page for the desired product. When the customer searches for a product name out of stock, the search returns the "not in stock" page. Long, drawn out blogs are a bore to read, even if the content isn't. I'm going to cut the tutorial off here. Part II will continue with the lesson, by explaining conditions, loops, functions, and objects. Stay tuned.var backpacks = []; backpacks["Jamie"] = "Sailor Moon"; backpacks["David"] = "DragonballZ"; backpacks["Michael"] = "Batman"; backpacks["Jim"] = "Jonny Quest";Python Sample Code:backpacks = { "Jamie" : "Sailor Moon", "David" : "DragonballZ", "Michael" : "Batman", "Jim" : "Jonny Quest" }Ruby Sample Code:backpacks = { :Jamie => 'Sailor Moon', :David => 'DragonballZ', :Michael => 'Batman', :Jim => 'Jonny Quest' }
In javascript you forgot to put a ";" array definitions. In javascript arrays can be defined like this "[1,2,3,4]" and assciotive arrays can be defined as "{'one':1,'two':2,'three':3}"
ReplyDeleteIn the first Python example you forgot a colon after the while statement, like this:
ReplyDeletewhile bunnies > 0:
In your python comment example that spans multiple lines, that isn't just a comment... It's a string. e.g. it would end up in the compiled to bytecode / .pyc file, while a true comment would not.
ReplyDeleteThe first Javascript example code doesn't show up because you have the script tags in there; either remove the tags or encode the < and > symbols
ReplyDeleteThanks. Other comments have pointed out technical errors; I've spotted three simple spelling errors. U can ignore them, the meaning comes through anyway, or make it look perfect.
ReplyDeleteIn "STATEMENTS", line 3; "a statement is ""a"" step..not ""s""
In "COMMENTS", Should start: Since statements are ""not"" always...U dropped the ""not"". 2nd sentence: It's not only a note from ""to"" programmer to himself. Should be ""a"" not ""to""
Thanks again. sbader7@yahoo.com
As others have mentioned, JavaScript has a literal syntax for arrays and hashes, eg. var stuff = { foo: "bar" };
ReplyDeleteAs of the latest version of Ruby (1.9), the literal hash syntax is the same for Ruby/Python/Javascript, eg. { foo: "bar" }