Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home C-tutorials C Break for loop

Related Tutorials


 
 

Share on Google+Share on Google+

C Break for loop

Advertisement
The break statement terminates the execution of the enclosing loop or conditional statement.

C Break for loop

     

In this section, you will learn how to use break statement in a for loop.

The break statement terminates the execution of the enclosing loop or conditional statement. In a for loop statement, the break statement can stop the counting when a given condition becomes true. You can see in the given example that the program is supposed to count the numbers from 1 to 20 using for loop. As per the condition defined the break statement stop the execution of the loop as soon as it detects the number 5 .

Here is the code:

BREAKFor.C

 

 


#include <stdio.h>
#include <conio.h>
void main() {
  clrscr();
  int n;
  for (n=1; n<20; n++) {
  printf("%d\n", n);
  getch();
  if (n==5)
  break;
  }
}

Output will be displayed as:

BREAKST.EXE

As the number 5 comes, the execution of the loop gets terminated.

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 5, 2009

Related Tutorials

Discuss: C Break for loop   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Darshan Manjunath
July 8, 2013
how to intrupt for loop

hi, I want to intrupt a for loop which count till 30 or 60. I dont want to break the loop. If any intrupt(use of any key) occurs it should come out of the loop.. can anyone suggest me.
DMCA.com