Wednesday, December 9, 2009

What do Interpreted Programming Languages have in Common? Part II

I begin this tutorial a few weeks ago with Part I and received some very nice comments correcting my (fortunately) minor errors. This isn't a tutorial about how to program in a specific language or even really about how to program. I wanted to show the common structure of interpreted programming languages in the hopes of revealing some common threads, rather than focusing on the ins and outs of one language. I've heard it said that if you learn one langauge, it makes learning the next one easier. My problem is I get lost in the nuances of the language in question and lose track of the basic structure of programming. I've created this tutorial series to try and correct that. This tutorial is for my education as much as anyone else's so I welcome comments but, as I said before, be polite. This is about learning.

Speaking of Languages, code examples are presented in JavaScript, Python, and Ruby. I figured this represented a healthy cross-section of commonly used interpreted languages. Now, on with the show.

Controlling Program Flow, Part I

In our last episode (sorry, couldn't resist), we left off with Arrays and Booleans. The tutorial picks up with methods of flow control. According to Wikipedia, flow control is "a statement whose execution results in a choice being made as to which of two or more paths should be followed. For non-strict functional languages, functions and language constructs exist to achieve the same result, but they are not necessarily called control flow statements." In other words, it's a way of making decisions within the program given different conditions. Speaking of which:

Conditions: These statements in a program act as a decision tree or like a set of intersections in a city. The decisions or route are dependent on what conditions are or are not true (speaking of Booleans) or where you're trying to go. The basic structure of a conditional statement looks something like this:

if (condition)
{
     conditional code;
}

That's a very general example, of course, but it gives you a place to start.

if statements:
If statements are more or less the same in all three of the languages we've been working with:

JavaScript Sample Code:

var value = 0;
if (value > 0)
{
     alert("Greater than zero");
}
Python Sample Code:

n = 0
if n > 0:
     print ("Greater than zero")
Ruby Sample Code:
n = 0
if n > 0
     return "Greater than zero"
end
Of course, that's not much of a decision tree. Each program has different conditional statements that are similar but not exactly the same, to deal with a decision that is either this or that:

if-else statements:
The following examples are equivalent and look almost the same, but not quite:

JavaScript Example Code:
var name = "Jim";
if (name == "Jim")
{
     alert("Your name is Jim.");
}
else {
     alert("You are not Jim.");
}
Python Example Code:
name = "Jim"
if (name == "Jim"):
     print ("Your name is Jim")
else:
     print ("Your name is not Jim")
Ruby Example Code:
name = "Jim"
if name == "Jim"
     print "Your name is Jim"
else
     print "Your name is not Jim"
end
That takes care of "the fork in the road", so to speak, but what if there's more than one decision to make? It can't be just "Jim" or "no Jim" all the time. What about Jim, Bill, and Heather, for example?

else-if statements:
Depending on the program, this statement is called else-if, or elif, or elsif, but you get the idea.

JavaScript Code Example:
if (name == "Jim")
{
     alert ("Good Morning, Jim.");
}
else if (name == "Bill");
{
     alert ("Good Morning, Bill.");
}
else if (name == "Heather");
{
     alert ("Good Morning, Heather.");
}
else
{
     alert ("You're not on the list.");
}
Python Example Code:

if (name == "Jim"):
     print ("Good Morning, Jim.")
elif (name == "Bill"):
     print ("Good Morning, Bill.")
elif (name == "Heather"):
     print ("Good Morning, Heather.")
else:
     print ("You're not on the list.")
Ruby Example Code:
if name == "Jim"
     return ("Your name is Jim.")
elsif name == "Bill
     return ("Your name is Bill.")
elsif name == "Heather"
     return ("Your name is Heather.")
else
     return ("You're not on the list.")
end
print name
As you can see, there really isn't much difference between now each language expresses conditional statements. The details are quite minor and I hope this all illustrates the common factors in such statements, which is the important piece to learn in this lesson.


Time limitations are forcing me to make Part II shorter than I originally intended, but at least I've got it up and on the blog. Pick through everything and see if I left any holes. If so, let me know. If not, then all is good, at least for Part II. See you next time for Part III: Loops.


Share/Bookmark

11 comments:

  1. Isn't it crazy how new languages are forever "reinventing the wheel"? It's just like BASIC or Pascal or C but with tweaks here and there. All the grammar is similar but the vocab is different - take your pick from {}, BEGIN END, whitespace or whatever for code blocks, try to remember what arbitary symbol the designers chose for comments, etc.
    Might as well just have a programming editor where you can write in the style you prefer, then it converts it afterwards :)

    ReplyDelete
  2. pet peeve: mixing the brace position style in javascript (or C/C++). Yup, in the javascript if...else example.

    ReplyDelete
  3. Not thats its always possible to avoid cleanly, but I personally can't stand to see a string of if else statements. I recommend that in the interest of readability coders try to avoid them as much as possible. In some languages switch statements are a possible solution. While the example is too trivial to be interesting, if I were to come across it I would either change it or recommend a change to (in python)

    names = [ 'Jim', 'Bill', 'Heather' ]
    if name in names:
          print "Good morning,", name
    else:
          print "You're not on the list"

    ReplyDelete
  4. In Ruby simple if can be done like this:

    n = 0
    return "Greater than zero" if n > 0

    ReplyDelete
  5. This episode, does not say much about interpreted languages. The topic as addressed could be describing any procedural language from PL/I, Cobol, C, or ....

    So why is the topic titled 'interpreted languages' and not 'procedural languages'?

    ReplyDelete
  6. Do these languages not have "case" statements? far rather that than a long string of "if...elses".

    ReplyDelete
  7. Ed Baxter, Halifax, N.S.December 11, 2009 at 6:27 AM

    It would seem from the comments that this article is being read and commented on by 'experienced' programmers. The article is a basic 'how to program' tutorial. In most 'high level' languages, there are multiple ways of achieving the same result, but it never hurts to understand the basic principles, which is what this article describes. Almost all of the 'how to' info can be applied to Fortran, Basic, Cobol, Pl/1(not so much!), C, Java etc, so lets lay off the author for making things simple (and accurate), for the 'raw' beginner.

    ReplyDelete
  8. The semantics of switch are quite different in different languages. In the if-else example given replacement with a switch (or case) statement is quite trivial is all languages. However the 'C' select statement is essentially a selection from a set of common values, stated another way -- choosing a value from a list of possibilities. In VB the case statement can be the choosing of the first expression which evaluates to true like the if-else. Add in the fact the 'C' requires the explicit coding of the break statement, the 'C' switch is actually more reminiscent of the Fortran computed goto.

    I realize this is on the topic of interpreted languages, but break appears in PHP and Javascript. I can't speak about Ruby or Python.

    ReplyDelete
  9. No, I haven't forgotten about the tutorial. Occupied with other tasks at the moment. Will return shortly to respond to comments and incorporate corrections. Thanks.

    ReplyDelete
  10. Hi,
    I just want to say thank you so much for the tutorials. This is exactly what I have been looking for. I am a concept person. I learn best when exposed to the big picture first rather than learning in little bits and pieces.
    Have you written part III yet? I found parts I and II by searching "What do programming languages have in common" No sign of part III or beyond in the Google search results.
    Again, thanks so much.

    ReplyDelete
  11. Haven't written part III yet. I've been swamped with other projects unfortunately. Hopefully sometime this summer, I'll have the bandwidth to get back to this project. Judging by the nature of some of the comments I've received in parts I and II, I have much to learn. Thanks.

    ReplyDelete

Please make comments.