Pinterest Stumbleupon Whatsapp
Ads by Google

beginner programmingIn part 2 of our absolute beginners guide to programming, I’ll be covering the basics of functions, return values, loops and conditionals. Make sure you’ve read part 1 The Basics Of Computer Programming 101 - Variables And DataTypes The Basics Of Computer Programming 101 - Variables And DataTypes Having introduced and talked a little about Object Oriented Programming before and where its namesake comes from, I thought it's time we go through the absolute basics of programming in a non-language specific way. This... Read More before tackling this, where I explained the concepts of variables and datatypes. You won’t need to do any actual programming yet – this is all still theoretical and language-independent.

Next time, we’ll start putting all this into practice with some real code. If you’ve always promised yourself you’re going to do some programming some day, now is a great time to start.

To quickly recap, last time I explained what variables were and some of the basic types data they can store. You should be able to explain what the following datatypes are:

  • Character
  • String
  • Integer
  • Float
  • Boolean
  • Array

We also looked at the difference between strongly-typed, and weakly-typed programming languages; and the advantages and disadvantages of each. Lastly, let’s make sure you know the difference between statements of assignment, and of equality. Which of the following assigns variable B to variable A, and which tests if they have the same value?

  • a = b;
  • a == b;

Great! If you’ve come this far, that’s an amazing achievement and you’re well on your way to making your own software! Let’s move on today’s lesson.

Conditionals and Loops

There are three structural building blocks of a program that you need to know, and they are almost universally referred to as: IF, FOR, and WHILE.

Ads by Google

IF is one-time test. “IF a is true, then do this”. IF a isn’t true, the program ignores whatever comes next and carries on with the rest of the code. You can also provide an alternative with ELSE. “If a is true, then do this, ELSE do this”. It allows you to make decisions in the program depending on a variable. Remember the test for equality that we learnt about earlier?

That could be used in an IF clause, for example:

if(a==b)
print “a is equal to b”
else
print “a is not equal to b”

FOR is a way to loop over the same code a set number of times. There is no testing involved – it just repeats the same block of code however many times we tell it to.

WHILE is also a loop, but instead of performing the options a pre-determined number of times, it performs a test each time the loop is performed and continues to loop until the test fails. If the test continues to be true, it never stops looping. This can cause problems if you have a bug in your WHILE look, leading to unresponsive programs that crash in an endless loop.

beginner programming

These 3 basic structures create the traffic lights and road diversions that control the logical flow around an application.

Functions

A function is just a group of code that’s been given a name. By grouping a block of code together and naming it, we can re-use it later and throughout the application without having to rewrite the whole code block again. Not only does it save time and reduces the overall code size of an application, it also means that if there was something wrong, we would only need to change it in one place.

Nearly all programming languages come with a built in set of functions that you can use in your application. For instance, ECHO is a function found in many languages that displays some text on screen. There are usually also some functions to deal with reading and writing files, graphical or audible output, keyboard and mouse input, and mathematics. You can use all these built-in functions without knowing how they actually work – all you need to know is the correct function name, and the parameters it requires. Wait.. parameters?

Parameters and Return Values

We often refer to functions as “taking” certain variables and “returning” something back to us. It might help to consider a function as a machine you can’t necessarily see inside of. You can put things into it, and you might get something out again, but you don’t actually have to know what goes on inside the machine. The things you put in are the parameters – variables of data that the function will work with. The return value is the output of the function – the data that will be given back to you once it’s finished running through it’s logic.

beginner programming

In the example above, the “signature” of the function is one that accepts two variables (a and b), and returns one (c).

Sometimes functions won’t return any values at all, though it’s common practice in those cases just to return a Boolean value of true or false to show whether it was successful or not. If you called a function to save a file for example, you wouldn’t necessarily want any variables back, but you would like to know if the file was written correctly or if there was an error.

In all applications, variables and data are constantly being “passed around” through thousands of different functions, each of which performs it own distinct purpose in the big scheme of things.

Check What You Learnt Today

To recap today’s points, you should understand what the following do in any programming language, and how they control the flow of the application:

  • IF
  • FOR
  • WHILE

You should also understand what a function is, and what I mean when I say “this function accepts a string and returns a Boolean”.

That’s all for this lesson. Next time, we’ll try to put some of this knowledge into practice using real code to write a small application, though I haven’t decided which language to tackle first. If you have any requests, then perhaps you could post in the comments. What programming languages do you think are the most important today?

Image Credits: ShutterStock 1,2

  1. Ashwin starz
    December 10, 2014 at 1:44 pm

    good examples and explanations.........very useful for students and freshers.......thanks a lot

  2. Vivitech Ltd
    January 26, 2012 at 5:04 pm

    Good examples. For those of you who want to try a bit more advanced programming techniques, check out pluralsight.

  3. Vivitech Ltd
    January 26, 2012 at 3:09 pm

    That's a useful article, a good website to use for learning JavaScript for beginner is the code year website.

  4. Tripytrev
    December 15, 2011 at 2:50 pm

    Id like to know what the x++ means? And I thing Dev++ with C++ is a great beginning tool that way you could move on to hiphop, php sql programming together with C++ application programming.

    • Jeff Fabish
      December 15, 2011 at 4:03 pm

      TripyTrev,

      The "++" is called the 'increment operator', all it means is to add 1. So the statements 'x + 1' and 'x++' are functionally equivelent. There is also the 'decrement operator', which looks like 'x--', of course this subtracts one. C++ is a good example, as it's an improvement of the C programming language. So C++ is an incremented version of C (C + 1 = C++). 

      Does this clear it up?
      - Jeff

  5. Justin9
    November 1, 2011 at 5:51 pm

    Bro u should go for C#.

  6. Scutterman
    October 28, 2011 at 8:19 pm

    Another great article. I would have talked about code blocks before getting into the meat of what they can be used for, since that can help understanding.
    Also, I'm not sure how many languages have it, but the foreach loop is invaluable to me, I rarely ever use the standard for loop. But that may be down to the type of data I'm handling.

    Are you going to introduce variable scope in the next tutorial?

    An interesting point point is that echo isn't actually a function, it's a language construct (at least in PHP).

    • James Bruce
      October 29, 2011 at 3:45 pm

      Interesting. 

      Yup, good point, I totally missed the foreach loop. 

  7. Jeff Fabish
    October 28, 2011 at 7:44 pm

    Good article, James! There are two more common loops, do while's and for each loops. 

    Do Whiles: Unlike while's, a do while will execute at least once (even if the condition is false).
    For Each: A for each loop will iterate through every index in a given array. 

    // Do while:

    int x = 0;
    do
    {
    Console.WriteLine(x);
    x++;
    } while (x < 0);

    // For each

    string [] names = { "Make", "Use", "Of" };
    foreach(string name in names)
    {
    Console.Write(name];
    }
    // Prints: "MakeUseOf"

    • James Bruce
      October 29, 2011 at 3:46 pm

      Is do while that common? I've never personally used it, i dont know, I just just a general preference for straight up while loops...

      For Each - yep, totally missed that one. Good catch. 

      • Scutterman
        October 29, 2011 at 4:53 pm

        The do...while is occasionally useful. I use it when creating unique elements (usernames, urls, etc.) so I can create and verify the element before telling it to loop if the created element already exists somewhere. This means that the original creation code isn't duplicated in the loop.

      • Jeff Fabish
        October 29, 2011 at 7:51 pm

        You're right, it's not that common at all.

  8. Garybyron
    October 28, 2011 at 6:06 pm

    Part one link not found, update please

    • James Bruce
      October 29, 2011 at 3:46 pm

      done

  9. Karttikeya
    October 28, 2011 at 5:14 pm

    The link to Part 1 seems to be broken or something. Can't find the page.?

Leave a Reply

Your email address will not be published. Required fields are marked *