Subscribe by Email


Saturday, May 19, 2012

Explain simple loops in detail?


Loops are such an important programming constructs for various object oriented languages that they cannot be neglected. All of us are aware of the looping constructs that we have in programming languages like C and C++. We have three basic loops:
  1. For loop
  2. While loop
  3. Do while loop

Why Loops are Important?


- Loops find extensive use in programming and they are a means to tell the program to keep executing a set of statements until some break condition is encountered.
- Loops come to be a very handy tool when it comes to the repetition of the whole block of code. 
- It can be done to reduce the length of the code and the task of the programmer or developer of writing the same code again and again innumerable times. 
- In some cases it also happens that the number of times for which the loop is to be executed is obtained from the user, in such cases looping of the particular block of code becomes extremely important. 
-There are many software programs or applications that perform very complex tasks or calculations all by the virtue of the looping constructs. 

Before taking on the loops in to your software program you should be well versed with the true and false concept of the programming language that you are using. Let us discuss all the above mentioned three loops one by one:

                1.  For Loop: 
               Here’s the syntax of the for loop:

For (initialization of the variable; test condition; increment condition)
{
Statement 1;
Statement 2; (code to be executed)
.
.
.
Statement n;
}
In the expression for the initialization of the variable one can declare the variable for the loop and initialize it with the required value. Secondly the test condition is responsible for checking the whether the loop should be executed or not on the basis of the true or false value. The increment condition lastly helps the loop to increment the value of the initialized variable. All of these 3 expressions are separated from each other by semicolon. An empty condition is evaluated to false value.

2. While Loop: 
Here’s the syntax for the while loop:

While( test condition)
{
Statement 1;
.
.
Statement n;
}
According to some programmers the while loops are perhaps the easiest to operate. While loops are supposed to be entry controlled loop since the condition is checked up on the entry itself and based on its true value it is executed. While loops are somewhat like the for loops except that they contain the initialization expression and the update expression inside their body. But the drawback with this loop is that it is quite length. This loop won’t allow the execution of the statements even once if the test condition is evaluated to be false.
      
          3. Do while loop
          Here’s the syntax for the do – while loop:
Do
{
Statement 1;
Statement 2;
.
.
.
Statement n;
} while (test condition)
This loop holds good for the programs in which the execution of a particular block of statements is required at least once irrespective of whether the test condition is true or false. The test condition is encountered in the last and is evaluated. If it is found to be false, then the loop won’t be eligible for a second iteration. Because of this factor the do while loop is commonly known as the exit controlled lop. 


No comments:

Facebook activity