A loop is used for executing a block of statements repeatedly until a given condition returns false.
There are three types of loop in C programming.
In this guid, We will learn What does loop means in C language? and these three types of loops are covered in the separate tutorial.
- Structure of for loop:
- Description
- Advantage of loop
Quick links
What does loop means in C language?
A loop means that the code located within the scope of the loop will be executed several times.
To understand it we could dissect the loop in its different sections:
- Loops have a scope, which means an entry/start point. And they also have an end point.
- Loops have a condition to tell them how many times the code within their scope has to be executed.
- Loops have a task, which is the code inside them that has to be repeated as many times as per the condition tells.
Structure of for loop
In C, the beginning and end of the loops are determined by the brackets { } and everything contained inside the brackets will be executed several times. But loops also have a condition and that usually is declared before entering the loop.
For example: a common loop found/used in C is the “for loop" which is similar to:
for (i = 0; i < 10; i = i + 1)
{
printf("Hello");
}
In above example, you see the loop declaration which indicates that we are about to enter a loop what we are saying here in this line is that we are using a variable “i” whose value starts from 0 and that we are going to execute the code for as long as the value of “i” is less than 10 and that at each execution of the code inside the loop the value of “i” will be incremented by 1.
Then we have the opening bracket “{“ that tells us that the code to be executed will start from there.
And now the code to be executed in this loop is to print the word “Hello”.
Finally, we have the closing bracket “}” to signal that when code to be executed in loop has finished so the loop can now go back to loop declaration.
Description:
What happens in this little example, is that we have declared the initial value of our variable to be 0, then it gets evaluated against the condition to check it complies with “i < 10” and in case it fulfills the condition the code gets executed. As a result it prints “Hello”. Then it goes back to the beginning in order to perform the increment “i = i + 1”, it evaluates that the new value of “i” still complies with the condition “i < 10” and if it does it goes to the code and executes it again, which means it prints “Hello” once again. Now the process becomes repetitive and it goes again and again until the condition is not longer met. In that case, the loop breaks execution and the program can continue with whatever lines of code are written after the closing bracket.
There are other types of loops, but in essence they all have the same sections and work in a similar way and code can get more complicated than in the above example, for instance you can use different kind of conditions to make them work not only numeric values and the increment could actually be a decrement and it does not necessarily have to be one by one.
So, long story short the loops make the code within them to get executed several times.
Advantage of loop:
- It provides code reusability.
- By using loops, we don't need to write the same code again and again.
- By using loops, you can traverse over the elements of data structures (like array or linked list).
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.