NEWS & UPDATES >> BCA BCSP064 Synopsis & Project Work Started for DEC 2017, IGNOU MCA MCSP060 Synopsis Work Started for DEC 2017, CONTACT 4 IGNOU Mini Project

What is Conditional and Looping Statement, WHAT IS EDITOR & ASSEMBLER, INTERPRETOR & COMPILER


What is 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.

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.


WHAT IS  EDITOR, ASSEMBLER, INTERPRETOR & COMPILER


To write a program in any of the programming languages requires an editor.

This is a program that is used to create text files. While saving the program, filename and extension as per programming language is required to be given e.g in C programming language f1.c, in C++ f1.cpp or f1.C, in Java f1.java etc. The extension may also depend on the conventions of the operating system used, for instance, in unix the extension for a C++ program is .C while for Windows it would be .cpp.

There are different types of editors. Some of the programming languages have

some specific built in editors.
   

A Programming Language is different from machine language, which is understood by a computer in the sense that it can be directly executed. Hence a program in any higher level programming language like C requires a translation process that can translate the source program into machine code so that it can be executed by the computer.

As you may already know from a previous unit, programming languages can be low level languages or high level languages.

Assembly language is a low level programming language similar to machine language, but far easier to write and understand because machine language binary instructions and operands are replaced by mnemonics that are comprehensible to humans. Just As a program written in programming language requires a translator to translate the source program in machine code, a program written in assembly language uses the utility named as assembler for translation purpose. Assembly language is the most basic programming language available for any processor. With assembly language, a programmer works only with operations implemented directly on the physical CPU. Assembly language lacks high-level conveniences such as variables and functions, and it is not portable between various families of processors.

High level programming languages provide:

Good readability

Portability

Easy debugging


Easy software development

Hence Programming languages translators are broadly divided into two 

categories:

Compilers

Interpreters


Compiled Language : An additional program called a compiler translates a program written in a programming language; into a new file that does not require any other program to execute itself, such a file is called an executable.

e.g. C, C++, Pascal are languages that are typically compiled

Compilers produce better optimized code that generally runs faster and compiled code is self-sufficient and can be run on their intended platforms without the compiler present.

Interpreter : An interpreter is a program that translates each statement in the programming language into machine code and runs it. Such an arrangement means that to run the program one must always have the interpreter available.

e.g Basic , Prolog, Perl are languages that are typically interpreted.

Programs in any language can be interpreted or compiled. So there are basic compilers available as well. Compiled code runs faster and does not need the compiler at run time, whereas interpreted code is slower and needs the interpreter every time the program has to be run.

Check Your Progress 2

1)      What is the need of programming language?


To solve any problem computer has to be given instructions. Instructions cannot be given in any natural language, which we use (like English, Hindi etc). So it is required to have a programming language to write the program to solve the given problem with the help of a computer.



2) What is the purpose of looping statement s in       a programming language?


If in a program, a set of statements has to be executed more than once at a particular place, then looping statements are used.



3)What are basic operators in any of the                   programming language?

   The operators in any programming language         are broadly classified into the following types:
  a)Arithmetic operators: Operators, which are      used to perform arithmetic operations such as     addition, subtraction, multiplication, division etc.
  b)Logical Operators: Operators under this           category are AND, OR, NOT.
 c)Relational Operators : >,<,=, not = ,<=, >=         these are the relational operators.


4)     What is the purpose of using an array in a             programming language?

In programming, when large amount of related data need to be processed and each data element is stored with different variable name, it becomes very difficult to manage and manipulate. To deal with such situations, the concept of array is used. Array provides a simple & efficient method to refer, retrieve & manipulate a collection of similar data by a single name.






No comments:

Post a Comment