Facebook

Loop Control Statements

Loop Control Statements alter  execution of a program from the normal execution sequence.The statements are as follows

  • break
  • continue
  • goto

break statement

break statement terminates the execution of loop immediately ineffective of the condition status.The control is transferred to the first statement after the loop block.If break statement is used in nested loops      (Refer : Nesting of Loops) then innermost loop is terminated and control is transferred to outer loop.

Syntax

break;

Program 1

#include <stdio.h>

int main() 
{
	int i;
	for(i=1;i<20;i=i+2)
	{
		if(i>15)
		{
		break;
		}
	printf("%d\n",i);
	}
	return 0;
}

Run

 

Program 2

#include <stdio.h>

int main() 
{
int i;
while(i<=15)
{
if(i%2 == 0)
{
	printf("%d\n",i);
}
i++;
if(i>10)
{
	break;
}

}
	return 0;
}

Run

continue statement

continue statement forces to execute( instead of termination)the next iteration and skips some statements.

Syntax

continue;

Program 3

#include <stdio.h>

int main()
{
	int i;

	for(i=0;i<10;i++)
	{
		if(i >5)
		{
		continue;
		i=i+5;
		printf("%d\n",i);
		}
		printf("%d\n",i);
		}
	return 0;
}

Run

goto statement

goto statement jumps the control to a desired labelled statement of a program.The use of goto is generally avoided as it leads to difficult understanding of the control flow.

Syntax

goto label;

.....

....

label:statement;

 

Program 4

#include <stdio.h>
 
int main ()
{
   int i,j;
   int num;
   //printf("Enter the no");
   scanf("%d",&num);
   for(i=0;i<10;i++)
   {
   	if(num>5)
   	{
   		num = num +2;
   		printf("%d\n",num);
   	}
   	else
   	{
   		goto LOOP;
   	}
   }
   LOOP:for(i=0;i<10;i++)//LOOP is a label
   {
   	num=num+3;
   	printf("%d\n",num);
   }
   return 0;
}

Run

Rate this post

Comments

This post currently has 3 responses

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar