Jump Statements in c Programming n
Jump Statements in c Programming n
The continue statement in C is a jump statement that is used to bring the program control to the
start of the loop. We can use the continue statement in the while loop, for loop, or do..while loop to
alter the normal flow of the program execution. Unlike break, it cannot be used with a C switch
case.
What is continue in C?
The C continue statement resets program control to the beginning of the loop when encountered.
As a result, the current iteration of the loop gets skipped and the control moves on to the next
iteration. Statements after the continue statement in the loop are not executed.
Syntax of continue in C
The syntax of continue is just the continue keyword placed wherever we want in the loop body.
continue;
use of continue in C
The continue statement in C can be used in any kind of loop to skip the current iteration. In C, we
can use it in the following types of loops:
• Single Loops
• Nested Loops
Using continue in infinite loops is not useful as skipping the current iteration won't make a
difference when the number of iterations is infinite.
Example of continue in C
The continue statement can be used in for loop, while loop, and do-while loop.
printf( i);
printf("\n");
int i -o,
// while loop to print 1 to 8
while (i < 8) {
// wheni = 4, the iteration will be skippedand for
// will not be printed
if (i
continue ;
printf( i);
return 0;
Output
123 5 6 7 8
123 5 6 7 8
Example 2: C Program to use continue in a nested loop
The continue statement will only work in a single loop at a time. So in the case of nested loops, we
can use the continue statement to skip the current iteration of the inner loop when using nested
loops.
printf("\n");
return 0;
Output
0124
0124
0124
The continue skips the current iteration of the inner loop when it executes in the above program. As
a result, the program is controlled by the inner loop update expression. In this way, 3 is never
displayed in the output.
if(condition)
continue;
Continue
Condition True
False
Remaining Part of
Loop Body
Example: C Program to demonstrate the difference between the working of break and
continue statement in C.
Output
The continuestatement in C is used in loops to skip the current iterationand move on to the
next iteration without executingthe statements below the continuein the loop body.
The break and continuein C are jump statements that are used to alter the flow of the normal
executionof the loops.
Break Statement in C
Last Updated : 03 Oct, 2024
The break statement is one of the four jump statements in the C language. The purpose of the
break statement in C is for unconditional exit from the loop
What is break in C?
The break in C is a loop control statement that breaks out of the loop when encountered. It can be
used inside loops or switch statements to bring the control out of the block. The break statement
can only break out of a single loop at a time.
Syntax of break in C
break;
We just put the break where ever we want to terminate the execution of the loop.
use of break in C
The break statement in C is used for breaking out of the loop. We can use it with any type of loop to
bring the program control out of the loop. In C, we can use the break statement in the following
ways:
• Simple Loops
• Nested Loops
• Infinite Loops
• Switch case
Examples of break in C
Break statements in C can be used with simple loops i.e, for loops, while loops, and do-while loops.
return 0,
Output
Break statements can also be used when working with nested loops. The control will come out of
only that loop in which the break statement is used.
// Cprogramto illustrate
// using break statement
// in Nestedloops
#include <stdåo.h>
int main()
// nested for loops with break statement
// at innerloop
for (int i
for (int j -
if (i 4)
printf(
else {
// if i > 4 then this innermostloop will
// break
break;
printf( " );
return 0,
Output
12
123
1234
Note: Break statement only breaks out of one loop at a time. So if in nested loop, we have
used break in inner loop, the control will come to outer loop instead of breakingout of all the
loops at once. Vt/ewill have to use multiple break statements if we want to break out of all
the loops.
An Infinite loop can be terminated with a break statement as a part of the condition.
int i
return o,
Output
01234
In the above program, the loop condition is always true, which will cause the loop to execute
indefinitely. This is corrected by using the break statement. Iteration of the loop is restricted to 8
iterations using break.
How break statement works?
//code
if(condition to break)
break;
//code
Flowchart of break in C
Break Statement Flow Diagram
switch(expression)
case valuel:
statement 1;
break;
case value2:
statement 2;
break;
case value n:
statement n;
break;
default:
default statement;
Example of break in switch case
printf("Enter TwoValues:\n
scanf("%f , &x, &y);
switch (c) {
// For Addition
Case
printf("%.lf + %.lf y,
break;
// For Subtraction
case
printf("%.lf - %.lf
break;
default :
printf(
'"Error! please write a valid operator\n");
Output:
Conclusion
In this article, we have discussed the break statement and how it can be used to alter the flow of
the normal execution of the C program. The break statement is particularly useful when we want to
terminate the loop earlier than the normal logic.
The break statement in C is used for early terminationand exit from the loop.
The differencebetween the break and continuein C is listed in the below table:
break continue
The break statement terminates the loop and The continue statement terminates only the current
brings the program control out of the loop. iteration and continues with the next iterations.
The break can also be used in switch case. Continue can only be used in loops.
goto Statement in C
Last Updated : 11 Mar, 2023
The C goto statement is a jump statement which is sometimes also referredto as an unconditional
jump statement. The goto statement can be used to jump from anywhere to anywhere within a
function.
Syntax:
Syntaxl Syntax2
In the above syntax, the first line tells the compiler to go to or jump to the statement marked as a
label. Here, the label is a user-defined identifier that indicates the target statement. The statement
immediately followed after 'label:' is the destination statement. The 'label:' can also appear before
the 'goto label;' statement in the above syntax.
Labell: Statement 1
goto
Labe12: Statement 2
be13
Labe13: Statement 3
stop
Type 1: In this case, we will see a situation similar to as shown in Syntaxl above. Suppose we need
to write a program where we need to check if a number is even or not and print accordingly using
the goto statement. The below program explains how to do this:
if (num 70 2 0)
// jump to even
goto even;
else
// jump to odd
goto odd;
even:
printf( is even", num )
// return if even
return;
odd :
printf( is odd", num )
int main ( )
Output:
26 is even
Type 2: In this case, we will see a situation similar to as shown in Syntax2 above. Suppose we need
to write a program that prints numbers from 1 to 10 using the goto statement. The below program
explains how to do this.
int n
label:
printf( n);
if (n 10)
goto label'
printNumbers();
return O ;
Output:
12345678910
Disadvantages of Using goto Statement
• The use of the goto statement is highly discouraged as it makes the program logic very complex.
• The use of goto makes tracing the flow of the program very difficult.
• The use of goto makes the task of analyzing and verifying the correctness of programs
(particularly those involving loops) very difficult.
• The use of goto can be simply avoided by using break and continue statements.