0% found this document useful (0 votes)
30 views

Iteration Constructs

The document discusses different types of iteration constructs or loops in C programming language. It describes the three main loop structures - for, while, and do-while loops. It provides details on the general format of for loops including initialize counter, conditional test, and re-evaluation parameter. It provides examples of using for loops to print even/odd numbers within a range. It also discusses multiple initializations/increments in for loops, nested for loops, and uses of for loops without definitions.

Uploaded by

hrishipisal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Iteration Constructs

The document discusses different types of iteration constructs or loops in C programming language. It describes the three main loop structures - for, while, and do-while loops. It provides details on the general format of for loops including initialize counter, conditional test, and re-evaluation parameter. It provides examples of using for loops to print even/odd numbers within a range. It also discusses multiple initializations/increments in for loops, nested for loops, and uses of for loops without definitions.

Uploaded by

hrishipisal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

IT SERVICES PRIVATE LIMITED

Iteration Constructs
Iteration constructs, also known as loops, allow a set of instructions to be performed
until a certain condition is reached. This condition may be predefined or open ended. The
loop structures available in C are :

The for loop

The while loop

The do...while loop

The for Loop


The general format of the for loop is
for(initialize_counter; conditional_test; re-evaluation

parameter)

statement;
The initialize counter is an assignment statement that sets the loop control variable
, before the loop is entered. This statement is executed only once. The conditional test is
a relational expression which determines when the loop will exit. The re-evaluation
parameter defines how the loop control variable changes (mostly, an increment or
decrement in the variable set at the start) each time the loop is repeated. These three
sections of the for loop must be separated by semicolons. The statement which forms the
body of the loop can be either a single statement or a compound statement.
The for loop continues to execute as long as the conditional test evaluates to true.
When the condition becomes false, the program resumes on the statement following the for
loop.
for(initialize counter; conditional test; re-evaluation parameter)

1. Initialize counter:Initialize counter is always assign statement which sets loop control variable
before loop is entered.
2. Conditional test:Conditional test is always relational expression which determine when the loop
will exit.
3. Re-evaluation parameter:Re-evaluation parameter determines how loop control variable changes after
every iteration.
Impetus IT Services Pvt.Ltd.
B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


The following program prints even numbers from 1 to 25.
#include <stdio.h>
main ()
{
int even;
const int MAX_TIMES = 25;
printf(The even numbers from 1to 25 are:\n);
for
( even = 2;
even <= MAX_TIMES;
even += 2 )
{
printf(%d\n, even);
}
}
The above for loop initializes the integer variable i to 2 (to get an even number) and
increments it by 2 every time the loop is executed.
In for loops, the conditional test is always performed at the top of the loop. This
means that the code inside the loop is not executed if the condition is false in the beginning
itself.
Multiple Initializations /Increments in for
The scope of the for loop can be extended by including more than one initialization
or increment expressions in the loop specification. These expressions are separated by the
comma operator and evaluated from left to right. The order of the evaluation is important if
the value of the second expression depends on the newly calculated value of the first one.
Consider the following loop
for (i = 0, j = 10; i
{

<

50; i++,

k- -)

This example has a normal loop variable i, but it also initialises another variable j,
and decrements a third k. The variables j and k do not need to have anything with each
other, or with i.
Nested for L oops
A for is said to be nested when it occurs within another for. The code will be
something like
for ( i = 1;
{
for ( j
{
}

i
=

< maxl; i++ )


0;

<= max2; j++ )

}
Impetus IT Services Pvt.Ltd.
B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


Consider the following example,
#include <stdio.h>
main ()
{
int i,
j,
k;
i = 0;
printf(Enter no.
of rows
:);
scanf(%d,
&i);
for
(
j = 0;
j < i;
j++)
{
printf(\n);
for
(k = 0;
k <= j;
k++)
{
printf (*);
}
}
}
This program takes displays * on each line and for each line increments the number
of * to be printed by 1. It takes as input the number of rows for which the * has to be
displayed
For an input of 5, the output will be
*
**
***
****
*****
More on for

The for loop can be used without one or all of its definitions.

For example,
for
{

0;

!=

255;

printf (Enter integer.


scanf(%d,
&i);
.
.

);

}
Impetus IT Services Pvt.Ltd.
B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


The above will accept a value for i until the input is 255. This loop does not have any
re-evaluation factor. The loop terminates when i becomes 255.
Similarly, consider
printf(Enter value
scanf(%d,
&i) ;
for
(
;
i
<
{

for

checking

100;

);

This loop does not have an initialization factor or a re-evaluation factor. for loop,
when used without any definitions gives an infinite loop.
f or ( ; ; )
printf (This loop will go on and on and on...\n);,.

However, a break statement used within this loop will cause an exit from it.
for (; ;)
{
printf(This will
i = getchar () ;
if
(i ==
X
break;
}

go
||

on

and

on (x

= =

x)

WILL

exit));

The above loop will run until the user types x or X at the keyboard.
for loop (or any other loop) can also be used without the body (statements). This
helps increase efficiency of some algorithms and to create time delay loops.
for(i

0;

<

xyz _value, i++);

is an example of a time delay loop.

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


Examples:1. Accept a number (num) from the user and display the number from 1 to num.
Program:-

int main(int argc, char** argv) {


unsigned int count;
int limit;
printf("Please enter a integer : ");
scanf("%i", &limit);
for (count = 1u; count <= limit; count++)
{
printf("Value = %u\n",count);
}
return (EXIT_SUCCESS);
}
2. Accept a number (num) from the user and display all odd numbers from 1 to num.
Program:int main() {
unsigned int odd;
int num;
printf("Please enter a +ve non zero integer : ");
scanf("%i", &num);
printf("Odd numbers from 1 to %d are as follows : \n", num);
for (odd = 1U; odd <= num; odd += 2)
{
printf("\t\t\t%u\n", odd);
}
return 0;
}

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


3. Accept a number (num) from the user and display all even number from 1 to num.
Program:int main(void) {
unsigned int even;
int limit;
printf("Please enter a integer : ");
scanf("%i", &limit);
printf("The first %d even numbers are:\n",limit);
for (even = 2U; even <= limit; even+=2 )
{
printf("\t\t%u\n", even );
}
return 0;
}

4. Accept a number (num) from the user and display its factorial.
Program:int main(void) {
unsigned int count;
int num;
long double fact = 1.0;
printf("Please enter a integer : ");
scanf("%i", &num);
for (count = 2u; count <= num; count++)
{
fact = fact * count;
}
printf("The factorial of %d is %.0Lf\n",num,fact);
return (EXIT_SUCCESS);
}

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


5. Accept a number (num) from the user and display first num odd numbers
Program:int main() {
unsigned int count;
int limit;
unsigned int odd;
printf("Please enter a integer : ");
scanf("%d", &limit);
printf("First %d Even numbers are as follows\n", limit);
for (odd = 1u, count = 1; count <= limit; count++)
{
printf("\t%u\n", odd);
odd += 2;
}
return 0;
}
6. Accept a number (num) from the user and display all numbers from num to 1 in
descending order.
Program:int main(void) {
int intVar;
int count;
printf("Please enter a +ve Non Zero Integer: ");
scanf("%d", &intVar);
printf("Integers in the range of %d..1 are\n", intVar);
//possible conditional expr : count >= 1, count != 0
for (count = intVar; count > 0; count--)
{
printf("\t%d\n", count);
}
return 0;
}
// alternative method
int main(void) {
int intVar;
printf("Please enter a integer : ");
scanf("%d", &intVar);
printf("Integers in the range of %d..1 are\n", intVar);
Impetus IT Services Pvt.Ltd.
B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


for ( ; intVar > 0; intVar--)
{
printf("\t%d\n", intVar);
}
return 0;
}
// alternative method
int main(void) {
int intVar;
printf("Please enter a number : ");
scanf("%d", &intVar);
printf("Integers in the range of %d..1 are\n", intVar);
for ( ; intVar > 0; )
{
printf("\t%d\n", intVar--);
}
return 0;
}
// alternative method
int main(void)
{
int intVar;
printf("Please enter a number : ");
scanf("%d", &intVar );
printf("Integers in the range of %d..1 are\n",intVar);
for(
{

; intVar

printf("\t%d\n", intVar-- );
}
return 0;
}
// alternative method
int main(void) {
int intVar;
printf("Please enter a number : ");
scanf("%d", &intVar);
printf("Integers in the range of %d..1 are\n", intVar);
for (;;) //infinite loop
{
printf("\t%d\n", intVar--);
if (intVar == 0)
Impetus IT Services Pvt.Ltd.
B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


{
break; //terminates loop
}
}
return 0;
}

The while Loop


The second kind of loop structure in C is the while loop. Its general form is while
(condition) statement;
where statement is either an empty statement, a single statement or a block of statements
that repeats. The condition may be any expression. The loop iterates while this condition is
true and the program control is passed to the line after the loop code, when the condition
becomes false (true is any non zero value).
The for loop can be used, provided the number of iterations are known before the
loop starts executing. When the number of iterations to be performed is not known
beforehand the while can be used.
#include <stdio.h>
main ( )
(
int i,
j;
char a =
;
i = j = 1;
print f (\nEnter number
of chars to enter
scanf(%d,
&i) ;
while
(j <= i)
{
a = getche () ;
j++;
}
printf(\n%d characters entered,
i) ;
return 0 ;
}

:);

The above program asks for number of characters to be entered, accepts the
characters and displays number of characters entered.
The while loop accepts characters until the variable j, which is initially set at 1, is
equal to i. i is accepted initially as the number of characters to be entered, j is incremented
within the while loop.

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


Like for loops, while loops check the condition at the top of the loop, which means
the loop code is not executed, if the condition is false at the start.
The conditional test in the loop may be as complex as required. The variables in the
conditional test may be reassigned values within the loop, but a point to remember is that
eventually the condition test must become false otherwise the loop will never terminate. The
following is an example of an infinite while loop.
#include

<stdio.h>

main ()
{
int i = 0;
while
(i < 100)
{
printf(This goes on
i
+= 10;
printf(\t%d,
i);
i+= 10;
printf(\t%d,
i);
printf(\nCtrl-C will
}
}

forever, HELP!!!\n);

help);

In the above, i is 0 whenever the condition is checked, and so the loop never
terminates.
If more than one condition is to be checked to terminate a while loop, the loop will
terminate if at least one of the conditions become false. The following example illustrates
this.
#include <stdio.h>
main ()
{
int
i,
j;
i = 0;
a = 10;
while
(i < 100
{
.
.
i++ ;
a- = 2 ;
}
.
.
}

&&

>

5)

This loop will perform 3 iterations, the first time a will 10, the next time it will be 8 and
the third time it will be 6. After this though i is still less than 100 (i is 3), a becomes 4, and
the condition a > 5 becomes false, so the loop terminates.
Impetus IT Services Pvt.Ltd.
B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


The do...while Loop
The do...while loop is sometimes referred to as the do loop in C. Unlike for and
while loops, this loop checks its condition at the end of the loop, that is after the loop has
been executed. This means that the do...while loop will execute at least once, even if the
condition is false initially. The general form of the do...while is
do
{
statements;
}while(condition);
#include <stdio.h>
main()
{
int
num,
count ;
num =
count = 0;
do
{
printf (Enter a integer :
);
scanf (%d, & num) ;
printf (Integer is
%d, num ) ;
count++;
}
while
(num != 0);
printf(\nThe total nos. entered were %d, --count);
}
/* count is decremented before printing because count for last integer (0) is
not to be considered */
The above will accept integers and display them until zero (0) is entered. It will then,
exit the do...while and print the number of integers entered.

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


Example : 1. Accept a number n from the user and check if it is prime number.
Program:#include<stdio.h>
int main()
{
int intVar;
int count = 2;
int temp;
int flag = 1;
printf("\nPlease enter a integer : ");
scanf("%d", &intVar );
temp = abs(intVar) / 2;
while( count <= temp )
{
if( ( intVar % count ) == 0 )
{
flag = 0;
break;
}
count++;
}
if( flag == 1 )
{
printf("\nThe number %d is Prime",intVar);
}
else
{
printf("\nThe number %d is not Prime", intVar);
}
return 0;
}

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


2. Accept alphabet from a user and check whether it is vowel or consonant. Continue
the process till alphabet Z is encountered.
int main(){
char ch;
int counter = 0; //initialization important
const int MAX_TIMES = 5;
do {
printf("Please enter a alphabet :");
scanf("%c",&ch);
while(getchar()!=10);//very important
if(isalpha(ch))
{
switch(toupper(ch))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("\'%c\' is a Vowel\n", ch);
break;
default:
printf("\'%c\' is a Consonant\n",ch);
counter++; //only when consonant is found
} //end case
}
else
{
printf("Entered character is not alphabet !\n");
}
}while(counter != MAX_TIMES );
return EXIT_SUCCESS;
}

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


3. Write a program to display the following.
*
* * *
* * * * *
* * * * * * *
Program:int main(int argc, char** argv) {
int i; // current row number
int n; // total number of rows
int j; // current column number
printf("Please enter number of ROWS : ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
//prints space
for (j = 1; j <= (n - i); j++)
{
printf(" ");
}
//prints a star and space
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
return (EXIT_SUCCESS);
}

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


4. Write a program to display the following.
ABCDEDCBA
ABCDEDCBA
ABCDEDCBA
ABCDEDCBA
ABCDEDCBA
Program:#include<stdio.h>
#include<process.h>
#include<ctype.h>
int main()
{
char ch = 0;
int i = 0;
int j = 0;
printf(Please enter a alphabet :);
scanf(%c,&ch);
if( ! isalpha(ch) ){
printf(\nThe entered character is not an alphabet);
exit(EXIT_FAILURE);
}
ch = toupper( ch );
for (i = A; i <= ch; i++ ){
printf(%c,i);
}
for (i = ch - 1; i >= A; i-- ){
printf(%c,i);
}
for( i = ch - i; i >= A; i-- ){
printf(\n);
for( j = A; j <= i; j++)
{
printf(%c, j);
}
for( j = 1; j <= 2 * (ch - 1) 1; j++ ){
printf( );
}
for( j = I; j >= A; j-- ) {
printf(%c,j);
}
}
return 0;
}

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


4. Write a program to display the following.
*******
*******
*******
*******
*******
*******
*******
Program:#include<stdio.h>
int main()
{
int i = 0;
int j = 0;
int num = 0;
printf(\nPlease enter a integer);
scanf(%d,&num);
for( i = 1; i <= (2 * num) 1; I++ )
{
print(*);
}
for( i = n 1; j >= 1; i-- )
{
printf(\n);
for( j = 1; j <= i; j++ )
{
printf(*);
}
for( j = 1; j <= 2*(num - 1); j++ )
{
printf( );
}
for( j = 1; j <= I; j++ )
{
printf(*);
}
}
for( i = 2; i < num; i++ )
{
printf(\n);
Impetus IT Services Pvt.Ltd.
B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


for( j = 1; j <= i; j++ )
{
printf(*);
}
for( j = 1; j <= 2*(num 1)-1; j++ )
{
printf( );
}
for( j = 1; j <= i; j++ )
{
printf( );
}
}
printf(\n);
for( i = 1; i <= (2 * num)-1; i++ )
{
printf(*);
}
return;
}

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


5. Write the program for the following
*****
****
* * * * **
*****
*****
Program:#include<stdio.h>
int

main()

{
int i = 0;
int j = 0;
int num = 0;
printf(Please enter a integer);
scanf(%d,&num);
num = ( num % 2 ) ? num + 1 : n;
for( i = 1; i <= num; j++ )
{
printf(\n);
for( j = 1; j <= num; j++ )
{
if( i == j) !! (i == (n j) + 1 )
{
printf(*);
}
else
{
printf( );
}
}
}
Return 0;}

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


6. Write the program to display the following
*****
*****
*****
*****
*****
Program:int main(){
int j = 0;
int i = 0;
int num = 0;
printf(\nPlease enter a integer );
scanf(%d,&num);
printf(\n);
for( i = 1; i <= num; i++ ){
printf(*);
}
for( i = 2; i < num; i++ ){
printf(\n*);
for( j = 2; j < num; j++ )
{
printf(*);
}
printf(*);
}
printf(\n);
for( i = 0; i <= num; i++ ){
printf(*);
}
Return 0;
}

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


Nested while and do...while
Like for loops, while and do...while loops can also be nested. An example is

#include <stdio.h>
main ()
{
int

x;

char
i

i,

ans;

do{
x

0;

ans

y ;

printf (\nEnter

sequence

of

character:);

do{
i

getchar () ;

x++;
} while
i =

(i

!=

\n);

printf (\nNumber of characters entered: %d,--x);


printf(\nMore
ans
}

while

sequences

(Y/N)?);

getchar () ;
(ans

==

| |

ans

== Y);

This program code first asks the user to enter a sequence of characters till the enter
key is hit (nested while), it then asks if more sequences of characters are to be entered. If
yes (outer while), it prompts the user to enter another sequence. This goes on till the user
hits any other key except y or Y. The program then terminates.

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


Jump Statements
C has four statements that perform an unconditional branch : return, goto, break,
and continue. Unconditional branching means, transfer of control from the point where it
is, to a specified statement. Of the above jump statements return and goto can be used
anywhere in the program, whereas break and continue statements are used in conjunction
with any of the loop statements.

for

(...)
for

(...)
for

(...)

while

(...)

if

(...)

goto

errorl;

.
.
.
}
}
}
}
errorl:

printf

(Error

!!!);

As seen, the label appears as a prefix to another statements in the form,


label:

statement
or

label:

{
statement

sequence

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


The break Statement
The break statement has two uses. It can be used to terminate a case in the switch
statement and/or to force immediate termination of a loop, bypassing the normal loop
conditional test.
When the break statement is encountered inside a loop, the loop is immediately
terminated and the program control passes to the statement following the loop. For example,
#include

<stdio.h>

main ()
{
int

i,

for ( i

j;
= 1, j = 0; i <= 100;

i++ )

{
printf(Enter
scanf(%d,
if

( j

==

%d

i );

&j);
100 )

break;
}
}
In the above code, the user can enter 100 values for j, however, if 100 is entered, the loop
terminates and control is passed to the next statement.
Another point to be remembered while using a break is that it causes an exit from an
inner loop. This means that if a for is nested within another for, and a break statement is
encountered in the inner loop, the control is passed back to the outer for loop.

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

IT SERVICES PRIVATE LIMITED


The continue Statement
The continue statement causes the next iteration of the enclosing loop to begin
.When this statement is encountered in the program, the remaining statements in the body
of the loop are skipped and the control is passed to the re- initialization step.
For the for loop, continue causes the increment portions of the loop and then the
conditional test to execute. For the while and do.. .while loops, program control passes to
the conditional tests. For example,
#include <stdio.h>
main ()
{
int i ;
for ( i = 1;
i <= 100; i++ )
{
if
(i %
9 == 0)
continue;
print f (%d\t,
i) ;
}
}
The above prints all numbers from 1 to 100 which are not divisible by 9. The output will look
somewhat like this.
1 2 3 4 5 6 7 8 10 11 12 13 14 15 16 17
19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35
37 38 39 40 41 42 43 44 46 47 48 49 50 51 52 53
55 56 57 58 59 60 61 62 64 65 66 67 68 69 70 71
73 74 75 76 77 78 79 80 82 83 84 85 86 87 88 89
91 92 93 94 95 96 97 98 100

The exit()
The function exit() is a standard C library function, Its working is similar to the working
of a jump statement, the major difference being that the jump statements are used to break
out of a loop, whereas exit() is used to break out of the program. This function causes
immediate termination of the program and control is transferred back to the operating
system. An exit() is usually used to check if a mandatory condition for a program execution
is satisfied or not. The general form of an exit() is
exit(int

return _code);

where return _code is optional. Zero is generally used as a return _code to indicate normal
program termination. Other values indicate some sort of error.

Impetus IT Services Pvt.Ltd.


B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in

You might also like