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 etc. SQL allows

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you query the ma

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li