Continue and goto in C
Like the break statement, the continue statement can be placed only in the body of a for loop, a while loop, or a do…while loop. When a continue statement executes, the next iteration of the enclosing loop begins immediately. The statements between the continue statement and the end of the loop aren’t executed. The operation of continue is also shown in Figure 13.1. Notice how this differs from the operation of a break statement.
Continue statement flow diagram
# include <stdio.h>
int main(){
int x;
printf(“Printing only the even numbers from 1 to 10\n”);
for( x = 1; x <= 10; x++ )
{
if( x % 2 != 0 ) /* See if the number is NOT even */
continue; /* Get next instance x */
printf( “\n%d”, x );
}
}
Left to itself, the for loop would execute 10 times. On the sixth iteration, however, count is equal to 5, and the break statement executes, causing the for loop to terminate. Execution then passes to the statement immediately following the for loop’s closing brace. When a break statement is encountered inside a nested loop, it causes the program to exit the innermost loop only.
Example
#include <stdio.h>
int main () {
/* local variable definition */
int a = 10;
/* do loop execution */
do {
if( a == 15) {
/* skip the iteration */
a = a + 1;
continue;
}
printf("value of a: %d\n", a);
a++;
} while( a < 20 );
return 0;
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
The goto Statement
The goto statement is one of C’s unconditional jump, or branching, statements. When program execution reaches a goto statement, execution immediately jumps, or branches, to the location specified by the goto statement. This statement is unconditional because execution always branches when a goto statement is encountered; the branch doesn’t depend on any program conditions (unlike if statements, for example).
Continue statement flow diagram
#include <stdio.h>
int main()
{
const int maxInput = 5;
int i;
double number, average, sum=0.0;
for(i=1; i<=maxInput; ++i)
{
printf("%d. Enter a number: ", i);
scanf("%lf",&number);
// If user enters negative number, flow of program moves to label jump
if(number < 0.0)
goto jump;
sum += number; // sum = sum+number;
}
jump:
average=sum/(i-1);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f", average);
return 0;
}
This is a simple program that accepts a number between 0 and 10. If the number isn’t between 0 and 10, the program uses a goto statement on line 15 to go to start, which is on line 9. Otherwise, the program checks on line 16 to see whether the number equals 0. If it does, a goto statement on line 17 sends control to location0 (line 23), which prints a statement on line 24 and executes another goto. The goto on line 25 sends control to end at the end of the program. The program executes the same logic for the value of 1 and all values between 2 and 10 as a whole.
Output
1. Enter a number: 3
2. Enter a number: 4.3
3. Enter a number: 9.3
4. Enter a number: -2.9
Sum = 16.60
<br>Why do we use continue statement in C?
The 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 of statements inside the body of loop for the current iteration.
Why we use break and continue statement in C?
When a break statement is encountered, it terminates the block and gets the control out of the switch or loop. When a continue statement is encountered, it gets the control to the next iteration of the loop. … A continue inside a loop nested within a switch causes the next loop iteration.
What is the use of goto statement?
What is the use of “goto” statement? goto statement is used to transfer the normal flow of a program to the specified label in the program. Below is the syntax for goto statement in C.