0% found this document useful (0 votes)
14 views

Jump Statements in c Programming n

The continue statement in C is used to skip the current iteration of a loop and continue with the next iteration, applicable in while, for, and do-while loops, but not in switch cases. It alters the normal flow of execution by jumping to the beginning of the loop when encountered. The document also compares the continue statement with the break statement, which exits the loop entirely, and discusses the syntax, usage, and examples of both statements.

Uploaded by

kussupurne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Jump Statements in c Programming n

The continue statement in C is used to skip the current iteration of a loop and continue with the next iteration, applicable in while, for, and do-while loops, but not in switch cases. It alters the normal flow of execution by jumping to the beginning of the loop when encountered. The document also compares the continue statement with the break statement, which exits the loop entirely, and discusses the syntax, usage, and examples of both statements.

Uploaded by

kussupurne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Continue Statement in C

Last Updated : 04 Oct, 2024

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

Example 1: C Program to use continue statement in a single loop.

The continue statement can be used in for loop, while loop, and do-while loop.

// C programto explain the use


// of continue statement with for loop
#include <stdio.h>
int main()
// for loop to print 1 to 8
for (int i
// wheni = 4, the iteration will be skippedand for
// willnot be printed
if (i
continue ;

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.

// C programto explain the use


// of continue statement with nested loops
#include <stdio.h>
int main()

// outer loop with 3 iterations


for (int i
// inner loop to print integer 1 to 4
for (int j
// continue to skip printing number3
if (j
continue ;
pr intf( "%d

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.

How continue statement works?

for( init; condition; update)

if(condition)
continue;

Working Of C continue in for Loop

The working of the continue statement is as follows:


• STEP 1: The loop's execution starts after the loop condition is evaluated to be true.
• STEP 2: The condition of the continue statement will be evaluated.
• STEP 3A: If the condition is false, the normal execution will continue.
• STEP 3B: If the condition is true, the program control will jump to the start of the loop and all
the statements below the continue will be skipped.
• STEP 4: Steps 1 to 4 will be repeated till the end of the loop.
Flowchart of continue in C
LOOPBody

Continue

Condition True

False

Remaining Part of
Loop Body

Flowchart Of the continue Statement in C

C break and continue Statement Differences


break statement: By using break statement we terminate the smallest enclosing IQQÆ(e.g, a
while,do-while, for,or switch statement).

continuestatement: By usingthe continuestatement,the loopstatementis skippedand the


next iteration takes place instead of the one prior.

Example: C Program to demonstrate the difference between the working of break and
continue statement in C.

// C program to demonstrate difference between


// continue and break
#include <stdio.h>
int main()

printf("The loop with break produces output as:


for (int i
// Programcomesout of loop when
// i becomesmultiple of 3.
if (i -
break;
else
pr intf( "%d i);

printf("\nThe loop with continue produces output as:


for (int i
// Theloop prints all values except
// those that are multiple of 3.
if (i
continue ;
printf( i);
return 0;

Output

The loop with break produces output as:


12
The loop with continue produces output as:
124567
Explanation: In the above program, the first loop will print the value of i till 3 and will break the
loop as we have used a break statement at i equal to 3. And in the second for loop program will
continue but will not print the value of i when i will be equal to 3.
Conclusion
In this article, we have discussed continue statement which is one of the four jump statements in C.
We also studied its syntax, working, and how we can use it to alter the normal flow of out C
program.

Continue Statement in C —FAQs

What is the use of continue statement in C?

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.

What type of statements are break and continue?

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

Example 1: C Program to use break Statement with Simple Loops

Break statements in C can be used with simple loops i.e, for loops, while loops, and do-while loops.

// C Programto demonstratebreak statement with for loop


#include <stdåo.h>
int main()

// using break inside for loop to terminate after 2


// iteration
printf( "break in for loop\n");
for (int i
if (i -
break;
else {
printf("%d i);

// using break inside while loop to terminate after 2


// iteration
printf( "\nbreak in while loop\n");
int i
while (i < 20) {
if (i -
break;
else
printf("%d i);

return 0,

Output

break in for loop


12
break in while loop
12
Example 2: C Program to use break Statement with Nested Loops

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.

Example 3: C Program to use break Statement with Infinite Loops

An Infinite loop can be terminated with a break statement as a part of the condition.

// C Programto demonstrateinfinite loop without using


// break statement
#include <stdio.h>
int main()

int i

// while loop whichwill alwaysbe true


while (1) {
printf("%d", i);
if
break;

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?

for( init; condition; operation)

//code
if(condition to break)
break;

//code

Working Of break in a for loop

The working of the break statement in C is described below:


1. STEP 1: The loop execution starts after the test condition is evaluated.
2. STEP 2: If the break condition is present the condition will be evaluated.
3. STEP 3A: If the condition is true, the program control reaches the break statement and skips the
further execution of the loop by jumping to the statements directly below the loop.
4. STEP 3B: If the condition is false, the normal flow of the program control continues.

Flowchart of break in C
Break Statement Flow Diagram

Break in C switch case


In general, the Switch case statement evaluates an expression, and depending on the value of the
expression, it executes the statement associated with the value. Not only that, all the cases after
the matching case after the matching case will also be executed. To prevent that, we can use the
break statement in the switch case as shown:

Syntax of break in switch case

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

// C Programto demonstrateworkingof break with switch


// case
#include <stdåo.h>
#include
int main()
char c;
float x, y;
while (1) {
printf( "Enter an operator (4, if want to exit
"press x: '
scanf(" %c", &c);
// to exit
if (c - 'x')
exit(0) ;

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:

Enter an operator if want to exit press x:


Enter Two Values:
10
20
10.0 + 20.0 = 30.0

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.

Break Statement in C —FAQs

What is the use of a break statement in C?

The break statement in C is used for early terminationand exit from the loop.

What is the difference between break and continue?

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 syntax is: The syntax is:


break; continue;

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

goto label; label :

label: goto label;

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

Flowchart Ofgoto Statement in C

Below are some examples of how to use a goto statement.


Examples:

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:

// C program to check if a number is


// even or not using goto statement
Finclude < stdio.h>

// function to check even or nat


void checkEvenOrNot (int num)

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 ( )

int num 26;


checkEvenOrNot (num)
return O ;

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.

// C program to print numbers


// from 1 to 10 using goto statement
{include < stdio

// function to print numbers from 1 to 10


void printNumbers()

int n
label:
printf( n);

if (n 10)
goto label'

// Driver program to test above function


int main ( )

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.

You might also like