Sunday, 26 July 2015

Insights into Programming - 1


published : 05/06/2013

This is the first article in our series, "Insights into Programming". This page is meant to enhance your understanding of the laws and mechanisms that lay behind the functioning of a programming language. This series will serve as an aide to "Baby steps into Programming". As we have done in our other series', we'll discuss three programming languages here also, namely C, C++ and Java.

Though, the fundamental laws that govern all the programming languages are similar but there are slight differences that exist and give every programming language it's unique character.

First of all let us take a look at programming terminology, it starts with these terms:
1) Keywords : These are a set or collection of words that convey a special meaning to the compiler. These are reserved for use by the compiler and whilst using IDEs, they are highlighted by the use of a specific color (white in Turbo C++ 3.0, navy blue in Dev C++ or NetBeans, red in C9, etc.) or emboldened. e.g Take the world if in any programming language. It is a conditional statement and is used to test a condition, say if( 3 < 5) and execute the statements in it's block, that is bounded a pair of braces { }.

2) Identifiers : It is the name that we give to different elements of a program, such as variables, functions, etc.

3)Data Types : Programming languages support different kind of data elements to hold different kind of values in a program. The basic data types that can be found in almost any modern programming language are:

1) integer, used with the keyword int. It stores basically whole numbers, positive and negative. This might sound a bit ambiguous, but it is true. An integer in programming languages can carry +ve and  -ve values but they can only be whole numbers. e.g.
                 int x = 5;
                 y = x / 4;
then the value of y will be 1. Because integers, in Programming languages exclude any value after the decimal point and that is why 5 / 4 equates to 1, not 1.25.
The range of values that an integer variables can carry vary from language to language, but as a general measure, it is taken to be from -32,768 to 32,767.

The syntax or the rule for declaring an integer variable is:
          int <variable_name>;
Note: In modern programming languages, every line of code needs to be terminated by termination punctuation, or a semicolon (;). 

2)  character, used with the keyword char. It stores a single character such as 'a', '8' or '&'. In C, the char doesn't actually store the character assigned to it, but the number or code, it has in the codification paradigm that it's compiler follows. In it's early editions, C stored a character's corresponding code in ASCII (American standard code for information interchange). Whereas, in C++ or java, a character variable actually stores the character. We'll discuss this in much detail in subsequent articles. 

The syntax for declaring a character variable is :   
             char <variable_name>;

3) floating point value or real number, used with the keyword float, is used to store the real numbers, or those which have fractional values. The range of values it can support also varies from from language to language, But as a general measure it does  from 3.4E-38 to 3.4E+38.  Well this values are in hexadecimal, but you can know that it can support values even after 10 places right to the decimal. Again, returning to the syntax:
            float <variable_name>;
Again, some distinctions exist as to how values can be assigned to floating point variables. In C or C++ they can be assigned values just like any other normal variable, the difference being, they can hold fractional values, like float x = 3.14. But in java, the assignment needs to end with an 'f' at the tail of the fractional value, otherwise, it is taken to be a long variable. In java, the same assignment needs to be somewhat like this, 
                                                   float x = 3.14f;

These are all the basic data types that can be found in almost any programming language. Some extensions to these, like a long integer that expands the range of int, or a double that expands the range of float, or String, that increases the capacity of a char, in the sense that a string can hold multiple characters, e.g. String x can be equal to a name, say"Sarah".
                                                    i.e. String x = "Sarah";
But these extensions are language specific, and that is why we'll discuss them later, when we dig deep into specific languages.

4) Operators : Operators are crucial parts of any programming language. they enable us to perform various operations on different data items. e.g. the plus '+' operator, lets us add two integral or floating point values, like 
                                          y = z + x;
And here the assignment operator '=', helps us store the resultant vale of that operation in the variable y.

The basic types of operators, that are generic to all programming languages are Binary Operators i.e which perform an operation on two variables, or operands. These binary operators can be further classified into several sub-classes, as :

i) Arithmetic Operators: the operators that perform arithmetic operations are called arithmetic operators. These include =, *, /, +, -, %. We'll dig into them, using examples. Let us take two integer variables a  and b.

When we write, a = 9 and b = 3, it's obvious enough, they carry the values, 9 and 3 respectively.
Now if we perform a * b, the result will be 27.   a * b = 27
Similarly,   a / b = 3, a + b = 12, a - b = 6. These should be obvious enough to you.

Now, we'll move to the new Modulus operator %. It looks like it might calculate the percentage of some value, over the other, but what it actually does, is stores the value of the remainder from the division of two numbers. Let us take an example to understand it better.

Let a = 5 and b = 2. Now if we are to divide these two numbers, the long division process will yield this result. 

The remainder is left 1, that is if we are not cross over to decimals. And that is the point of the modulus operator, while using that, the division must remain in whole numbers. So if we perform 
     c = a % b, c will be equal to 1, that is, the remainder from the division of the two operands.
Now, you might think that this is pretty much as silly operator, and what purpose will it serve. But as we move on, you'll see that it has a lot importance in a programming environment.

ii) Relational Operators :
As the name suggests, a relational operator or a comparison operator, establishes the relation between two variables or operands. Like, 9 is greater than 6 or 5 is less than or equal to 7, and in doing so compares them. Basically, there are four relational operators, which are:

1) <  (Less than)
2) > (Greater than)
3) >= (Less than equal to)
4) <= (Greater than equal to)

Though I think that their use is pretty obvious, but still, some example would be dandy, wouldn't they?

Suppose a and b are two variables and carry the values 9 and 12 respectively, so:
1) So, a is less than (<) b, right, so its True. And true or false are the results we will take on relational operators.
2) a b, is it? False
3) a >= b ? True. Why? Because 9 is either less than 12 or equal to it. Satisfies one of the two conditions, that's why the result is True.
4) a <= b? False, why? Because it satisfies neither of the conditions. 9 is neither greater than 12 nor equal to it, so the result comes out to be true

iii) Equality Operators :
There are two equality operators, that can be used to check whether two variables oroperands are equal to each other. The Equality operators are:

1) == (Equal to)
2) != (Not Equal to)

Again, while discussing Equality operators, we get a True or False result.
So, if we have, again two variables a and b, carrying the values 5 and 5 respectively, then

1) a == b is True
2) a != b is False

iv) Logical Operators:
The Logical operators,w ell, they let us perform logical operations, which are to compute, say whether something is true or something else is true, or both are true or neither. It's all sounding rather foggy right now, isn't it? Let us look at an example to understand it better.

Say we have three variables ab and c. Suppose we are to find if a is greater than both band c, what will we do? There is a method in which we check if a is greater than b and if a is greater than c and if both the comparisons are true, then we say that a is greater than both b and c.

We can do the same logical comparison in programming languages using the And Operator (&&).
The coding equivalent of our thought experiment is

if (a > b && a > c) == True.

Also, we have another operator to check whether a is greater than either or c, which is the Or Operator(||) i.e the button right above Enter on your keyboard. It works this way:

if (a > b || a > c) == True.
The only disadvantage of this operator is that if the first condition turns out to be True, then then it skips the second condition. And it could never tell if the second condition is true as well. So, using the Or operator, you can never know if a > b or a > c or a > b,c.

Barring the Or and And operators, we have another Logical operator called Not (!). What it does is, checks whether a variable is not equal to the test condition and return True orFalse. Let us take an example,: Say we are to allow access to a particular website, except for an IP address 102.345.1.1. What will we do. We can do something like this

if( ! 102.345.1.1) == True
      pass.

The above statement is what we call a pseudo code, where we mark down, what is need to be done, in order to perform the desired operation. Blocking uses from a real website, is somewhat different and more complicated than this, but this is just to give you a general Idea.

These are the basic kinds of operators that can be found in almost any Modern Programming Language. There are more categories that can be found in particular languages, but this pretty much gives the gist. We'll discuss the language specific operators in different languages, when we look at them individually.

That's all for this article. Everything taught today will be used to code in the next edition of Baby Steps into programming.

In the next edition we'll talk about escape sequences, endline streams and tools to format your output and much more.

No comments:

Post a Comment