0% found this document useful (0 votes)
55 views9 pages

Jump Statements

1. Jump statements like break, continue, and goto allow unconditional or conditional jumping within code. break exits the current loop or switch block. continue skips to the next iteration of a loop. goto jumps to a labeled statement. 2. The break statement exits the entire enclosing block such as a loop. continue skips the remaining statements in the current loop iteration. goto can jump forward or backward within a function. 3. Labels are used with goto to mark the statement being jumped to. This allows jumping anywhere in a function, but overuse of goto is discouraged for readability.

Uploaded by

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

Jump Statements

1. Jump statements like break, continue, and goto allow unconditional or conditional jumping within code. break exits the current loop or switch block. continue skips to the next iteration of a loop. goto jumps to a labeled statement. 2. The break statement exits the entire enclosing block such as a loop. continue skips the remaining statements in the current loop iteration. goto can jump forward or backward within a function. 3. Labels are used with goto to mark the statement being jumped to. This allows jumping anywhere in a function, but overuse of goto is discouraged for readability.

Uploaded by

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

Jump Statements:

    
    1. break: is used to terminate the control structures (for, while and do while, switch-case).
    
    2. continue: (for, while and do while) .
    
    3. goto: we can use anywhere in the code.
    
    4. return: in functions only
    
    
    //break
    
    #include <stdio.h>

int main()
{
    for(int i=1; i<=10; i++)
    {
         printf("%d ", i);
         break;    // unconditional jump
      }

}
   
   https://onlinegdb.com/2EK51Dxq5
   
   #include <stdio.h>

int main()
{
    for(int i=1; i<=10; i++)
    {
         printf("%d ", i);
         
         if(i==7)
         break;    // conditional jump
      }

#include <stdio.h>

int main()
{
  int i=1; 
  
  while(i<=10)
    {
         printf("%d ", i);
         
         if(i>5)
         break;    // conditional jump
         
          i++;
      }

jumping position of break:


     In all the control structures , when break is executed , it will jump out of the loop. it will jump to the
next statement after control structure.
    
    #include <stdio.h>

int main()
{
  int i=1; 
  
  while(i<=10)
    {
         printf("%d ", i);
         
         if(i==5)
         break;    // jump from
         
          i++;
      }
   // break will jump here
}

#include <stdio.h>

int main()
{
  int i=1; 
  
  while(i<=10)
    {
         if(i>5)
         break;    // jump from
         
         printf("%d ", i);
          i++;
      }
   // break will jump here
}
// CONTINUE
  It will continue with next iteration by skipping all the staements written below this in a loop.

#include <stdio.h>

int main()
{
  int i=1;
  
  while(i<=10)
{
    
    printf("%d ",i);
    i++;
  
  continue;
   
   printf(" hello \n");
   
   

    
}

#include <stdio.h>

int main()
{
  int i=1;
  
  while(i<=10)
{
    
    printf("%d ",i);
    i++;
  
  if (i==5)
  continue;
   
   printf(" hello \n");
   
   

    
}

#include <stdio.h>

int main()
{
  int i=0;
  
  while(i<10)
{
    i++;
    
     if (i==5)
  continue;
    
    printf("%d ",i);
    
  
 } 
    
}

#include <stdio.h>

int main()
{
  int i=0;
  
  while(i<45) // jump back here
{
    i++;
    
     if (i==13)
  continue;
    
    printf("%d ",i);
    
  
 } 
    
}

#include <stdio.h>
int main()
{
  int i=0;
  
 do  
{
    i++;
    
     if (i==13)
  continue;
    
    printf("%d ",i);
    
  } while (i<45); // jump here
    
}

#include <stdio.h>

int main()
{
  int i;
  
 for( i=0; i<45; i++) // jump back at updation
{
     if (i==13)
  continue;
    
    printf("%d ",i);
    
  }  
    
}

jumping position of continue:


     1. In while loop . it will jump back to condition.
     2. In do-while , it will jump forward to condition.
     3. In for loop, jump back to updation.
    
    
    // to print even numbers

#include <stdio.h>

int main()
{
  int i;
  
 for( i=1; i<=50; i++) 
{
     if (i%2!=0)
        continue;
    
    printf("%d ",i);
    
  }  
    
}

// to print odd numbers

#include <stdio.h>

int main()
{
  int i;
  
 for( i=1; i<=50; i++) 
{
     if (i%2==0)
        continue;
    
    printf("%d ",i);
    
  }  
    
}

goto statement:
    By using goto statement, we can jump anywhere in the code.
    1. forward jump (will skip statements)
    2. backword jump (will repeat statements)
    
    it can be conditional or unconditional jump.

#include <stdio.h>

int main()
{
    
    goto a;
  
  printf("A"); 
  
  printf("B");
  a:              //label
  printf("C");
    
}

#include <stdio.h>

int main()
{
    
    goto a;     // forward jump
  
  printf("A"); 
  a:            // label
  printf("B");

  printf("C");
    
}

#include <stdio.h>

int main()
{
    l:    // label
    
   printf(" hello ");
    
   goto l;   //backward jump
      
      // a infinite loop, due to unconditional jump
}

// WAP to print  numbers upto 100 without using any loop

#include <stdio.h>

int main()
{
    int i=1;
    
    l:    
    
   printf(" %d", i);
    i++;
    
    if(i<=100)
        goto l;   
}

// WAP to print  to find the factorial of anumber 5= 5*4*3*2*1 =120

#include <stdio.h>

int main()
{
    int fact, n;
    int ch;
    
    repeat:
    
    printf("enter a number to find factorial:");
    scanf("%d", &n);
    
    fact=1;
    for(int i=1; i<=n; i++)
    {
        fact=fact*i;
    }
    printf("The factorial of %d = %d", n, fact);
    
    printf("\n\n Do you want to check factorial of another number (1/0):");
    scanf("%d", &ch);
    
    if(ch==1)
        goto repeat;
}

// WAP to print  to find the factorial of anumber 5= 5*4*3*2*1 =120

#include <stdio.h>

int main()
{
    int fact, n;
    char ch;
    
    repeat:
    
    printf("Enter a number to find factorial:");
    scanf("%d", &n);
    
    fact=1;
    for(int i=1; i<=n; i++)
    {
        fact=fact*i;
    }
    printf("The factorial of %d = %d", n, fact);
    
    printf("\n\n Do you want to check factorial of another number (y/n):");
    fflush(stdin);  // to clean input buffer
    scanf("%c", &ch);
    
    if(ch=='y')
        goto repeat;
}

You might also like