Discarding a number for the given range of number using break in for loop of the C program

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: Discarding a number for the given range of number using break in C program

To show: How to use the 'break' statement in 'for' loop to discard a number for the given range of number

 

 

// Using 'break' statement in 'for' loop

#include <stdio.h>

 

int main(void)

{

int x;

 

for(x = 1; x <= 10; x++)

{

// break loop only if x == 5

if (x == 5)

break;

printf("%d ", x);

}

printf("\nBroke out of loop at x == %d\n", x);

getchar();

return 0;

}

 

Output example:

 

1 2 3 4

Broke out of loop at x == 5

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index