Fifth Chapter Lesson-18: ‘continue’ statement, ‘break’ statement and ‘goto’ statement

At the end of this lesson-

  • 1. You will be able to write a program using ‘continue’ statement.
  • 2. You will be able to write a program using ‘break’ statement.
  • 3. You will be able to write a program using ‘goto’ statement.

 

‘continue’ statement: The continue statement is used inside loops to bring the program control to the beginning of the loop. When a continue statement is encountered inside a loop, statement skips some lines of code inside the loop body and continues with the next iteration. It is mainly used for a condition so that we can skip some code for a particular condition.

The syntax for a continue statement in C is as follows −

continue;

 

How ‘continue’ statement works- 

Working of continue statement in C programming

 

A program using ‘continue’ statement in for loop to print all the odd numbers between 1 to 10. 

/* Program using for loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i;
    for(i=1; i<=10; i=i+1)
    {
        if(i%2==0)
           continue; 
        printf("%d\t ",i);
    }
    getch();
}

In the above program, when test_expression of the loop body becomes true, then ‘continue’ statement works.  ‘continue’ statement works that means skipping execution of printf() function and continues with the next iteration.

 

‘break’ Statement: The ‘break’ is a keyword in C and break Statement is a loop control statement which is used to terminate the loop. The break statement is used inside loops or switch statement. As soon as the break statement is encountered from within a loop, the loop iterations stops there and control returns from the loop immediately to the first statement after the loop. In the case of nested loops, it breaks the inner loop first and then proceeds to outer loops. Basically 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 based on some condition.

The syntax for a break statement in C is as follows −

break;

 

How ‘break’ statement works- 

Working of break statement

 

Let’s see the following program-

/* Program using for loop */
#include<stdio.h>
#include<conio.h>
main()
{
    int i;
    for(i=1; i<=5; i=i+1)
    {
        printf("Bangladesh\n");
        if(i==3)
           break;      
    }
    getch();
}

In the above program, iteration of the loop will be three times instead of five times. Because, when condition of the loop body will be true, break statement will work that means the control directly comes out of loop and the loop gets terminated. As a result Bangladesh word will be printed three times.

 

‘goto’ statement: The ‘goto’ statement is known as jump statement in C. goto is used to transfer the program control to a predefined label. The goto statment can be used to repeat some part of the code for a particular condition. It can also be used to break the multiple loops which can’t be done by using a single break statement. However, the goto statement is rarely used because it makes program confusing, less readable and complex.

The syntax for a goto statement in C is as follows −

label:
----------
----------
goto label;

OR

goto label;
-----------
-----------
label:

In the above syntax, here label is a user defined identifier which indicates where program execution control will be shifted. To write this identifier it should be followed the rules of writing identifier. Do remember, this identifier ends with colon(:) instead of semicolon(;).

 

Let’s see the the following program that determine LCM of two numbers. 

#include<stdio.h>
#include<conio.h>
main()
{
     int a,b,l;
     printf("Enter the two numbers: ");
     scanf("%d %d",&a,&b);
     if(a>b)
        l=a;
     else
        l=b;
     again:
     if(l%a==0 && l%b==0)
        printf("LCM of %d and %d is %d",a,b,l);
     else
     {
        l=l+1;
        goto again;
     }
    getch();
}

In the above program, using ‘goto’ statement program execution control has been shifted to the above particular position. 

 

Lesson Evaluation-

Knowledge Based Questions:

Comprehension Based Questions:

Creative Questions:

Multiple Choice Questions:

 


Written by,

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *