C : Jump Statements
Tags: cpp, programming, tutorial
Published: 2021-03-15 18:06:21

1. What are jump statements?
2. Types (Break, Continue, GoTo, Return)

****
1.**What are jump statements :-** Jump statements are used to interrupt the normal flow of program. They makes the control jump to another section of the program when encountered. It is usually used to terminate the loop or switch-case instantly. It is also used to escape the execution of a section of the program.

2.**Break :**
->Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
```
break;
```
->The break statement is also used with if...else statement inside the loop.
```
#include <stdio.h>
int main() 
{
int num = 5;
while (num > 0) 
  {
  if (num == 3)
    break;
  printf("%d\n", num);
  num--;
}
}
```
3. **Continue :** The continue statement skips the current iteration of the loop and continues with the next iteration. Its syntax is:
```
continue;
```
->The continue statement is almost always used with the if...else statement.

```
#include <stdio.h>
int main() 
{
int nb = 7;
while (nb > 0) 
{
  nb--;
  if (nb == 5)
    continue;
 printf("%d\n", nb);
}
}
```
4. **Go to:**
->Transfers control to the labeled statement.
->The label is an identifier. When the goto statement is encountered, the control of the program jumps to label: and starts executing the code.
->Here label can be any plain text except C keyword and it can be set anywhere in the C program above or below to goto statement.

```
goto label;
... .. ...
... .. ...
label: 
statement;
```
```
/*Example:*/
#include <stdio.h>
 
int main () {

   int a = 10;
   LOOP:do 
       {
         if( a == 15) 
         {
             a = a + 1;
             goto LOOP;
         }
         printf("value of a: %d\n", a);
         a++;
       }while( a < 20 );
   return 0;
}
```

**NOTE :** Use of the goto statement is highly discouraged in any programming language because it makes it difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten to avoid them.

5. **Return :**
->usually used at the end of a function to end or terminate it with or without a value. 
->It takes the control from the calling function back to the main function(main function itself can also have a return).
->return can only be used in functions that is declared with a return type such as int, float, double, char, etc.
->The functions declared with void type does not return any value. Also, the function returns the value that belongs to the same data type as it is declared. 

```
return [value/variable];
```
```
/*Example:*/
#include <stdio.h>
char func(int ascii) 
{
  return ((char) ascii);
}
int main() 
{
  int ascii;
  char ch;
  printf("Enter any ascii value in decimal: \n");
  scanf("%d", & ascii);
  ch = func(ascii);
  printf("The character is : %c", ch);
  return 0;
}
```
->In this program, we have two functions that have a return type but only one function is returning a value[func()] and the other is just used to terminate the function[main()].

The function func() is returning the character value of the given number(here 110). We also see that the return type of func() is char because it is returning a character value.

The return in main() function returns zero because it is necessary to have a return value here because main has been given the return type int.