CSC/ECE 517 Fall 2011/ch1 1e an: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 29: Line 29:
=== Implementation of Block Structure in programming languages ===
=== Implementation of Block Structure in programming languages ===


[['''AlGOL''']]
'''ALGOL'''


   begin  
   begin  
Line 47: Line 47:




[['''PASCAL''']]
'''PASCAL'''


   if wages > tax_threshold then
   if wages > tax_threshold then

Revision as of 18:33, 1 September 2011

Block Structured Language vs OO Languages

Introduction

A programming paradigm is a fundamental style of computer programming. It provides for the programmer the means and structure for the execution of a program and thus serves as a pattern or model. Many programming paradigms have their own concepts and abstraction thus offering different techniques for the programming world. Also they are as well-known for what they do not do as for what they do.As a result, many programming paradigm are regarded as an improvement over earlier styles. This wiki chapter describes about Block Structured Languages and Object Oriented programming paradigm while emphasizing on the transition/improvement of Block Structured Language to Object Oriented Language due to its advantages and usage in today's programming world.

Overview of Block Structured Languages

Every programming language has a unique syntax and a set of instructions which are usually in a form of statements that should conform to a set of rules in defining and constructing it. The construction of these instruction depends on the language you use. Most of them follow a procedural approach where implementation is done step by step while other require objects to come up with a solution. Among these, the most common and used technique is what we called block structure programming where declarations and statements are grouped together in a form of a block.

Block structured programming language permits the creation of blocks, with blocks nested within other blocks. The function of blocks in programming is to enable groups of statements to be treated as if they were one statement, and to narrow the lexical scope of variables, procedures and functions declared in a block so that they do not conflict with variables having the same name used elsewhere in a program for different purposes.

In a block-structured programming language, the names of variables and other objects such as procedures which are declared in outer blocks are visible inside other inner blocks, unless they are shadowed by an object of the same name. They do not require prior detailing to be given to the definitions made.The structure is formed first without knowledge of inner groupings. This supports top-down development of code in an organized manner.Blocks allow the programmer to treat a group of statements as a unit, and the default values which had to appear in initialization in this style of programming can, with a block structure, be placed closer to the decision. This technique does not only simplifies the code but also contributes to its readability and maintainability.

Example of a block structured code in JavaScript:-

calculateVal:function(val){
   var increment = $(val).increment;
   var incValue = ;
   for(i=0;i<increment.length;i++){
      if(increments[i].selected==true){
            incValue=increment[i].value;
      }
   }
  return incValue;
}


The for looping statement uses the block structure in this code snippet. The value of incValue changes as it passes to the loop.

Implementation of Block Structure in programming languages

ALGOL

  begin 
  integer X;
  Read Int(X);
 
      begin 
      real square;
  
      square= X*X;
      Print Real(square);
      end
  end


In Algol, the block structure is formed by grouping the code by a set of delimiters such as begin.... end. The block of statements is used also to represent the scope of the variable. Here, variable square has the scope within the nested block while scope of variable X is within the whole block.


PASCAL

  if wages > tax_threshold then
       begin 
       paytax := true ;
       tax := (wages - tax_threshold) * tax_rate;
       if wages > supertax_threshold then 
             begin 
             pay_supertax := true;
             supertax := (wages - supertax_threshold) * supertax_rate;
             end
       else
             begin 
             pay_supertax := false;
             supertax := 0;
             end
       end
  else
       begin
       paytax :=0;
       superpaytax :=0;
       tax :=0; supertax :=0;
       end
  finaltax := wages - tax - supertax;


The block structure in this example is implemented in a conditional statement. Notice that is easier to determine the result of the condition by just checking on the inner blocks. Changes can also be integrated with ease in this case without affecting the other blocks. This blocks can also be implemented on looping statements.


Overview of Object Oriented Language