Commonly Used Loops in Programming

Commonly used Loops:

In programming languages, loops are used to do anything repeatedly. Sometimes we need to perform the same task again and again then at that point loops save our time and efforts.

understanding loop
Understanding loop

The idea of the loop is that you go round and round until an end condition is met only then is the loop broken. As an example, suppose you want to add 3, ten times. Then you do something like this:

int and;

ans= 3+3+3+3+3+3+3+3+3;

This would be OK but if you had a thousand number of a figure greater than it, then what would you do? So to do this type of things we use loops in programming languages.

Type of Loops

There are four types of loops commonly used in Programming

  1. for loop
  2. while loop
  3. dowhile loop
  4. foreach loop

For Loop:

For loop is used for iteration. This loop start from a value does some action on every iteration and meets the set ending point. The syntax of For Loop is following:

In the syntax above for is the keyword. Then after for keyword there are three things which are the starting value, ending value and update Expression. After putting these values to for loop its look like that:

for (Starting value; Ending Value; Update Expression )

{ … \  body \  … }

 

If we look more precisely, starting value is that from where we start iteration, and the ending value is the terminating value. When the ending value is reached, the loop will exit and update expression.

for (iterated value ; condition ; iteration ){   }

Now take the above example in which we have to add 3 ten times. So what we have to do just put starting value ending value that is our condition and iteration value that after one iteration what would the possible change in the variable. Now let’s do that:

Now, what happen when the above code runs? First, it set the variable answer value 0. The in the second step for loop start with variable “i” value =1, and it increments by one until its value not equal to 10. And inside for loop that line execute answer = answer + 3 it will add 3 to the answer and save it to answer, and as I start from 1 to 10 then this loop execute 10 times and after executing 10 times the value of answer is 30 which is equal to sum of 3, ten time.

If we minimize the above code its look likes that:

  • Initial: answer = 0;
  • Step 1: answer = 0+3
  • Step 2: answer = 3+3
  • Step 3: answer = 6+3
  • Step 4: answer = 9+3
  • Step 5: answer = 12+3
  • Step 6: answer = 15+3
  • Step 7: answer = 18+3
  • Step 8: answer = 21+3
  • Step 9: answer = 24+3
  • Step 10: answer = 27+3

Now after step 10 answer value = 30.

Loops are also used to iterate arrays indexes like:

 

So instead of location every location one by one we use a For loop and iterate until end like:

Now the loop runs and visits every location on that array one by one.

While Loop:

The Working of the while loop is same as in for loop only the syntax is different.

while(condition){

}

In while loop the starting value is set before while keyword and after while keyword the condition is giving and in the body of while loop the iteration value is set. Let’s do the same above example with while loop.

The output of above code is same as in for loop.

Do While Loop:

Do while loop is also same as while and For loop but it must execute at least one time then check the condition Syntax:

do{

}while(condition);

Let’s Do the same above example with do while loop

Decrement a Loop:

We see above only about increment object and iterate loop, but if in a condition when we have to decrement the loop and go back, the syntax is same, only values of start and end are changed:

for(int x = 10 ; x>=0 ; x– ){ ….}

Now this code starts from 10 and after every iteration it decrement value of x and while x is not equal of less than 0 loops execute.

Foreach Loop:

For each loop is used to search arrays mostly. While searching or visiting every index/node of an array we use for loop and this we must know the total length of the array but what if we don’t know the length of the array then the foreach loop is used. It visits every index one by one and increment automatically.

Syntax:

foreach (data_type identifier in expression){

// Statements to be executed

}

Example:

Print Table of 5 Using Loop:

Leave a Reply

Your email address will not be published. Required fields are marked *