{"id":449,"date":"2011-04-12T08:30:19","date_gmt":"2011-04-12T14:30:19","guid":{"rendered":"http:\/\/blog.shadowkatmandu.net\/?p=449"},"modified":"2011-04-12T08:30:19","modified_gmt":"2011-04-12T14:30:19","slug":"an-introduction-to-computer-programming-part-2","status":"publish","type":"post","link":"https:\/\/blog.shadowkatmandu.net\/?p=449","title":{"rendered":"An Introduction to Computer Programming, Part 2"},"content":{"rendered":"<p>A <a title=\"What is a program?\" href=\"http:\/\/blog.shadowkatmandu.net\/?m=20110208\" target=\"_blank\">couple of months ago<\/a>, I wrote about what a program is and explained a little about the different kinds of computer programs.\u00a0 Today I want to extend that lesson a little by talking about some basic syntax concepts of programming.<\/p>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s start by defining what I mean by syntax.\u00a0 Programming languages, like spoken languages, have rules about how they are used.\u00a0 In spoken English, for example, the basic structure of the simplest sentence is to have a noun followed by a verb and concluded with a period.\u00a0 In a programming language, most lines of code have a <em>keyword<\/em> or a set of mathematical operators, and many lines have <em>variables<\/em> in them.\u00a0 Just as English uses a period to designate the end of a sentence, programming languages have some way of delimiting the end of a line of code.\u00a0 The rules which govern that delimitation plus how operators or keywords or the like are used are the syntax rules of the language.<\/p>\n<p>&nbsp;<\/p>\n<p>No two programming languages are exactly alike&#8211;that&#8217;s by definition&#8211;but they all have similarities.\u00a0 I&#8217;ll begin with the end:\u00a0 Every programming language, as I stated, has a different way of showing where the end of a line of code is.\u00a0 For some languages, this is as simple as a CRLF, also known as a Carriage Return &#8211; Line Feed.\u00a0 In simpler terms, it means hitting the Enter key at the end of a line.\u00a0 Carriage Return is a bit of a misnomer, dating from the days of using typewriters when one would actually move the typewriter&#8217;s physical carriage to the start of a new line.\u00a0 The carriage was returned to its starting point; thus a carriage return.\u00a0 The mechanism for this also rolled the paper up so typing would continue on a new line rather than typing over the last one, an action called a line feed.\u00a0 Computers separate these actions into two pieces, and it is possible to have an LF without a CR and vice versa.\u00a0 (Although having one without the other can be a little ugly on the screen!)<\/p>\n<p>&nbsp;<\/p>\n<p>Most modern languages, however, use a semi-colon as their delimiter.\u00a0 This is both powerful and problematic.\u00a0 It&#8217;s powerful because one can put several instructions on a single line, but doing so can make the code harder to read.\u00a0 Sure, we&#8217;re used to paragraphs in written speech, using periods as a delimiter, but most written speech is not a list of instructions.\u00a0 Lists of instructions are often put on separate lines for clarity.\u00a0 Consider this example:<\/p>\n<p>&nbsp;<\/p>\n<p>Method 1:<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 Open the cookie package.<br \/>\n2.\u00a0 Remove part of the cookie dough and put it on the cookie sheet.<br \/>\n3.\u00a0 Place the cookie sheet in the oven.<\/p>\n<p>&nbsp;<\/p>\n<p>Method 2:<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 Open the cookie package.\u00a0 2.\u00a0 Remove part of the cookie dough and put it on the cookie sheet.\u00a0 3.\u00a0 Place the cookie sheet in the oven.<\/p>\n<p>&nbsp;<\/p>\n<p>The second method works, but the first is clearer.\u00a0 Meanwhile, I am now hungry for cookies.<\/p>\n<p>&nbsp;<\/p>\n<p>Much like our written language, programming languages have delimiters beyond what is used to indicate the end of a statement.\u00a0 Just as we delimit what we say into paragraphs, programming languages like to delimit blocks of code.\u00a0 This is primarily done through the use of curly brackets (&#8216;{&#8216; and &#8216;}&#8217;).\u00a0 Some simply use a keyword for this kind of delimitation.\u00a0 For example, one language might have something like this:<\/p>\n<p>&nbsp;<\/p>\n<p>if (condition)<br \/>\n{<br \/>\nDo some things;<br \/>\n}<\/p>\n<p>&nbsp;<\/p>\n<p>While another language would have something like this:<\/p>\n<p>&nbsp;<\/p>\n<p>if condition<br \/>\nDo some things<br \/>\nend if<\/p>\n<p>&nbsp;<\/p>\n<p>&#8220;Do some things&#8221; is not literal; it is what programmers call &#8220;pseudo-code.&#8221;\u00a0 It&#8217;s not real code and no computer would understand it, but we humans can see it and understand it&#8217;s an abbreviation of sorts for regular computer code.<\/p>\n<p>&nbsp;<\/p>\n<p>The above examples bring to light the next pair of concepts in programming:\u00a0 Conditional statements and variables.\u00a0 However, I have already gone long on this post so I will save those concepts for next time.\u00a0 See you then.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A couple of months ago, I wrote about what a program is and explained a little about the different kinds of computer programs.\u00a0 Today I want to extend that lesson a little by talking about some basic syntax concepts of programming. &nbsp; Let&#8217;s start by defining what I mean by syntax.\u00a0 Programming languages, like spoken [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.shadowkatmandu.net\/index.php?rest_route=\/wp\/v2\/posts\/449"}],"collection":[{"href":"https:\/\/blog.shadowkatmandu.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.shadowkatmandu.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.shadowkatmandu.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.shadowkatmandu.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=449"}],"version-history":[{"count":1,"href":"https:\/\/blog.shadowkatmandu.net\/index.php?rest_route=\/wp\/v2\/posts\/449\/revisions"}],"predecessor-version":[{"id":450,"href":"https:\/\/blog.shadowkatmandu.net\/index.php?rest_route=\/wp\/v2\/posts\/449\/revisions\/450"}],"wp:attachment":[{"href":"https:\/\/blog.shadowkatmandu.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.shadowkatmandu.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.shadowkatmandu.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}