What are loops?

Asked 27-Nov-2017
Viewed 558 times

1 Answer


1

Talking about the loops…

What are loops?

First, let us understand what a loop does… It is that portion of the code which takes up the responsibility for a performing a certain take for a certain number of times.

Well, believing in the practical experience and knowledge. Let us understand the above-mentioned statement via a small example.

Holding on to one particular language. Let us start with C Programming Language as the base to learn more about loops…

Considering a scenario where you need to introduce yourself like for 5 times. So, how would you do a process that job… Here we go with a simple code using a C Program:

#include <stdio.h>


main() {

   printf( "Hello, I am XYZ\n");
   printf( "Hello, I am XYZ \n");
   printf( "Hello, I am XYZ \n");
   printf( "Hello, I am XYZ \n");
   printf( "Hello, I am XYZ \n");

}
After the program is processed, the result is-
Hello, I am XYZ

Hello, I am XYZ
Hello, I am XYZ
Hello, I am XYZ
Hello, I am XYZ
This was quite an achievable task for you but, let us consider a scenario where you need to print the same code for around 500 times. Well, considering the human tendency that is possible but a time-consuming task which may just be a waste… for a small output.
Here comes the introduction to a concept called “Loops”, which helps in the execution of a task for more than one times according to the user’s appetite.
Nowadays many programming languages are geared up with the concept of loops, which as mentioned in the above statement help in the execution of the task iteratively.
Let us see how this concept does will be helpful for any programmer with their codes…

Hence, these programming languages come up with variants of such concepts about loops, they are as follows:

  • For Loop
  • While Loop
  • Do-While Loop


The further explanation of the terms, in brief, is as follows:

For Loop:

Writing down solution for the above program using For Loop concept and let us see, how easily it could be done using such an innovative concept…
#include <stdio.h>
main() {
   int i;
   for ( i=0; i < 5; i++ )
   {
      printf( "Hello, I am XYZ\n");
   }
}
When the above program is executed, it produces the following result -
Hello, I am XYZ
Hello, I am XYZ
Hello, I am XYZ
Hello, I am XYZ
Hello, I am XYZ
The above mentioned code used For Loop, which is used to process a set of statements present within a scope {} wherein it checks for the condition like in the above example, if ‘i’ is less than 5 further it functions considering the output to be True or False, if True it enters the scope and execute the statements into it. Later it increments the value of ‘i’ by one -

Statement 1 is printf() function, which prints Hello, I am XYZ!

After processing all the statements under the scope of the loop, the system turns around to the for(i=0;i<5;i++) and the condition (i++) is executed followed by the condition (i<5) is been checked and it keeps on iterating until the condition does not hold true.
Thus, a loop statement permits us to process a set of state or unit statement variable numbers of time.

Let us study the flowchart of the mentioned code:
What are loops?

The while Loop:

The while loop is present in the C Language used in the following syntax:
while ( condition )
{
   /*....while loop body ....*/
}
Let us study its Flow Diagram:

What are loops?

Points need to be pondered using While Loop:

  • It initiates with a keyword while following a condition scoped within ( )
  • Further, the body of loop is been executed within curly braces {}
  • It can possess one or more than one statement functioning in an iterative manner
  • It is optional to use the curly braces for a unit statement
  • While Loop is active only till the time its condition is True after that it breaks off out of the loop and execute the statements outside the scope of the loop body
  • It functions as a relational statement, evaluated in 2- dimensions either True or False which may also be interpreted in values if equivalent to 0, it is False and any value other than 0 is True

The do...while Loop:

As you have got familiar with the concept of the While Loop. Further, one more concept was introduced in the C Programming Language known as do…While which process the loop body without having a check on the while condition as shown by the syntax:
do
{
   /*....do...while loop body ....*/
}
while ( condition );
 Studying about the Flowchart of the following:
Writing down the same example mentioned above, then it would produce the same result as above:
#include <stdio.h>
main() {
   int i = 0;
   do {
      printf( "Hello, I am XYZ\n");
      i = i + 1;
   }
while ( i < 5 );
}
Output for the above the code is as follows…
Hello, I am XYZ
Hello, I am XYZ
Hello, I am XYZ
Hello, I am XYZ
Hello, I am XYZ

The break statement:

Break statement present inside the loop, it breaks off the continuity of the loop and follows the statement present outside the loop scope.
Having a deeper understanding of it using a syntax:
break;
Let us study its Flowchart:
What are loops?

Experimenting with the same example mentioned above printing the output thrice:
#include <stdio.h>
main()
{
   int i = 0;
   do
 {
       printf( "Hello, I am XYZ\n");
       i = i + 1;
       if( i == 3 )
 {
  break;
         }
    }
while ( i < 5 );
}

Results are here:
Hello, I am XYZ
Hello, I am XYZ
Hello, I am XYZ

The continue statement:

The continue statement is quite equivalent to break statement.
It asks for the next iteration omitting any code in the middle instead of the termination of the loop.
Well understanding it through a syntax:
continue;

Supporting it further with the flowchart:
What are loops?

Let me explain you this with the support of a code:
#include <stdio.h>
main()
{
   int i = 0;
   do
 {
       if( i == 3 ) {
          i = i + 1;
         continue;
      }
      printf( "Hello, I am XYZ\n");
      i = i + 1;
   }
while ( i < 5 );
}
The output of the code is:
Hello, I am XYZ
Hello, I am XYZ
Hello, I am XYZ
Hello, I am XYZ
Well, these were common in all the programming languages till date...
Helping out reaching the programmers for more of an efficient approach to coding...

Cheers!

 


Comment
nice it's very helpful. - Anonymous User13-Dec-2017