Skip to main content

Continue and break statement Statement in C Program #DOTC_Mdb

Continue and break statement Statement in C Program.


Hi Everyone ,

Today we will discuss about Continue and break statement in C . Before understanding Continue and break statement in C please follow my below blog for your reference.

https://dheeraj60.blogspot.com/2020/06/while-and-do-while-loop-lop-in-c.html

https://dheeraj60.blogspot.com/2020/06/nested-for-loop-with-simple-pattern.html

https://dheeraj60.blogspot.com/2020/06/loop-concept-in-c-with-examples-dotcmdb.html

 

Let’s start with Continue Statement in C.

Continue statement is used inside loops. When a continue statement is encountered inside a loop, control jumps to the beginning of the loop for next iteration, skipping the execution statement of loop for the current iteration.  Continue statement in C programming works somewhat like the break statement. Instead of termination, it forces the next iteration of the loop to work and skipping the code in between. Break statement will cover later after Continue Statement.

In Loop :  for loop, continue statement causes the conditional test and increment part of the loop to execute. And  for the while and do...while loops, continue statement causes the program control to pass to the condition.

Syntax of Continue Statement is as below:

Continue;

Let’s understand how we can use continue statement with simple  program using while loop  and see how the value of variable can skip with using continue statement.

Below is the Program where i want value from 0 to 10 but want to skip any one value by using continue statement.

#include <stdio.h>

#include <stdlib.h>

int main()

{

for (int i=0; i<=10; i++)

   {

      if (i==2)

      {

                continue;

       }

 

       printf("%d \n", i);

   }

 

       return 0;

}

 

Let’s understand the Program Continue statement is encountered when if (i==2) condition checked here the value of variable i is equals to 2 . So the print statement for condition i==2 will not execute and this statement will be skipped and another value will be displayed in the output apart from 2. In condition you can make changes if you want to skip that particular value.

 

Below is the output of Above Program in Compiler Code- Block.

 

If you see the output 2 is not there and all value from 0 to 10 is displayed.


Hope you Understand how continue statement works in program. Let’s take one another example to understand continue statement by using do while  to give same condition and can skip any value  by giving condition in If statement.

 

Below is the Program.

#include <stdio.h>

#include <stdlib.h>

int main()

{

 int i=0;

   do

   {

      if (i==0)

      {

         i++;

         continue;

      }

      printf("%d \n", i);

      i++;

   }

   while(i<=10);

 

       return 0;

}

 

Same Program which we write using for loop but here we give condition if (i==0 ) and use same condition by using do while loop  so here it will skip 0 value and print the value from 1 to 10 .

 

Below is the output of above Program in Compiler Code Block.

 

 

 

Hope you are able to understand the use of continue statement , you can try continue statement for other programs which i have written in my previous blog as per your requirement.

 

Break Statement in C :  Break Statement is used to come out of the loop instantly. When you use break statement inside a loop, the control directly comes out of loop and the loop gets terminated. It is used with if statement, whenever used inside loop. In other word we can say break statements are used in the situations when we are not sure about the actual number of iterations for the loop or we want to terminate the loop with some condition. Whereas in continue statement we just skip one condition but through break statement it check the if condition and print that value and loops get terminated after that and only the conditional value will be the output.

Syntax for break statement.

break;

Let’s see the same example which we used in continue statement and it follows the IF condition and after break it will give the output which satisfy the if condition.

Below is the Program.

#include <stdio.h>

#include <stdlib.h>


int main()

{

 int i =0;

     while(i<=10)

     {

        printf("%d\n", i);

        if (i==3)

        {

            break;

        }

        i++;

     }

     printf(" out of the while loop");

 

       return 0;

}

Here it will check the if condition as we given the condition if (i==3) then break statement so it will give the value from 0 to 3 then it will out of the while loop . Not like continue where it can skip 3 and give all the output .

Below is the output of above program in compiler Code – Block.

 

 

Output value is 0 to 3 as it prints the value for the the condition if (i==3) then we use break statement so it will get out from the loop  . As per requirement we can make changes in condition.

Let’s take one more example by using for loop for break statement where the same program but i want value from 0 to 8 then use break statement to terminate the loop. 

Below is the Program.


#include <stdio.h>

#include <stdlib.h>

 

int main()

{

 

 int i;

      for (i =0; var<=10; i ++)

      {

           printf("%d\n", i);

           if (i==8)

           {

               break;

           }

      }

     printf("Out of for-loop");

 

       return 0;

}

  

Here we put the condition if (i==8) after that break statement so it will give the value from 0 to 8 and after that loop gets terminated. So if we are not sure about iteration we can give condition as per our requirement and use break statement to get out from the loop.

Below is the output of above program in Compiler Code – Block.



Here you see the output is 0 to 8 as in condition if (i==8) after that break statement is used.

Now you are able to understand the break statements as well as it’s similar to continue but work differently as in break  it work till IF condition and terminate the loop  till the condition while in continue it check the condition and skip that portion only and display all the value .

Hope you are able to understand both break and continue statement in C program. Please write some other code and also take reference from my previous blog and write some programs and make use of break and continue statement to control over programs. For any doubts on Programs let me know .We can also use break in switch case statement which i will cover in next blog .


Please read this blog carefully and use this statement in different program as per your requirement. Please feel free to reach me for any questions.

Thanks.


Comments

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Basic SQL Commands in Oracle Database #Diksha Oracle Training Centre

  Basic SQL Commands in Oracle Database.   Hello Everyone, In my previous blog I discussed about SQL and Classification of SQL . Today I will discuss about SQL basic commands which widely used in RDBMS. The Topics of SQL command which I am going to cover in this blog are mainly divided into four Categories: ·         DDL:   DDL consists of commands which are used to define and design the database. ·         DML:   DML consists of commands which are mainly used to manipulate the data in the database. ·        DCL: DCL Consists of commands which deal with the user permissions/access and controls in the database. ·       TCL:   TCL Consist of commands which deal with the transaction in the database. If you want to explore theory part please follow my below blog as in today blog I will discuss about how to use the commands. h...

Pseudo Column in Oracle #Diksha Oracle Training Centre

  Pseudo Column in Oracle. Hello Everyone, Today I will discuss about Pseudo column in Oracle. This topic is very important so before going to read this blog, I suggest you to please see my previous blogs which is in archive folder for your reference so that it’s easy for you to understand this topic. Let’s discuss about Pseudo Column in Oracle. Pseudo column: A pseudo-column is dynamic in Oracle as it behaves like a table column but is not stored in the table. Pseudo column behaves like a table column, but is not actually stored in the table. You can select from pseudo columns, but you cannot insert, update, or delete their values. A pseudo column is also similar to a function without arguments. In   Simple word we can say Pseudo column is a column that yields a value when selected, but which is not an actual column of the table. An example is RowID or SysDate. It can be use in combination with the DUAL table. Below are the Pseudo Column commonly used in Oracle Dat...