Categories: C

Jump statements in C

In some programming cases, we want to take control to the start of the loop, skipping the statements inside the loop, and sometimes we want to jump out of the loop. For these purposes, we have some special statements known as the jump statement in C. They are :

  • break statement
  • continue statement
  • goto statement
break statement:

The break statement allows us to jump out of the loop instantly, without waiting to get back to the conditional test. When the break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if.

Example: Write a program to check whether a number is prime or not.


#include <stdio.h>

void main() {

  int n, i, flag = 0;
  printf("Enter a positive integer: ");
  scanf("%d", &n);

  // 0 and 1 are not prime numbers
  // change flag to 1 for non-prime number
  if (n == 0 || n == 1)
    flag = 1;

  for (i = 2; i <= n / 2; ++i) {

    // if n is divisible by i, then n is not prime
    // change flag to 1 for non-prime number
    if (n % i == 0) {
      flag = 1;
      break;
    }
  }

  // flag is 0 for prime numbers
  if (flag == 0)
    printf("%d is a prime number.", n);
  else
    printf("%d is not a prime number.", n);
    
    }

output:

Enter a positive integer: 23
23 is a prime number.
continue statement:

continue statement is used when we want to take control to the start of the loop, skipping the statements inside the loop, which have not yet been executed. When continue is encountered inside any loop, control automatically passes to the beginning of the loop. A continue is also usually associated with an if.

Example:


#include <stdio.h>

void main() {
   int i,j;
   for(i=1;i<=2;i++)
   {
       for( j=1;j<=2;j++)
       {
           if(i==j)
           continue;
         printf("\n%d %d",i,j);  
       }
   }
 }

Output:

1 2
2 1

Here, when the value of i equals that of j, the continue statement takes the control to the for loop
(inner) skipping the rest of the statements pending execution in the for loop(inner).

goto statement:

This statement is used to transfer control to the tagged statement in the program. The tag is the valid identifier and placed just before the statement from where the control is transferred. The usage of the goto keyword should be avoided as it usually violates the normal flow of execution.

Example:

#include <stdio.h>

void main() {
   int marks;
   printf("enter marks: ");
   scanf("%d",&marks);
   if(marks<38)
      goto sos;
   else
       printf("You have passed");
       
   sos:printf("you have failed");
   
 }

Output:

enter marks: 12
you have failed

Note: also read about do-while loop in c & while loop examples in c

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago