Sunday, August 1, 2010

New Series - Language's Basics by Example



I'm going to start working on a new posts series today. This time, I will extend some previous code, to show you some basic features of each of the 20 languages I have been using so far. If you haven't been following this blog, then I will remind you which those languages are:

Languages targeting the .NET/CLR runtime:
C#, VB.NET, C++/CLI, F#, Boo, Phalanger, IronPython, IronRuby, Delphi Prism, Zonnon, Nemerle, Cobra, JScript.NET

Languages targeting the Java/JVM runtime:
Groovy, Java, Jython, JRuby, Fantom, Scala, JavaFX

Now, about the content, I will show you an extended version of the Greetings Program implementing the following Language Basics statements and constructs features by using a Console Application. I will be using again 2 versions of the same program: Verbose and Minimal, so we can keep showing some numbers just for the fun.

The Program's Structure will be as follows:

// Language Basics

    // Greet Class
        // Fields or Attributes
        // Properties
        // Constructor
        // Overloaded Constructor
        // Method 1
            // "if-then-else" statement
        // Method 2
            // "for" statement
        // Overloaded Method 2.1
            // "while" statement
        // Overloaded Method 2.2
            // "switch/case" statement

    // Console Program
        // Define object of type Greet 
        // Instantiate Greet. Call Constructor
        // Call Set Properties
        // Call Method 2
        // Call Method 2.1 and Get Properties
        // Call Method 2.2
        // Stop and exit - Read Input From Console

And here below some definitions of each of the statements and constructs used on the upcoming posts:

Class
"In object-oriented programming, a class is a construct that is used as a blueprint (or template) to create objects of that class. This blueprint describes the state and behavior that the objects of the class all share. An object of a given class is called an instance of the class. The class that contains (and was used to create) that instance can be considered as the type of that object, e.g. an object instance of the "Fruit" class would be of the type "Fruit".

It encapsulates state through data placeholders called attributes (or member variables or instance variables); it encapsulates behavior through reusable sections of code called methods." Taken from: http://en.wikipedia.org/wiki/Class_(computer_science)

Constructor
"In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created." Taken from: http://en.wikipedia.org/wiki/Constructor_(computer_science)

Attributes
"In computing, an attribute is a specification that defines a property of an object, element, or file. It may also refer to or set the specific value for a given instance of such." Taken from: http://en.wikipedia.org/wiki/Attribute_(computing)

Methods
"In object-oriented programming, a method is a subroutine that is exclusively associated either with a class (in which case it is called a class method or a static method) or with an object (in which case it is an instance method). Like a subroutine in procedural programming languages, a method usually consists of a sequence of programming statements to perform an action, a set of input parameters to customize those actions, and possibly an output value (called the return value) of some kind. Methods provide a mechanism for accessing and manipulating the encapsulated data stored in an object." Taken from: http://en.wikipedia.org/wiki/Method_(computer_science)


Method and Constructor Overloading
"Function overloading or method overloading is a feature found in various programming languages such as Ada, C#, C++, D and Java that allows the creation of several methods with the same name which differ from each other in terms of the type of the input and the type of the output of the function." Taken from: http://en.wikipedia.org/wiki/Function_overloading

Conditional Statements (if-else-then)
"In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. Apart from the case of branch predication, this is always achieved by selectively altering the control flow based on some condition." Taken from: http://en.wikipedia.org/wiki/If_statement

Conditional Statements (Case and Switch)
"Switch statements (in some languages, case statements) compare a given value with specified constants and take action according to the first constant to match." Taken from: http://en.wikipedia.org/wiki/If_statement

"Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multiway branch (or "goto", one of several labels). The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit), offering the potential of faster execution through compiler optimization." Taken from: http://en.wikipedia.org/wiki/Switch_statement

Control Flow Statement (For loop)
"In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement.

Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. For loops are also typically used when the number of iterations is known before entering the loop." Taken from: http://en.wikipedia.org/wiki/For_statement

Control Flow Statement (While loop)
"In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement.

The while construct consists of a block of code and a condition. The condition is evaluated, and if the condition is true, the code within the block is executed. This repeats until the condition becomes false. Because while loops check the condition before the block is executed, the control structure is often also known as a pre-test loop." Taken from: http://en.wikipedia.org/wiki/While_loop 

Console Application
"A console application is a computer program designed to be used via a text-only computer interface, such as a text terminal" Taken from: http://en.wikipedia.org/wiki/Console_application


So, what do you think? interested? :D
I will start with C# then Zonnon because those are the ones I was using when I got the idea of working on this new series.

stay tuned!
bytes!

1 comment: