When I started programming as a kid the www wasn't around.
This meant I had to use whatever me or my friends happened to have available. And learning was hard too, since my friends didn't know much about coding, and study material wasn't easily available (especially since I was a kid and didn't know much English).
I ended up learning some very banal coding in BASIC on my friends C64 (He had a diskette station add-on, which was awesome compared to the standard cassette-deck!).
I really only knew how to print out text to the screen and accept number input from the user, as well as the goto-command.
|
The "Hello, World!" program in BASIC on C64 |
Once I actually managed to get a book from the bookstore (actually just a thin pamphlet really) about C/C++ programming, I had no idea I needed a compiler (or even what it was), and even if I did, I would have no way to get one, since when it came to software, I was limited to whatever my friends and I happened to have.
(The' /C++' part in the title of pamphlet was actually added with a marker by the employees of the bookstore, it was actually just an introduction of C).
All this also meant I believed all programming languages was fundamentally different, and that you really needed to re-learn everything from scratch if you ever switched languages.
That may actually be somewhat true if you compare languages based on fundamentally different principles, but if you compare different Object Oriented language for instance, the similarities are much more than I would have imagined back then. A robust understanding of one usually makes it really easy to understand another.
That being said though, as a beginner it is probably best to deepen your understanding of one, before moving on to learn others, just to avoid confusion.
At the very beginning I thought I would compare a very basic program written in some different languages, to make the point clear that very much is rather similar in different languages.
The language you choose to dive deeper into is very much a question of taste, but also a question of what you would like to use your programming skills for.
If you plan to make homepages for instance, you definitely need to learn HTML, JavaScript, and maybe some kind of server language like PHP (Although it is quite possible to use other languages such as Ruby, if you really want).
The languages I've chosen for this 'lesson' is strictly due to the fact that you won't have to install anything on your computer, but there are of course countless other languages to choose from.
XML
I would like to start by introducing the very basics of XML, mainly to demonstrate the concept of indentation.
XML stands for 'Extensible Markup Language', and is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.
You should start the document with a line that explains to whoever reads the file that it is indeed an XML file, like so:
<?xml version="1.0" encoding="UTF-8"?>
(But don't worry too much about what that really means right now)
Simply put, XML is a way to organize things. It uses start tags looking like: <some_stuff>
, and end-tags looking like </some_stuff>
. Every start tag Must have an end tag.
If for example you want to organize your library of books, it might look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book>Hitchhiker's guide to the galaxy</book>
<book>Neuromancer</book>
<book>Out of the Silent Planet</book>
</library>
This assumes you only have 3 books...
However what if you want to save more information of each book, for instance the author?
Then we will add new tags inside the 'book' tags, just as the 'book' tags themselves is inside the 'library' tags:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book><title>Hitchhiker's guide to the galaxy</title><author>Douglas Adams</author></book>
<book><title>Neuromancer</title><author>William Gibson</author></book>
<book><title>Out of the Silent Planet</title><author>C. S. Lewis</author></book>
</library>
It's starting to get a little messy isn't it?
What if we like to add more information of the books? Or information on the authors even?
Let's say we want to add a little something telling if the author is alive or not. This of course should be inside the 'author' tag, rather than the 'book' tag, since it pertains to the authors, not the books.
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book><title>Hitchhiker's guide to the galaxy</title><author><name>Douglas Adams</name><status>Deceased</status></author></book>
<book><title>Neuromancer</title><author><name>William Gibson</name><status>Alive</status></author></book>
<book><title>Out of the Silent Planet</title><author><name>C. S. Lewis</name><status>Deceased</status></author></book>
</library>
As you can see it quickly becomes almost unreadable to the human eye, although a computer would have no problem finding the relevant information here.
This is an example of code with really bad form. This is important if you, or other programmers need to view or edit the code. The first step is obviously to use newline a little more generously:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book>
<title>Hitchhiker's guide to the galaxy</title>
<author>
<name>Douglas Adams</name><status>Deceased</status>
</author>
</book>
<book>
<title>Neuromancer</title>
<author><name>William Gibson</name><status>Alive</status>
</author>
</book>
<book>
<title>Out of the Silent Planet</title>
<author><name>C. S. Lewis</name><status>Deceased</status>
</author>
</book>
</library>
That's a little better, but it's still not as easy as see the organization of the file as it could be. This is where indentation comes in. To indent code means to space it out in a way so you can easily see what 'belongs' to what. In other words, in this example, that the title and the author is an aspect of the book, whereas the 'status' is an aspect of the author.
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book>
<title>Hitchhiker's guide to the galaxy</title>
<author>
<name>Douglas Adams</name>
<status>Deceased</status>
</author>
</book>
<book>
<title>Neuromancer</title>
<author>
<name>William Gibson</name>
<status>Alive</status>
</author>
</book>
<book>
<title>Out of the Silent Planet</title>
<author>
<name>C. S. Lewis</name>
<status>Deceased</status>
</author>
</book>
</library>
Now it is much easier to see that 'name' and 'status' belongs to 'author', 'author' and 'title' belongs to 'book' and 'book' belongs to 'library'.
In many programming languages indentation is just good form that makes the code easier for humans to read, but some languages require correct indentation to work at all!
Hello World! x 13
Usually the first program people write in any language is one that simply displays the text "Hello World!". This is a good way to make sure all the basics are set up properly and in case of compiling languages, that the compiler is installed and set up correctly.
Here we will look at this simple program written in different languages, to see that the basics of most languages are the same.
I have chosen these languages because it doesn't require you to install anything to try them out, but rather your computer is already capable of running them, or you can use interactive IDEs online.
Batch scripting
If you have Windows, go to your Documents folder and create a new Text Document.
Copy and paste the following text:
echo Hello World!
Now change the filename to hello.bat
Press the windows button on your keyboard and type cmd
then press enter.
This will open the command prompt. Now type cd Documents
and press enter, this will take you to your Documents folder, where the file was created.
Now simply type hello.bat
and press enter to run the program.
See? It wrote "Hello World!" to the command prompt!
Batch programming is basically just executing command prompt commands. Simply typing echo Hello World!
in the command prompt will have the same effect.
This was how you usually administered DOS computers, but Windows have added another scripting language for administrative scripts.
VBScript
(This still applies to Windows computers)
Again navigate to your documents folder and create a new Text Document and copy and paste the following:
Wscript.echo "Hello World!"
Now save and rename the file to hello.vbs
Simply double-click the file to run it.
A pop-up window displayed the text "Hello World!".
Bash scripting
Another way (which is probably what you want to do if you use the online terminal) is to type cat > hello.sh
then enter.
now type the following:
echo Hello World!
then press ctrl-c
to exit cat (or save as usual if you didn't use the cat method).
Now in Unix/Linux/BSD you will first have to add a permission to execute the file before you can run it.
Simply type the following in the terminal window:
chmod +x hello.sh
Now to run the program type:
./hello.sh
And "Hello World!" appeared on the command line!
You might have noticed the similarities with Batch scripting, and just like the command line in Windows, you can get the same result by simply typing echo Hello World!
in the terminal window.
Python
The "Hello World" program is already typed in for you (named main.py):
# Hello World program in Python
print "Hello World!\n"
Notice the line starting with #. This indicates to Python that it is a comment, Python ignores lines beginning with a pound sign, and these are used to write informative things to others working with your code (or yourself).
The command print tells Python that the things in quotes following it should be output to the terminal. The \n is the code for new line.
Just push the "Execute" button to test the code.
Python 3
This is almost identical to the previous example, but this shows that the standard syntax for languages sometimes evolve, and this more modern standard of Python requires the text that is an argument of the 'print' command to be enclosed in parenthesis, and the row to be ended with a semicolon.
No \n is required any longer; the default behavior of 'print' is to end with new line.
# Hello World program in Python
print ("Hello World!");
Ruby
Again the pound sign is used for comments, but instead of 'print', 'puts' is used to output text.
Like Python 3 the line must end with semicolon, but the argument to 'puts' does not need to be inside parenthesis.
# Hello World Program in Ruby
puts "Hello World!";
Perl
The file is named main.pl, since pl is the file format for Perl script.
This code is almost identical to the first Python script, except the line ends in a semicolon.
But don't be fooled by the similarities of some syntax in different languages, since other things can be very different.
#
# Hello World Program in Perl
#
print "Hello World!\n";
Now move on from scripting languages, and continue to some compiled languages.
The difference in the online IDEs is that you must click the 'compile' button before the 'execute' button.
C
The file is named main.c, since c is the file format for C source files. After compiling an executable file simply named main will be created.
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
As you can see this is a bit more complex than the script languages.
In C the pound symbol is NOT used for comments.
The first line includes the standard input-output header file, which makes the program able to write out text to the terminal.
Everything within the brackets in the int main()
function is executed when the program is launched.
As you can probably guess printf
is used to write text to the terminal, and just as in previous examples \n means new line.
The return 0;
line simply exits the program.
Remember what was said earlier about indentation?
This program could be written like this and still work:
#include <stdio.h>
int main(){printf("Hello, World!\n");return 0;}
But it is Really bad form, and if you insisted on writing code like this you would probably get fired (or at least hated). Admittedly it isn't that hard to read in this tiny example, but imagine if we did this in really large programs.
Java
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
}
}
You don't really need to understand the details of the line public static void etc. Just remember it must be included and what's inside that function (inside the brackets) is what the program will execute.
The file is named HelloWorld.java, in other words the same as the class with the file format .java added.
C++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}
You might notice this is somewhat similar to C, but you don't have to explicitly add the .h to iostream and the line writing the text looks a bit different.
The source file is named main.cpp.
C#
using System.IO;
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}
Haskell
main = putStrLn "hello world"
Lolcode
This is an example of a programming language that is made more as a joke, rather than one that's meant to be used effectively for actual projects. It uses syntax inspired by 'lolspeak'.
Nevertheless it is probably possible to make serious application in this language.
HAI 1.2
VISIBLE "HAI, WORLD!!!1!~"
KTHXBYE
Conclusion
As you can see many languages are rather similar.
But you are probably wondering which one you should learn first.
If there is any language you find particularly interesting for whatever reason, that's probably what you should start with. After all, unless you feel motivated to learn it will be hard to put in all the hours required to acquire any skill.
If you want to make homepages you should learn HTML, CSS, and Javascript (despite the name, it has nothing to do with Java). If you also want to learn server-side programming I would recommend PHP and SQL.
If all you want to do is to automate and simplify administrative tasks you should probably learn Batch scripting and VBScript if you administer Windows systems, and Bash and something like Perl if you administer Linux systems.
If you want to make programs in general I would recommend to start out with learning structured programming in C, and then move on to an Object-oriented language like Java or C# for instance.
There are also functional languages like Lisp and Haskell, but they aren't used as widely as object-oriented language, although most people who have learned a functional language claims it really improved their skills as a programmer.
Although not used as much any longer you might want to consider learning assembly if you want to force yourself to learn how computers work at a really low level.
Just remember that assembly is different for every architecture. For instance knowing assembly for the 8086 architecture doesn't mean you can write the same code for the ARM architecture.
From what I understand assembly is sometimes used in combination with other high-level languages if you for instance write an operative system. You can then write short snippets of assembly code when performance is crucial, and write the rest of the code in C, for instance.
Perhaps this is best saved until you understand the concepts in easier high-level languages though.
Most important of all: remember to have fun coding!
(Although considering how many problems we run in to in writing programs, programmers probably need to have slight masochistic tendencies).
Comments
Post a Comment