SWITCH , BREAK AND CONTINUE STATEMENT IN C …

Hello Coders, I hope you all are doing great in Coding. In this blog, we are going to study about some statements in C Language which are commonly used while coding.

1.)BREAK AND CONTINUE STATEMENT-:


a.)Break statement-:

Sometimes we want to jump out of the loop when a certain condition is fulfilled without repeating the loop again and again. When we use break statement in any loop, control exits through the loop and passes to statement following loop. Break statement is generally used with if condition and in loop statements.  A break statement is written as break; . 
Let’s understand it by an example -:
 
Example 1-:
 
#include<stdio.h>
#include<conio.h>
void main()
{
   clrscr();
   int i=1;
   while(i<=5)
   {
     if(i==3)
       {
          break;
       }
   printf(“%d”,i);
   i++;
   }
    getch();
}   
 
OUTPUT
1
2
 
Here you can see we have initialized i from 1 and until i is less than or equal to 5 we are repeating the loop, Inside while loop we have used if statement which checks for a condition and breaks the loop if the condition evaluated is true. Here when i get equal to 3 compiler directly jumps to getch(); rather than repeating the loop with i=4. All processing in the loop after break statement get stops. 
 
So, we understood that break statement exits control from the loop and pass to just next statement after the loop.

b.)Continue statement-:

Break statement breaks the loop, just opposite of it is continue statement. Continue statement repeats the loop when a certain condition is fulfilled rather than processing the next statement in the loop. You will get more clear about it by an example.
 
Example2 -: Program to print integers from 1 to 5 excluding 3.
 
#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    int i=0;
    while(i<5)
    {
        i++;
        if(i==3)
        {
            continue;
        }
        printf(“%d\n”,i);
    }
    getch();
}
 
OUTPUT
1
2
4
5
 
Here we initialized i from 0 and until i is less than 5 we are repeating loop. First, i=0  through i++  i becomes 1 and in if condition it is checked whether i=3 or not if not then directly printf(); works and 1 get printed. Than i becomes 2 compiler check whether i=3, here not therefore 2 get printed.  When i get equal to 3 compiler enters in if statement and continue statement repeats the loop rather than printing the value 3. Means, we are telling the compiler to skip print statement is i=3 and work with the next number. Again loop starts and value of i become 4 and then 5. That’s how values get printed. Notice that if we write code as -:
 
void main()
{
 clrscr();
 int i=0;
 while(i<5)
  {
     if(i==3)
     {
         continue;
     }
      printf(“%d\n”,i);
      i++; 
  }
 getch();
}
 
Here we will receive output as
0
1
2
Its because when i=3 we are not increasing the value of i but only continuing the loop by continue statement. So values upto 2 get printed. 
 
So we understood that continue statement bypasses statements in the loop at certain condition. 

 

2.)SWITCH STATEMENT-:

This statement allows the user to make a decision from a number of choices. Sometimes we want to execute different processing for a different set of choices. We will understand by an example but first, we should see the syntax of switch statement.
Syntax-:
 
Switch(n)
{
    case 1:
      do this();
    break();
    case 2:
      do this();
    break();
    case 3:
       do this();
    break();
   default:
        do this();
 
Here n is an integer expression which will identify the case to be executed. In line case 1; case is the keyword which consists of an integer. The integer evaluated in the switch(n) will be matched from cases below and the command just after that will be executed. Let’s understand by an example -:
 
Example-:
 
#include<stdio.h>
#include<conio.h>
void main()
{
   clrscr();
   int i=2;
   switch(i)
   {
       case 1:
          printf(“Hello\n”);
          break;
       case 2:
          printf(“How are you\n”);
          break;
       case 3:
          printf(“I am f9\n”);
          break;
       default:
          printf(“Good night\n”);
   }
   getch();
}
 
OUTPUT
How are you 
 
So here what happened is first compiler checks value of i in switch(i) which is 2 and then choose the case to be executed for n=2. 
Here we can also choose the value of n through input statement scanf(); . 
 
 
So that’s all in this blog i hope you are now comfortable with switch , break and continue statement. If you have any doubt please let me know. Comment your doubts in the comment box. 
Thanks …..

Leave a comment

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started