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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 34: Line 34:


== Important Aspects of Block-Structured Programming ==
== Important Aspects of Block-Structured Programming ==
=== Relation of Block-Structured Programming with Structured Programming/Procedural Programming ===
There is a subtle relation between block programming and structured programming. Structured programming encompasses majority of the fundamentals of block programming paradigm. Most of the block-structured languages fall under the structured programming paradigm. In essence, structured programming employs a hierarchical approach in which the main problem is broken down into different smaller modules. Thus, it breaks down a bigger tasks into smaller ones thus solving the smaller tasks to indirectly solve the actual problem. The important thing to note here is that such programs always have a single point of entry and often have single points of exit. The modules in this paradigm are independent of each other and thus; are blocks of code where the scope is limited to that particular module. Structured Programming normally imply simple hierarchical flow structures consisting of sequence (execution of statements in particular order), selection(some selection criteria) and iteration(repetition until the program reaches a certain state).


== Going the Object-Oriented Way ==
== Going the Object-Oriented Way ==

Revision as of 02:39, 31 August 2011

Wiki Chapter: CSC/ECE 517 Fall 2011/ch1 1e aa

Brief Background on the Programming Paradigms

Introduction

This Wiki chapter talks about the basic fundamentals of block structured programming and Object Oriented programming and explains the advantages of Object Oriented programming over block structured programming which made O-O languages more common and widely used in the Software Industry today. We also focus on the practicability of using block structured approach in O-O languages.

Block-Structured Languages

A Block is a part of code that is clustered together. It is thus; a group of program statements and variables referred to in those statements. Block of code always begins with variable declarations and is followed by procedural declarations, is always contained within delimiters; typically begin-end, opening and closing curly braces '{ }' and can be compiled and executed as a single execution unit. Block can be the body of a subroutine, a function or an entire program. The main block can contain subsections consisting of inner blocks. Those inner blocks can contain more inner blocks giving rise to a nested block structure. Typically, Nesting can be repeated to any depth required. One example of a language which allows such block structure is Pascal.

program a;  
   var id1, id2, id3 : integer;     { program a declarations }  
                                                  
   procedure b;                            
         var id1 : integer;         { procedure b declarations } 
                                                        
       procedure c;                         
              var id2 : integer;    { procedure c declarations}      
              begin    { Beginning of c's statement part }             
              id2 := id1;                     
              end;              
         begin     { Beginning of b's statement part }
         id1 := id3;                           
         id2 := id1;
         end; 
                                                                      
    begin     { Beginning of main program's statement part } 
    id1 := id2; 
    end.

In most primitive block structured languages, the scope of a variable can be limited to the block in which it is declared. This is called lexical scoping. Thus, referring to the nested structure of the blocks; all the variables declared in the outer block can be accessed within that block and all of its inner blocks but are not accessible outside that block. Additionally, values of the variables in the outer blocks are accessible in the inner blocks if and only if there is no other variable in the inner block with the same name. This duplicate declaration of variables is called Shadowing.

By having statements grouped together as a Block allows us to treat it as a single statement and thus allows the programmer to keep the 'lexical' scope of the functions, variables and procedures closely bound to that Block. Earliest block-structured languages were Algol 58 and Algol 60 with which the initial idea of block was born.

Important Aspects of Block-Structured Programming

Relation of Block-Structured Programming with Structured Programming/Procedural Programming

There is a subtle relation between block programming and structured programming. Structured programming encompasses majority of the fundamentals of block programming paradigm. Most of the block-structured languages fall under the structured programming paradigm. In essence, structured programming employs a hierarchical approach in which the main problem is broken down into different smaller modules. Thus, it breaks down a bigger tasks into smaller ones thus solving the smaller tasks to indirectly solve the actual problem. The important thing to note here is that such programs always have a single point of entry and often have single points of exit. The modules in this paradigm are independent of each other and thus; are blocks of code where the scope is limited to that particular module. Structured Programming normally imply simple hierarchical flow structures consisting of sequence (execution of statements in particular order), selection(some selection criteria) and iteration(repetition until the program reaches a certain state).

Going the Object-Oriented Way

What makes O-O better?

Block-structure in O-O programming

Conclusion

References