Looping Statements In Programming

how to start programming

Loops in programming

Looping statements are statements that execute one or more statements repeatedly until the condition holds.

Types of Looping statements

  1. Entry Controlled
  2. Exit controlled
types of looping statements

Entry Controlled Loops

Entry controlled loops means before entering into the body it first checks the condition, if the condition is true then only it executes the remaining code else it stops its execution. And they are classified into two types as follows

  1. For-loop
  2. While-loop

For-Loop Syntax

for(intialization; condition; increment/decrement)
    // block of code ;

Steps for executing the for-loop

  1. Initialization of variable
  2. checking the condition
  3. increment/decrement

The above three steps repeat until the condition holds true, if the condition is false control comes out of the loop.

Now let’s see a simple program using for-loop

print 1 to 100 numbers using for-loop

So, in the above program first, the value of the variable i is set to 1. After it checks the condition and it is less than equal to 100. So the value of i is printed on the output screen and it increments its value of i by 1. The above process continues until i is equal to 100.

While-Loop Syntax

    // statements or block of code

The while loop also follows the same steps, The while loop must be used in situations where we do not know the exact number of iterations of the loop beforehand.

Now let’s see the sample program using while-loop

program to display no of times the given number is divisible by 2.

So, in the above program, 35 is assigned to a variable number, and the count is initialized to zero. our aim is to find the number of times 35 is divisible by 2. So we repeat the while loop until the number is greater than 1 and we will increment the count respectively we will divide the number by 2. Finally, we are displaying the count.

One important thing in the above problem is if the action inside the loop does not modify the variables being tested in the condition of the loop, the loop will run infinite times. For the above example if we don’t modify the variable number the loop will run infinitely.

Exit Controlled Loops

Exit Controlled loops mean at first the test condition is checked after the execution of the body of the loop. Therefore, an exit controlled loop executes at least once even condition fails. Do-While comes under this category.

Do-While Loop Syntax

   // block of code or statements

The advantage of using do-while loop is it executes the statements once even if the condition fails.

Now let’s see a sample program using do-while loop

print even numbers from 1 to 100.

So, in the above program variable number is initialized to 1, and inside the do-while loop, we are checking if a number is even or not. If it is even print it otherwise increment the number. This process continues until the number less than equal to 100.once it becomes greater than 100 it will stop iteration and comes out of the loop.

Advantages of Loops

  1. It provides code reusability.
  2. execution takes place faster in loops compared to Recursion.
  3. Loops are used in traversing techniques.
  4. Takes less space.