Sunday, 1 April 2018

The Break and Continue Statement

The Break Statement :-
   The keyword break allows us to jump out of a loop instantly without waiting to get back to the conditional test. When the keyword break is encountered inside any C loop, automatically passes to the first statement after the loop. For e.g. the following program is to determine whether a number is prime or not

 Logic : To test a number is prime or not, is to divide it successively by all numbers from 2 to one less than itself. If the remainder of any of the divisions is zero, the number is not a prime.

Following program implements this logic
  main( )
  {
   int num i;
   printf("Enter a number");
   scanf("%d",&num);
   i = 2;
   while (i <= num-1)
   {
     if(num% i = = 0)
      {
        printf("Not a prime number");
        break;
      }
     i++;
   }
   if(i == num)
   printf("Prime number");
 }

The Continue Statement:-
   The keyword continue allows us to take the control to the beginning of the loop bypassing the statements inside the loop which have not yet been executed. When the keyword continue is encountered inside any C loop control automatically passes to the beginning of the loop.

 For example
 main( )
 {
  int i,j;
  for(i=1; i<=2; i++)
  {
    for(j=1; j<=2; j++)
     {
       if(i = = j)
       continue;
       printf("%d%d", i,j);
      }
    }
  }

The output of the above program would be.........
 12
 21 

When the value of i equal to that of j, the continue statement takes the control to the for loop (inner) bypassing rest of the statement pending execution in the for loop (inner).

0 Comments:

Post a Comment

Popular Posts

Categories

AI (32) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (888) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses