Continue Statement in C Language
In C programming language, the continue statement is used to continue the execution of the loop. It is sometimes desirable to skip some statements inside the loop. The continue statement is almost always used with the if...else statement within the block of the while, do while and for loop.
Continue statement with loop
// Program to calculate the sum of maximum 10 numbers
// If the user inputs a negative number, it's not calculated to the result
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
double num, sum = 0.0;
for (i = 1; i <= 10; ++i) {
printf("Enter a no%d: ", i);
scanf("%lf", &num);
if (num < 0.0) {
continue;
}
sum = sum + num;
}
printf("Sum = %.2lf", sum);
getch();
}
Output :
Enter no1: 1.2
Enter no2: 2.1
Enter no3: 4.5
Enter no4: 5.4
Enter no5: -15.9
Enter no6: -6.7
Enter no7: 24.5
Enter no8: -9.2
Enter no9: -500
Enter no10: 11.1
Sum = 48.80
When the user inputs a positive number, the sum is evaluted using sum = sum + num; statement.When the user inputs a negative number, then the continue statement is executed and it skips the negative number calculation.
Goto Statement
In C programming, goto statement is used to transfer the unconditional program control to some other part of the program using a labeled statement.
Syntax:
goto label; ............. ............. ............. label: statement;In this syntax, label is used as an identifier.
When, the control of program reaches to goto statement, the control of the program will jump to the label: and executes the code below of the label.
// Program to find the length of a given string
#include<stdio.h>
#include<conio.h>
void main()
{
int a = 10;
/* do loop execution */
loop: //loop is a label
do {
if( a > 13 && a<16) {
/* skip the iteration */
a = a + 1;
goto loop; //This jumps to label "loop:"
}
printf("value of a: %d\n", a);
a++;
}while( a < 20 );
getch();
}
Output :
value of a: 10
value of a: 11
value of a: 12
value of a: 17
value of a: 18
value of a: 19
ADVERTISEMENT
