{"id":95,"date":"2008-03-06T22:45:39","date_gmt":"2008-03-06T17:15:39","guid":{"rendered":"http:\/\/www.bin-co.com\/blog\/2008\/03\/learning-a-new-programming-language-the-hello-world-method\/"},"modified":"2008-03-06T22:45:39","modified_gmt":"2008-03-06T17:15:39","slug":"learning-a-new-programming-language-the-hello-world-method","status":"publish","type":"post","link":"https:\/\/www.bin-co.com\/blog\/2008\/03\/learning-a-new-programming-language-the-hello-world-method\/","title":{"rendered":"Learning a New Programming Language: The &#8216;Hello World&#8217; Method"},"content":{"rendered":"<p><img class=\"intro\" align=\"right\" src='http:\/\/www.bin-co.com\/blog\/wp-content\/uploads\/2008\/03\/hello.jpg' alt='Hello World Learning' \/><\/p>\n<p class=\"intro\">Learning a new programming language is fun &#8211; and essential if you want to stay competitive. I try to learn new languages all the time(my latest target is Haskell). As a result, I have a system to make learning new languages easier. This is for people who already know a programming language and want to learn another one.<\/p>\n<h2>Requirements<\/h2>\n<h3>What you must have&#8230;<\/h3>\n<dl>\n<dt>The interpreter\/compiler of the language you are learning<\/dt>\n<dd>You must be able to run the program after writing it.<\/dd>\n<dt>Language reference manual\/documentation<\/dt>\n<dd>Usually found at the site of the language. Put a shortcut to that on your desktop &#8211; because you will be using this all the time.<\/dd>\n<dt>An easy to follow tutorial<\/dt>\n<dd>Search for it in Google. If you find one and its hard to follow, ditch it and get another. You may not need this &#8211; as manual for most languages have a tutorial in them.<\/dd>\n<dt>A &#8216;todo&#8217; project that you cannot live without<\/dt>\n<dd>This is what forces you to learn the language &#8211; more on this later.<\/dd>\n<\/dl>\n<h3>What I would recommend you have&#8230;<\/h3>\n<p>Having this will help you learn &#8211; but its not necessary.<\/p>\n<dl>\n<dt>A Net Connection<\/dt>\n<dd>You may want to ask your doubt on the IRC channel. Or on a forum. Or to google the error message you got.<\/dd>\n<dt>A Decent IDE<\/dt>\n<dd>This may make your job easier &#8211; but if you have notepad, that&#8217;s enough.<\/dd>\n<\/dl>\n<h3>What you don&#8217;t need<\/h3>\n<dl>\n<dt>Books on the language<\/dt>\n<dd>You may need a book to master the language &#8211; but you don&#8217;t need one to learn it.<\/dd>\n<dt>A Teacher<\/dt>\n<dd>Its helpful to have someone to clarify the doubts you may have &#8211; but <strong class=\"highlight\">no one can teach you anything you cannot learn yourself<\/strong>.<\/dd>\n<\/dl>\n<h2>The Hello World Script<\/h2>\n<p>First you have to create the &#8216;hello world&#8217; application. This is more than just &#8216;print &#8220;Hello World&#8221;&#8216; &#8211; it will act as a <strong class=\"highlight\">cheatsheet for you until you familiarize yourself with the language<\/strong>. The point of this application is to use <strong class=\"highlight\">all the most commonly used elements of a language and putting it in a single place<\/strong> so that you can refer to it later.<\/p>\n<p>Your first job is to go to the tutorial, the manual and the internet until you create the Hello World application with the following stuff in it. I will provide an example &#8211; how the &#8216;hello world&#8217; application will look in PHP.<\/p>\n<p>After each step run the application and make sure it works.<\/p>\n<dl>\n<dt>Print &#8220;Hello World\\n&#8221;<\/dt>\n<dd>\n<p>Why do you think we call it a hello world application? Write the code to <strong class=\"highlight\">print a string &#8216;hello world&#8217; and save it<\/strong> to a file. Now <strong class=\"highlight\">run it using the interpreter<\/strong> &#8211; and make sure it works &#8211; see it in action.<\/p>\n<pre><code class=\"php\">\n\/\/ Printing(IO)\nprint \"Hello World!\\n\";\n\t<\/code><\/pre>\n<\/dd>\n<dt>Comments<\/dt>\n<dd>\n<p>Put a single line comment(like \/\/) on top of the &#8220;print &#8216;hello world'&#8221; code and if your language supports it, a multiline comment(\/* &#8211; *\/) as well. Run the script to make sure the comments work as advertised.<\/p>\n<pre><code class=\"php\">\n\/\/ Single line comment.\n\/*\nMulitline comment.\n*\/\n\t<\/code><\/pre>\n<p>I was just kidding about running the script to test the comments &#8211; you did&#8217;nt do it, did you?<\/p>\n<\/dd>\n<dt>Use a variable(a string and an integer) and a concatenation operator.<\/dt>\n<dd>\n<p>Create a variable called name(string) &#8211; and give it your name as the value. Create another integer variable and give it the current year. You don&#8217;t have to find it programatically &#8211; just put it as 2008 or something. Now you have to print out the a string &#8220;Hello, &lt;name&gt; &#8211; Welcome to year &lt;$year&gt;&#8221;. This string will let you concatenate two different type variable to a string &#8211; and it makes me feel like a time traveler ;-).<\/p>\n<p>Yeah &#8211; the value of variable name should appear at &lt;name&gt;. Use the concatenation operator if possible &#8211; in PHP, write<\/p>\n<pre><code class=\"php\">print \"Hello, \" . $name . \" - welcome to year \" . $year;<\/code><\/pre>\n<p>instead of <\/p>\n<pre><code class=\"php\">print \"Hello, $name - welcome to year $year\";<\/code><\/pre>\n<p>This is because different languages have different concatenation operators. The other stuff is the same almost universally. + refers to addition. &#8211; refers to subtraction. * is multiplication. My point is that <strong class=\"highlight\">if you know the operators for one language, you know the operators for almost all languages<\/strong>. But the concatenation operator differs from language to language &#8211; its &#8216;+&#8217; in javascript, ruby. Its &#8216;.&#8217; in Perl, PHP, etc.<\/p>\n<p>There are exceptions to this rule &#8211; &#8216;=&#8217; is the equality operator in SQL while it all other language its &#8216;==&#8217;. The assignment operator is &#8216;:=&#8217; in pascal &#8211; in all other it is &#8216;=&#8217;. If you notice any difference in any operator write some code using that operator in this section.<\/p>\n<\/dd>\n<dt>Use if\/else if\/else<\/dt>\n<dd>\n<p>Create something like this pseudo code&#8230;<\/p>\n<pre><code class=\"pseudo\">if (year &gt; 2008) {\n\tprint \"Welcome to the future - yes we have flying cars!\"\n}\nelse if(year &lt; 2008) {\n\tprint \"The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!\"\n}\nelse {\n\tprint \"Anything wrong with your time machine? You have not gone anywhere, kiddo.\"\n}<\/code><\/pre>\n<p>In PHP, this section will look like this&#8230;<\/p>\n<pre><code class=\"php\">\/\/If,else conditions\nif ($year &gt; 2008) {\n\tprint \"Welcome to the future - yes, we have flying cars!\";\n}\nelse if($year &lt; 2008) {\n\tprint \"The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and decent, stay away from your parents!\";\n}\nelse {\n\tprint \"Anything wrong with your time machine? You have not gone anywhere, kiddo.\";\n}\nprint \"\\n\";\n\t<\/code><\/pre>\n<p>If you use switch(and the new language supports switch), you can write an example of switch here. <\/p>\n<\/dd>\n<dt>Print &#8216;Hi there!&#8217; 3 times using a for loop<\/dt>\n<dd>\n<p>A PHP example of using for loop(my all time favorite loop)&#8230;<\/p>\n<pre><code class=\"php\">\/\/ For loop\nfor($i=0; $i&lt;3; $i++) {\n\tprint \"$i) Hi there!\\n\";\n}\n\t<\/code><\/pre>\n<\/dd>\n<dt>Create a list(array) and iterate through it using while\/for\/foreach loop.<\/dt>\n<dd>\n<p>I am using a while loop here &#8211; to keep things different&#8230;<\/p>\n<pre><code class=\"php\">\n\/\/Numerical Array, While\n$rules = array(\n\t'Do no harm',\n\t'Obey',\n\t'Continue Living'\n);\n$i = 0;\nwhile($i&lt;count($rules)) {\n\tprint \"Rule \" . ($i+1) . \" : \" . $rules[$i] . \"\\n\";\n\t$i++;\n}\t<\/code><\/pre>\n<\/dd>\n<dt>Create a hash(associative array) and iterate through it.<\/dt>\n<dd>\n<p>If the language you are going to learn have &#8216;foreach&#8217;, this maybe the best time to use it&#8230;<\/p>\n<pre><code class=\"php\">\/\/ Associated array, foreach\n$associated = array(\n\t'hello'\t=>\t'world',\n\t'foo'\t=>\t'bar',\n\t'lorem'\t=>\t'ipsum'\n);\nforeach($associated as $key => $value) {\n\tprint \"$key: $value\\n\";\n}<\/code><\/pre>\n<\/dd>\n<dt>Create a function with an argument and a return.<\/dt>\n<dd>\n<p>If you need, you can create an example for call by reference, optional arguments, default argument value, variable argument count etc. as well &#8211; but they are not essential. Make sure you include the code to call a function as well.<\/p>\n<pre><code class=\"php\">\/\/ Function, argument, return, call\nfunction hello($name) {\n\treturn \"Hello \" . $name;\n}\n$hello_string = hello(\"Binny\");<\/code><\/pre>\n<\/dd>\n<\/dl>\n<h3>Optional Code<\/h3>\n<p>I don&#8217;t usually included some of these in my &#8216;hello world&#8217; scripts &#8211; but they can be useful in some cases. Some may find them useful &#8211; so if you think you are going to use any of these, include it in your file.<\/p>\n<dl>\n<dt>Class, Objects, Member variables and functions<\/dt>\n<dd>\n<p>If you are a big fan of OOP, create the code for a class. Make sure it has all the following elements&#8230;<\/p>\n<ul>\n<li>At least one member variable<\/li>\n<li>At least two member functions<\/li>\n<li>Access the member variable and the other function from within a member function.<\/li>\n<li>Create an object of this class<\/li>\n<li>Call a member function using the object.<\/li>\n<\/ul>\n<pre><code class=\"php\">\/\/ One for the OOP fanboys - Class, members, objects and stuff.\nclass Movie {\n\tpublic $name = '';\n\tpublic $rating = 0;\n\t\n\tfunction __construct($name) {\n\t\t$this-&gt;name = $name;\n\t\t$this-&gt;rateMovie();\n\t}\n\tfunction rateMovie() {\n\t\t$this-&gt;rating = (strlen($this-&gt;name) % 10) + 1; \/\/IMDBs rating algorithm. True story!\n\t}\n\t\n\tfunction printMovieDetails() {\n\t\tprint \"Movie : {$this-&gt;name}\\n\";\n\t\tprint \"Rating : \" . str_repeat('*', $this-&gt;rating) . \"({$this-&gt;rating})\\n\\n\";\n\t}\n}\n\/\/Create the object\n$ncfom = new Movie(\"New Country for Old Men\"); \/\/It's a sequel!\n$ncfom-&gt;printMovieDetails();\n<\/code><\/pre>\n<\/dd>\n<dt>File IO<\/dt>\n<dd>\n<p>Include this code &#8211; it is sure to come in useful someday. Unless you are learning JavaScript.<\/p>\n<pre><code class=\"php\">\/\/ File IO\n\/\/ File reading, easy method...\n$contents = file_get_contents('hello.php');\n\/\/ Writing to a file\n$file_handle = fop<span><\/span>en('\/tmp\/hello.txt', 'w');\nfputs($file_handle, \"Hello World\");\nfclose($file_handle);\n\t<\/code><\/pre>\n<\/dd>\n<dt>Command Execution<\/dt>\n<dd>\n<p>I tend to include this &#8211; but it may be unnecessary for you.<\/p>\n<pre><code class=\"php\">\/\/ Command Executing\nprint `ls`; \/\/Execute the command 'ls' and print its output<\/code><\/pre>\n<\/dd>\n<dt>Regular Expressions<\/dt>\n<dd>\n<p>I always include them &#8211; I am a big fan of regular expressions. I have lost count of how many times <a href=\"http:\/\/xkcd.com\/208\/\">they have saved the day<\/a>.<\/p>\n<pre><code class=\"php\">\/\/ Regular Expressions\n$string = \"Hello World\";\nif(preg_match('\/^Hell\/', $string)) print \"Yup - its evil\\n\";\nprint preg_replace('\/l([^l])\/', \"$1\", $string); \/\/Remove an 'l' from both words. Should print 'Helo Word'\n\t<\/code><\/pre>\n<\/dd>\n<\/dl>\n<h3>Specialized Code<\/h3>\n<p>The language you learn may have a specialized field which we may have not used yet. If so, put it down here.<\/p>\n<p>If you are learning javascript, write some code to access DOM nodes here. If you are using PHP, database connectivity will go well here. If you are learning any web server side languages, write the code to fetch the POST\/GET request values. For PHP, it may look something like this&#8230;<\/p>\n<pre><code class=\"php\">\n\/\/ Database connectivity(native)\nmysql_connect('localhost', 'root', '') or die(\"Cannot connect to the Database server\");\nmysql_select_db('Data') or die('Could not find a database called \"Data\"');\n\n\/\/ Executing Query\n$sql_handle = mysql_query('SELECT name,url,description FROM Comment LIMIT 1') or die('Query Error: ' . mysql_error());\nwhile($row = mysql_fetch_assoc($sql_handle)) {\n\tprint \"Name\t:\t$row[name]\\n\";\n\tprint \"URL\t:\t$row[url]\\n\";\n\tprint \"Desc\t:\t$row[description]\\n\\n\";\n}\n\/\/ Just a note here - if you are going to use PHP with database in a production system(and you will, trust me), use a Database abstraction layer rather than the above mentioned native methods.\n\nprint $_REQUEST['username']; \/\/ Method to get the value of the field 'username' after a form submit. Will not work at CLI execution\n\t<\/code><\/pre>\n<h2>Project<\/h2>\n<p>Next thing you need is <strong class=\"highlight\">a project &#8211; which you are going to build using the new language<\/strong>. Some points that you have to look for when choosing the new project&#8230;<\/p>\n<ol>\n<li>It should not be a huge project.<\/li>\n<li>At the same time, it should not be something trivial.<\/li>\n<li>There must be something about the project which would force you to finish it.<\/li>\n<\/ol>\n<p>The third point needs a little explaining. You should not just pick any project that pops into your mind. It must be something that you need &#8211; something you could not live without. If it is important to you, you will have a <strong class=\"highlight\">motivation to finish the project<\/strong>. Otherwise, you will drop the project at the first sign of trouble. But if its important to you, you will spend more time on it &#8211; you will look at documentation, post the problem in forums, ask others in IRC channels. And eventually, you will fix the problem. And you will finish the project.<\/p>\n<p>While working on the project, keep the &#8216;hello world&#8217; open &#8211; you will find yourself referring to it often.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Learning a new programming language is fun &#8211; and essential if you want to stay competitive. I try to learn new languages all the time(my <a class=\"mh-excerpt-more\" href=\"https:\/\/www.bin-co.com\/blog\/2008\/03\/learning-a-new-programming-language-the-hello-world-method\/\" title=\"Learning a New Programming Language: The &#8216;Hello World&#8217; Method\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,21,30],"tags":[137,162,164,210,222,306],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-opinion","category-php","category-web-development","tag-hello","tag-language","tag-learning","tag-php","tag-programming","tag-world"],"_links":{"self":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":0,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}