coderz.py

Keep Coding Keep Cheering!

Jump statements in C

Matrix multiplication

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement