Fundamentals of Computer Programming - Old Questions

5. Differentiate between break and continue statement. Write a program to display following output.  [2+4]

1

1 1

1 1 1

1 1 1 1

1 1 1 1 1

6 marks | Asked in 2070

Difference between break and continue statement

BreakContinue
Break is used to break loop or iteration.Continue continues the loop or iteration.
Used with switch case loop.Not used with switch case.
Keyword used is "break".Keyword used is "continue".
Breaks loop and allows coming out from it.Allows iterating in the loop.
Control is transferred outside the loop.Control remains in the same loop.

E.g.

  1. for(i=1; i<=5 ; i++) 
  2. { 
  3. if(i==3) 
  4. break; 
  5. Printf("%d\\n",i); 
  6. } 

Output:  1, 2

E.g.

  1. For(i=1 ;i<=5; i++) 
  2. { 
  3. if(i==3)  
  4. continue; 
  5. printf("%d\\n",i); 
  6. } 

Output: 1, 2, 4, 5

Program to display given output

#include<stdio.h>

#include<conio.h>

#define n 5

int main()

{

    int i, j;

    for(i=1;i<=n;i++)

    {

        for(j=1;j<=1;j++)

        {

            printf("\\t1");

        }

        printf("\\n");

    }

    getch();

    return 0;

}