3.4 ELEMENTS OF PROGRAMMING LANGUAGE

Learning a programming language requires understanding of concepts such as representation of different types of data in the computer, the various methods of expressing mathematical and logical relationships among data elements and the methods for controlling the sequence in which operations can be executed for inputting the data, processing it and generating the output data. 

3.4.1 Variables, Constants, Data type, Array and Expression 

These are the smallest components of a programming language. 

For writing a program, one must know how to use the internal memory of a computer. A Computer memory is divided into several locations where each location has its own address. Memory organization can be represented diagrammatically as below:

Each location or cell can hold some information. In order to store, retrieve or manipulate data from a specific memory location, we must have some identifier for a particular location. . 

Variable: As referencing memory by its physical address is very tedious, variable names are used. A variable is a symbolic name given to a memory location. Once a variable is assigned to a memory location, the programmer can refer to that location by variable name instead of its address. Variable is the connection between a name and a value. 

It is composed of a name, attribute, reference and a value. Attribute means the type of value a variable can hold.

For example the following programming code in C declares variables a & b. 

int a,b; 

char c; 

In the above declaration, a & b are the variable name, which refer to a memory location where integer values can be stored. Instead of address of the memory location, variable names a and b will be used to refer to a memory location in order to store or retriever update its value. 

Similarly, c is a variable name given to a memory location where a character value can be stored. Further c will be used to refer to the memory location in order to store or retrieve or update its value. 

Constant : In contrast to a variable, which is used as identifier for a value and which can change, constants are identifiers that are used for values, which cannot be changed. In other words constants are symbols used to refer to quantities which do not change throughout the life of a program. No assignment can be done to a constant. 

A numeric constant stands for a number. This number can be an integer, a decimal fraction, or a number in scientific (exponential) notation. Some of the operations which can be performed with numeric constants are addition, subtraction, multiplication, division and comparison. 

A string constant consists of a sequence of characters enclosed in double/single quote marks. Chopping off some of the characters from the beginning or end, adding another string at the end (concatenation), copying are some of the operations that performed on the string constants. All these operations can be done on variables also. 

For example in the C programming language 

-integer constant is declared as: 

const int a=2; /* the value of a cannot be changed throughout the program*/ 

-string constant is declared as: 

char const *str; /* str ,string can not be altered by string library functions*/ 

Data Type: Anything that is processed by a computer is called data. There are different types of data that can be given to the computer for processing. A data type is a classification identifying the typeof data. It determines the 

  • Possible values for that type,
  • Operations that can be performed on values of that type,
  • The way values of that type can be stored in memory,

In each programming language there are some primitive data types. For example in the C programming language they are: Please note that these sizes can be compiler or machine dependent in the case of this language. For other languages such as Java, the sizes are defined by the language itself. 

  • int, both signed and unsigned integers, 2 bytes in size.
  • float, floating point numbers, up to 4 bytes in size.
  • double, floating point number with double precision. These are organized in 8 bytes (64 bits)
  • char, character type size of 1 byte (8 bits) It is used to form the strings i.e sequence of characters.

Array : In programming, when large amount of related data needs to be processed and each data element is stored with different a variable name, it becomes very difficult to manage and manipulate. To deal with such situations, the concept of array is used. 

An array is a set of elements of same data type that can be individually referenced by an index. Usually these are placed in contiguous memory locations. Generally two types of array are used: 

  • One dimensional array 
  • Two dimensional array

One dimensional array: A one-dimensional array is a structured collection of elements that can be accessed individually by specifying the position of a component with index/ subscript value. The index would let us refer to the corresponding value. value at that . 

Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is: 

type name [elements]; 

where type is a valid data type (like int, float...), name is a valid identifier or variable name and the elements field (which is always enclosed in square brackets []), specifies how many of these elements the array will contain. Therefore, in order to declare an array named as marks, that will store marks for 5 students,

Two dimensional Arrays : A two-dimensional array is like a table, with a defined number of rows, where each row has a defined number of columns. In some instances we need to have such an array of arrays. It will have two dimensions and data is represented in the form of rows and columns. 

Type name [elements] [elements]; For example int a [3] [3]; /* lets one dimension depict location and other dimension represent sales in a day or a week or a month*/
Expression : An expression is a combination of variables, constants and operators written according to the syntax of the programming language. In the C programming language every expression evaluates to a value i.e., every expression results in some value of a certain type that can be assigned to a variable. Every computer language specifies how operators are evaluated in a given expression. 

An expression may contain 
i) arithmetic operator: 
ii) relational operator 
iii) logical operator 

Assignment : It is composed of variable name, an assignment operator of the language and a value or variable or some expression as per composition allowed based on rules defined in grammar. 

e.g temp=5; 
temp=temp+1; 
This means to add 1 to the current value of the variable temp and make that the new contents of the variable temp 
temp = a+b ; 

Arithmetic : These types of expressions consist of operators, operands or some expression. The following is the list of arithmetic operator. 
+(addition), 
-(subtraction),
*(Multiplication), 
/(Division), 
% (modulo), 
++(increment by 1), 
--(decrement by 1)

Here are some examples of arithmetic expressions. 
e.g. x=y+z; /* addition of y and z will be stored in x */ 
i++; /* i will be incremented by 1 i.e i=i+1 */ 
y=x%2; /* remainder after x divided by 2 will be stored in y */ 

Logical, relational and equality : these types of expression result in a Boolean representation i.e. each expression will result in either True or False. It is composed of operands and relational/logical/equality operator. 

The following is the list of operators in the C programming language
== (equal to) 
!= (Not equal to) 
< (less than) 
<= (less than equal to) 
> (greater than) 
>=(greater than equal to) 
&& (logical AND) 
|| (logical OR) 
! (logical NOT) 

Relational expressions result in one of the truth value either TRUE or FALSE. They are capable of comparing only two values separated by any valid relational operator. 

e.g. 
Let x=1, y=3 
x==1 /* evaluates to true as x has value 1 */ 
x!=y /* evaluates to false */ 
x<y /* evaluates to true */ 
(x<2) && (y> 5) /* evaluates to true */ 

Bit Wise: Bitwise operators modify variables considering the bit patterns that represent the values they store.

& AND (Binary operator) 
| inclusive OR (OR) 
^ exclusive OR (XOR) 
<< shift left. 
>> shift right. 
~ one's complement

e.g. let a=2 (0000 0010),b=5(0000 0101) 
c=a&b; (0000 0000) /* c=0*/ 


3.4.2 Conditional and Looping Statement 

Conditional statement: An If statement is composed of three parts. The first part should be keyword w.r.t language to convey to the computer that it is if statement. And a Boolean expression. The second and thirds part can be a statement or group of statements as defined in rules of grammar of language. 

Generally, an if statement is evaluated and executed in the following sequence: first it evaluates the boolean expression. If the expression is true, the statements in the second part are executed. Otherwise if it is false, the statements in the third part are executed. The third part is optional; if it is absent and the expression is false, then the program control simply moves on to the next statement in the sequence. 

For example, 

if (n %2 ==0) 
printf(“Number %d is even”,n); 
else 
printf(“Number %d is odd”,n) 

Looping Statement: The purpose of a loop structure is to repeat certain tasks until some condition is satisfied. Several variations of a loop structure are available in each programming language to handle different situations. 

A program loop consists of two segments, one is the body of the loop and the other is the control statement. The control statement tests certain conditions and then directs the repeated execution of the statements contained in the body of the loop. The test may be either to determine whether the loop has repeated the specified number of times or to determine whether the particular condition has been met. 

Thus a loop consists of : 
  • Initial condition 
  • Execution of set of statements inside the loop 
  • Test the condition 
  • Again execute the statements if the condition is met else go to the next statement in the sequence

There are three variants of looping statements in the C programming language are: 
  • For loop 
  • While loop 
  • Do while loop

In this brief introductory unit, we will not go into the details of the distinctions between these three types of loops. 
e.g 1 
for(i=0;i<20;i++) 
printf(“%d =”,i+1); 
scanf(“%d”,&s[i]); 
e.g 2 
i=0; 
while(i<20) 
sum=sum+s[i]; 
i++; /* increment counter */ 
}

Basic structure or keywords may vary in different languages. Also loop structure may be structured or not as it might not have control variables. Most of the languages do have control variables in their loop structure. 

3.4.3 Subroutine and Functions 

In a program, it is often necessary to repeat a statement or group of statements at several points to accomplish a particular task. Repeating the same statement in a program each time makes a program lengthy and reduces readability. These problems could be sorted out if the necessary statements could be written once and then referred to each time they are needed. This is the purpose of a subprogram. Basically there are two different types of subprograms, called functions and subroutines. 

Making subprograms allows tackling small pieces of a problem individually. Once each piece is working correctly then the pieces are integrated together to create the complete solution of the problem. To implement functions and subroutines, we require writing the main program that references all of the subprograms in the desired order and also writing the subprograms. This can be done in any order that is convenient. The following steps take place during the execution of subprograms:

1) Temporarily halt the execution of the calling program i.e main program. 
2) Execute subprogram. 
3) Resume execution of the calling program at the point immediately following the call of the subprogram. 

Subroutine : A subroutine is a type of subprogram, a piece of code within a larger program that performs a specific task and is relatively independent of the remaining code. 

It is also called a procedure, routine or a method. 

A subroutine has no value associated with its name. All outputs are defined in terms of arguments; there may be any number of outputs. 

In most cases, a subroutine needs some information about the circumstances in which it has been called. A procedure that performs repeated or shared tasks uses different information for each call. This information consists of variables, constants, and expressions that you pass to the procedure when you call it. 

A parameter represents a value that the procedure expects you to supply when you call it. You can create a procedure with no parameters, one parameter, or more than one. The part of the procedure definition that specifies the parameters is called the parameter list. 

An argument represents the value you supply to a procedure parameter when you call the procedure. The calling code supplies the arguments when it calls the procedure. The part of the procedure call that specifies the arguments is called the argument list. For example here is a subroutine to find the sum of three numbers 

SUBROUTINE sub1(A,B,C, SUM) 
        REAL A,B,C,SUM 
        SUM = A + B + C 
    RETURN 
END 

The subroutine sub1 in the main program will be invoked as follows CALL sub1(A,B,C, SUM) 

Function : The purpose of a function is to take in a number of values or arguments, do some calculations with those arguments and then return a single result. 

Each language has different rules to define a function. In the C programming language the basic block for function is given as:

return value function name (argument list) 
statement; 
Functions can be called from the main program or from anywhere else, even from within itself. Program control will transfer to function definition statement as soon they are called and then return back to next statement immediately after the calling point. 

e.g 
#include<stdio.h> 
void main() 
int x, y; 
printf(“Enter number”); 
scanf(“%d”,&y); 
x=funname(y); 
if(x==1) 
printf(“Number %d is even”,y); 
else 
printf(“Number %d is odd”,y); 

int funname(int a) 
if((a%2)==0) 
return 1; 
else 
return 0; 

Library function: These are the functions supplied with the programming language. The code or definition of library functions does not have to be written in a user program while writing it. Coding or definition of these function are defined in header or library files which are required to be included in program. e.g. 

#include<stdio.h> 
printf(),scanf() etc. are functions defined in stdio.h header file. 
Similarly every programming language has a set of library or header files.


Comments

Popular posts from this blog

3.5 SOCIAL NETWORKING

3.6 BLOG

3.4 COLLABORATIONS