Thursday, February 14, 2013

A First Lesson in Programming

Yesterday I talked about teaching yourself programming. I said it's something anybody who understands "if/then" can do; you don't have to be a math whiz or a major-bigtime geek to learn to read and write code. I also said that today I'd present a first programming lesson. So let's get started.

a = 1;

What does this mean to you? If you're not already a programmer, it probably means "a equals one." But in the wonderful world of JavaScript, that's not what it means. It means "assign the numeric value of 1 to a (and from this point forward, treat any appearance of 'a' as if it were 1)."

In JavaScript (and Java and C++), "=" is the assignment operator. It doesn't mean "equals."

How then can you say "equals"? Consider this:

a == 1;

This is a perfectly legal (syntactically correct) JavaScript statement. Legal but useless. It means "a equals one." It's a useless statement in that it does nothing to 'a' and changes nothing in the state of the computer. It's the programming equivalent of neon gas; inert.

When would you want to use "a == 1"? Consider this statement:

if (a == 1)
   doWhatever( );


Notice that the top line is not a statement by itself. The semicolon comes at the end of the second line. Therefore the whole statement reads: "If the value of a is equal to 1, execute the function named doWhatever." (A function is just what you think it is: a named collection of statements that occurs elsewhere.) If a isn't equal to one, just skip the doWhatever() and do nothing.

Make sense so far? Good. In that case, it's time for a pop quiz. What's wrong with the following piece of code?

if (a = 1)
   doWhatever( );


Technically, there is nothing wrong with the syntax of this statement. It will execute without error. But it's not a good piece of code. Why? Consider what it says. It says "give the variable a the value 1, and if that value is true, execute the function doWhatever." In other words, "a = 1" sets a to one (whether that's what you intended or not). The "if" asks whether the value one is true, which it is, in the world of code. (Seemingly useless fact: In the land of JavaScript, any non-zero/non-null value will always be considered true.) Thus the top line of this statement will always be true and doWhatever() will always be called. You might as well leave out the top line and just call doWhatever(). Except, that's probably not what you wanted to do, because if it was, you would have written the code that way to begin with.

If that made any kind of sense, congratulate yourself. You've done your first bit of debugging.

Was any of it hard? Was any of it "rocket surgery"?

Let's recap. Here's what you learned:

1. A piece of code contains statements.
2. A statement ends with a semicolon.
3. You can have variables with names like 'a'.
4. The equals sign is actually an assignment operator.
5. But two equals-signs in a row means "equals."
6. The "if" keyword does what you think it does.
7. In JavaScript, a non-zero value is treated as true in the context of an "if."
8. There are things called functions, which are basically just named collections of statements.
9. Code can be buggy without containing illegal syntax! It can be syntactically correct, yet logically flawed. And the flaw can be hard to spot.

That's a huge amount to learn in one lesson. But it really wasn't that hard, right?

I hope this lesson gives you encouragement to continue on. Where should you go from here? I recommend that you start by reading more about JavaScript's data types. Then perhaps check out http://www.codecademy.com/learn for free structured online courses (on your choice of Ruby, JavaScript, or Python). If it starts to sound tedious, remember there's a lot of rote and tedium in the early stages of learning any language (whether it's French, Hebrew, Ruby, JavaScript, etc.), and you're bound to start to feel like you're doing a lot of wax on, wax off, at some point. But also remember: Like the karate kid, you'll eventually break through. And yes, the payoff is worth it.

12 comments:

  1. Well thought content impressed me! I am glad to check out sharing information and accordingly meet with the demands programming assignment expert

    ReplyDelete
  2. It isn't always against the law to seek help and thoughts from folks that hold revel in the topic or writing such paper. Custom Dissertation Help Online writer is the maximum asked professional dissertation writing carrier inside the recent records of academic writing services.

    ReplyDelete
  3. There have been instances while the garbage became accumulated in huge rubbish cans. Putting off this rubbish became a real task. Good garbage disposal reviews is accrued in big quantity after every few hours. As a result, it's miles essential to maintain it away from the kitchen then this is fine for all chef and housewife.

    ReplyDelete
  4. Those who are searching over internet IT assignment help can contact with us now. We are the best assignment writing service provider in melbourne, Australia. Our Academic assignment writers available 24*7 hours for the students, if you really want to need IT assignment help, java assignment help, programming assignment help, r programming assignment help, Computer science assignment help online at cheapest price and get high distinction grades.

    ReplyDelete
  5. Our models are an ideal solution for the delivery of containers or containers. Containers, Cabin Carriers, Managers Offices website, movable bathroom/toilets, Multistory Containers, Labor camps, Mobile Restaurants etc.. Porta Cabin Manufacturer and Supplier in Pakistan provides porta Cabins, site Managers Offices, movable bathroom/toilets, multistory Containers, Labor camps, Mobile Restaurants etc.bathroom / toilets

    ReplyDelete
  6. I'm a newbie on IT field. It's my first experience. Thank you for your first lesson. I can share with you some experience about online essay writers. For a long time I was one of them.

    ReplyDelete
  7. I got diagnosed with hpv, i have be dealing with this mess for the past 7yrs. On till i got review online about natural cure people testifies how they got cured with natural treatment. And i order the treatment, after taking it for two weeks i totally got cured with herbal treatment. I'm recommending you diagnosis with hpv should get this treatment via; dronokunherbalcure@gmail.com or whatsapp call.. +2349064844957

    ReplyDelete
  8. Hi! If you need custom research paper, here is the website to serve you in the best manners. Visit and look for yourself!

    ReplyDelete
  9. Whenever I came across any difficulty, I often used to search “who can do my assignment for me”, but, later on realised that there are many these type of services who promises a lot but does only a little. However, My Assignment Services is one such assignment provider who believe only in the quality. They are exactly the type of assignment service provider that I needed from a long time. By opting their services, you get assistance to a lot of subjects including their nursing assignment help, management, law, engineering, finance, economics, accounting, and a lot more. Therefore, you need not to be worry, instead go for My Assignment Services by visiting the link.

    ReplyDelete
  10. This is really a well-needed platform for the readers. well about last night, when i do programming so as usual i feel so hungry so i order pizza from Pizza Crown in Pasadena MD and you know what they provide best and fresh pizza at a very cheap price. also, give 10% off in dine and for order online!

    ReplyDelete
  11. I has suffered for Human papillomavirus HPV) for 2years, I was given some tablets at the hospital but I refused to take it, They said I have to be on it for life so I don't want take a drugs everyday for life. No point in taking medicine everyday when u won't get cure from it and I was advice to seek for natural herbal cure, after some time I found dr onokun is the most trustful herbalist that have herbs to cure wicked symptom's,I emailed dr onokun, for 2weeks been his patient he cured my (HPV) with his herbal. I only used his natural herbs for two weeks it was 100% cure. I'm not (HPV) patient anymore. I'm happy about it i finally got cured out of this mess been in my body for 2years. I also recommend you if you're living with (HPV) or herpes symptoms i also want you to be free contact dr onokun with the email attach to my post. Dronokunherbalcure@gmail.com

    ReplyDelete
  12. We support students seeking Essay Writing Services in the finance field. For this reason, we are dedicated towards full-time Best Paper Writing Service. We provide our excellent services at an affordable rate.

    ReplyDelete

Add a comment. Registration required because trolls.