Saturday 28 September 2013

Branching and Looping Constructs

This, by far, is the most important thing in coding. Without learning this, you will never be able to write any meaningful program. And it brings a power to us that is not ultimate. But as close to it as possible.

Branching

What is branching?

Going to different branches of a program based on the truth or fallacy of a particular condition is called branching.

I will illustrate this to you using a Flow Chart as follows:

This is the flow chart to find the larger of two numbers.


This program is the one of the simplest that we will write. And we can see that based on the truth or fallacy of the condition, X > Y we need to output either X or else Y.

Thus we need branching.

We can achieve branching using three constructs:

  1. if
  2. if - else
  3. switch - case
if




if - else



switch - case




Looping

What is looping?

Doing a set of instructions a specified number of times or until a particular condition is satisfied.

The following flow chart will illustrate how to find the sum of all the numbers that the user enters until he enters a zero:
We can see that this program needs both looping and branching.

In this example, we need to go on taking input from the user until he enters a zero.

We can achieve looping through three constructs:

  1. for
  2. while
  3. do - while
for







while





do - while




Note that the do - while loop will always run once. No matter what the value of the condition. We may use this in some of our programs.

In this post, we saw the various constructs in C that allow us to branch from the flow of control of the program. And also the three looping constructs.

it is essential that you understand whatever was taught in this post very very well. Because this forms the basics of coding.


Sunday 22 September 2013

Input/Output Constructs

The second most important feature to know about in while learning any programming language. Input/Output is the way through which we will be interacting with the user. And this forms an integral part of any program that we write. We will be seeing the two most basic constructs in this post. scanf and printf. scanf is for taking input from the user using the standard input device, generally the keyboard and printf is for printing output on the console output. In most cases this is the screen of your machine, but it can be routed to your printer or a file too.

scanf


scanf stands for scan formatted string. It is the most commonly used method for input in C. Using scanf, we can accept any type of input. The syntax for input is as so:



In this the format string depends on the type of input that we want to accept from the user. A list of format strings is shown below.



The format strings can be nested. That is we can accept multiple variables in the same scanf statement.

Some examples of scanf are as so:



Note: Whenever accepting a variable from the user you need to put the & operator (address of operator -- we will be learning more about it) before the variable name. This passes the address to the scanf function so that it can store the value that was input by the user in that address. You don't need to put that when accepting a string because the name of the string is the name of an array and the name of an array stores a pointer to the address of the first element on the array. None of this should make sense to you right now. It will make much more sense when we get started on pointers.

printf

The most common way to print things on the screen for the user to read. It works in a very similar way to scanf. printf stands for print formatted string. And the format strings are the same. Here while printing a variable you don't need to put the ampersand before it. Not for variables and not for arrays.

Some examples are shown below:



In this post, we looked at the basic I/O constructs that C provides. Now, we are well equipped to write a program of basic utility. And we will start up on the coding! Yippee!

More on Variables and introducing Arrays

In this post we are gonna see one more Variable type. And then I will go on to introduce you to arrays.

Boolean

A Boolean variable can have only two values. true or false. It is very useful when we are making yes or no decisions. This will happen in a lot of programs that we will soon start to write. The following code shows you how to declare the Boolean variable and initialize it to some value. Note that the case of the keywords true and false must be the same.



Arrays

This is the first method for data aggregation. Using arrays we store same type of variables together. Suppose you want to store 50 values which are the marks of all the students in your class. You can of course go ahead and declare 50 variables, hopefully you will not run out of variable names! So, we can do that in a simpler way by doing:



This declares 50 float variables. And then you can store your values in them. Moreover, apart from the fact that declaring arrays is simple, you can also work with data in an array without much pain. You just use a loop(we will learn about  it soon!) to cycle through all the values. And thus you can easily find the sum, average, the students who have failed, grades of all the students with minimal code.

The following is the syntax to declare an array:



You can declare an array of any data type.

You have seen that to declare an array you use the square brackets. In case you want to access individual elements of the array then you use the square brackets again but you put the index inside the brackets.

I know you must be asking what the hell is the index! Well index is the identity of each element of an array. In C, Arrays are indexed starting from 0. Another weird thing about C! But what this means is that the marks of the first student can be retrieved by writing marks[0]. Now if you want the marks of the 19th student of the class then you simply do marks[18].

So, in case you want one particular item of the array then you put the index of that item in side the square brackets, which are preceded by the name of the array itself.

NOTE: In C, If you declare an array of size 50 and then access the 60th element as arr[59], then the compiler will not throw an error at you. Simply because while declaring the array the compiler does not store the size of array. So you should always be careful, and make sure that you never access an element whose index exceeds the size of the array.

Character Arrays

They are also called Strings. And strings are collections of characters. Just like normal arrays. But what is different is that strings are things like your name, the name of a place and so on. Thus, Strings refer to any collection of characters. They are represented by putting them in double quotes as so:


Note that a string is always terminated by a character known as the null character and shown as the '\0', pronounced "backslash zero" character. This character indicates that a string has ended. So in the above string, after the last "n" of my name, the compiler will automatically add a '\0' character. Thus if you declare a string of size 50 then you can store only 49 characters in them as the last character will be taken up by the '\0'.

One more thing to note about arrays is that the maximum size of an array is the number that you put inside the square brackets when you declare it. You may or may not use the whole of the array. You can use only a part of it but always bear in mind that the whole array takes up memory space.

You can define a string and then accept it from the user and so on. We will be seeing the code for doing that very soon.

In this post, we talked about the boolean type and got started on the concept of data aggregation via arrays.

Saturday 21 September 2013

Variables

What are Variables?

Variables are containers. They can be filled with anything. But there are some restrictions too.

Before using a variable you must always declare it. In C, you must initialize it with a name and a type. You may or may not provide an initial value for the variable that you have declared. For example you initialize a variable with type "integer", this means that throughout the program this variable can store only an integer.


In this code snippet, I have shown how to initialize an integer variable called number. In line 33 I have shown how to do it without an initial value. And in line 35, I have initialized it with a value of 4.

Types of variables:

These are the four most commonly used types.



  1. int : Used to store integer values. It generally takes up 4 bytes of space, but it depends on your machine. It can store negative and positive values(In case you don't know what an integer means!)
  2. float : Used to store Decimal numbers. 4.0, 5.63 etc. It generally takes up 4 bytes of space and this again depends on your machine.
  3. double : The advanced version of float. It also stores decimal point values but It can store them with higher precision and it has a larger range too.
  4. char : used to store a single character. it takes up 1 byte of storage. It can store any one character. Either on your keyboard or some other special character. You can look up the whole list on the internet. Just Google it in case you are interested.
We will be looking at some other types soon enough. But these are the four basic types that you must be totally familiar with before we can start doing anything. The following is the kind of things you can do with these four types:


So this was about the very commonly used variable types. We will delve into some more variable types in the next post and then we will go on to input/output constructs which will be a lot more interesting than this!

Some notes on Programming Languages

So, You have just seen the Hello World program in C.

This is one of the most basic programs that can be written with any language. I maintain a github repository(Refer to my blog on Github and Git, VCS etc) that has this same "Hello, World!" Program in many other programming languages.

Hello World

So, This is something that can be done everywhere. And this is the reason why I don't believe that there is one programming language that is better than all others. This is so because there are a lot of programming languages. Write down from Assembly Language which is the closest to the machine to languages like Perl, Python that are now commonly termed as "Very-High Level Languages". Coding in these languages is extremely simple and intuitive. Moreover, they have a whole bunch of libraries. These libraries make sure that for any application that you may think there will be a library so you will not have to code that from the beginning. You can simply reuse the code. We will talk a lot more about reusing code throughout this series. It is a topic that is often closely related with Plagiarism of code itself.

C is considered to be a High Level Language.

The levels of programming languages refer to the level of abstraction. Or in layman terms, They simply refer to the "metaphorical" distance you are, from the internals of the machine. Coding in Assembly Level Languages(I don't prefer to call them low level languages simply because you don't call something that is hard "Low Level"!) has some issues:


  1. Code starts to get machine dependent. It starts getting dependent on the specifications of the machine you are coding on.
  2. You need to remember a lot and that makes coding a lot harder than it really should be.

High Level languages overcome the first problem by creating cross-platform compilers. You take a C program and compile it on a Mac, Linux, Windows, Solaris machine using a GCC compiler on all the machines and you will always get the same output.

The second problem is solved to some extent. You do need to remember somethings, But they are fairly simple to remember. As we move on through the series about C we will see that we need to remember some basic rules. Some keywords et al.

This process of remembering is also automated in many IDE's today. IDE's generally have the whole bunch of keywords and functions that are there in a language stored in them so that whenever we want something we can pull it from there without having to Google it or refer to Textbooks.

So in this blog we saw some points on high-level languages, problems with coding in assembly languages, equality of programming languages. We will be seeing a lot more philosophy but for now lets get back to coding.

The basics of any programming language

What do you need to know to write a program of decent usability in any programming language?


As programmers, Most commonly we use the following:

  1. Input/Output Constructs
  2. Variables
  3. Loops
  4. File operations 
  5. Functions
These 5 features are paramount for knowing a programming language. You can write programs like:

  1. Finding out if a number is prime
  2. Finding out if a string is a palindrome
  3.  Store data about students, employees et al. And retrieve them at a later time.
Just by knowing how to use these 5 features.

Thus, whenever you start learning any programming language always remember that you need to know these 5 features before you start coding really.

There are many more features to a programming language. The most common one today being "Objects, Instances, Classes, Structures". In real life, When we start writing programs for clients, then these become the major area of inerest. But now, when we are learning to code, they take a backseat and often amateurs question why we need these at all. I was in a similar confusion when I was first taught about these. But later when I did find out about their utility, which was  long way down the road, I found out that it is really impossible to code any good application without Objects or in general without Object Oriented Programming.

So, For the initial part of this course, we will be concentrating on the five features that I have told above. Then as we move on to complete them, we will get started with OOP, and we will take it from there.

git, Versioning, Github et al

As soon as you start talking about coding, coding projects, applications some words that you may often come across are "git", "Subversion", "Version Control System" etc. So what is this?

First of all, let me warn you that it took me a long time to understand this. So it may take you some time too.

Versioning is the systematic management of the code that you have so that you have a project that is well documented in every sense. You can find out which line of code was added by which member of your team as easily as possible. And make sure that mistakes/bugs were introduced when some change was made then you can roll back the changes and go back to the version as it was before that bug started occuring. It often happens in Software engineering that many people are working on the same project and more often than not, many people are working on the same file. This would lead to a lot of confusion if there was no proper system to maintain a log about who is doing what. Enter Version Control System(VCS).

VCS maintains a log of historical events about the state of the project.

git is an open source versioning system. It is used by many big software companies. There are also some other systems like subversion., mercurial etc. I have personally used only git and I have loved it.

In git, Historical events are called commits. Each commit is associated with a timestamp and the identity of the person who performed the commit. This makes sure that no change goes unnoticed. And the project administrator or for that matter any person who is working on the project can pull up the list of commits that have happenned on one file, the whole project et al. All kinds of features such as branches, Merging branches etc are there on git.

Branches is something that I would like to make a special mention on. It often happens that during the development of a project, the development could go on a completely different track. These changes are not directly related with the project and are just auxillary chages that are required to change some file that is directly related with the project. At this time, creating a branch and then working on this branch rather than working on the master branch is often beneficiary.

Github is a free server to host git projects. You can sync projects directly from your PC to the Github server and then access it from anywhere using only a web browser. This also ensures that you have asecure backup of all your projects(also called "repositories") on the server. Though the free version of Github provides only free repositories, it is a very good way to make sure you have secure backup. These public repos are available to anyone who enters the correct search keyword! This does not make much difference to the new programmer(like you!). But it may make some amount of difference to the advanced programmer, and generally advanced programmers upgrade so as to get Private repos.

So, this was all about git, github, versioning.