Java Reference
In-Depth Information
int num = 15; // num holds 15 at this point
int counter = 0; // counter holds 0 at this point
while(counter < 10) {
num = num + 1; // Modifying data in num
counter = counter + 1; // Modifying data in counter
}
// num holds 25 at this point
The first two lines are variable declarations that represent the data part of the program. The while loop represents
the algorithm part of the program that operates on the data. The code inside the while loop is executed 10 times. The
loop increments the data stored in the num variable by 1 in its each iteration. When the loop ends, it has incremented
the value of num by 10. Note that data in imperative programming is transient and the algorithm is permanent.
FORTRAN, COBOL, and C are a few examples of programming languages that support the imperative paradigm.
Procedural Paradigm
The procedural paradigm is similar to the imperative paradigm with one difference: it combines multiple commands
in a unit called a procedure. A procedure is executed as a unit. Executing the commands contained in a procedure is
known as calling or invoking the procedure. A program in a procedural language consists of data and a sequence of
procedure calls that manipulate the data. The following piece of code is typical code for a procedure named addTen :
void addTen(int num) {
int counter = 0;
while(counter < 10) {
num = num + 1; // Modifying data in num
counter = counter + 1; // Modifying data in counter
}
// num has been incremented by 10
}
The addTen procedure uses a placeholder (also known as parameter) num , which is supplied at the time of
its execution. The code ignores the actual value of num . It simply adds 10 to the current value of num . Let's use the
following piece of code to add 10 to 15. Note that the code for addTen procedure and the following code are not written
using any specific programming language. They are provided here only for the purpose of illustration.
int x = 15; // x holds 15 at this point
addTen(x); // Call addTen procedure that will increment x by 10
// x holds 25 at this point
You may observe that the code in imperative paradigm and procedural paradigm are similar in structure. Using
procedures results in modular code and increases reusability of algorithms. Some people ignore this difference and
treat the two paradigms, imperative and procedural, as the same. Note that even if they are different, a procedural
paradigm always involves the imperative paradigm. In the procedural paradigm, the unit of programming is not a
sequence of commands. Rather, you abstract a sequence of commands into a procedure and your program consists
of a sequence of procedures instead. A procedure has side effects. It modifies the data part of the program as it
executes its logic. C, C++, Java, and COBOL are a few examples of programming languages that support the procedural
paradigm.
 
Search WWH ::




Custom Search