Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Friday, March 13, 2020

Loop in C

Loop in C: A loop is used for executing a block of statements repeatedly until a given condition returns false.

Loop in C | welcome2protec.com


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.

  1. for loop
  2. while loop
  3. do....while loop

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.



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:

  1. Loops have a scope, which means an entry/start point. And they also have an end point.
  2. Loops have a condition to tell them how many times the code within their scope has to be executed.
  3. 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).










Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us