Jumping Statement(break,continue,goto,exit() , return)

                                                      Jumping Statement
  • break
  • continue
  • goto
  • exit()
  • return()
Break:-The break statement breaks the execution of the current statement block and executes the first line that follow the block.We use break statement to terminate the loop once the condition gets satisfied.It can be used to end an infinite loop or force to end before its natural end.

Syntax:-

if(boolean condition)
{
;//statements;
break; 
} 

Example:

#include <stdio.h>
int main() {
   char c;
   for(;;)
{
      printf( "\nPress any key, q to quit: " );

      // Convert to character value
      scanf("%c", &c);
      if (c == 'q')
          break;
   }
} // Loop exits only when 'Q' is pressed
Continue:-The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block has reached, causing to jump to the start of the following iteration .
Syntax:-
 
continue;
 

Example: 

#include <stdio.h>
int main()
{
    int i = 0;
    do
    {
        i++;
        printf("before the continue\n");
        continue;
        printf("after the continue, should never print\n");
     } while (i < 3);

     printf("after the do loop\n");
getch();
} 

Goto:-Goto statement allows to make an absolute jump to another point in the program. This feature should be used with caution since its execution causes an unconditional jump ignoring any type of nesting limitation. The destination point is identify by label which is then used as an argument for the goto statement. A label is made of valid identifier followed by a colon (:) 

Syntax:-
//statement:

//jump-statement:
        goto identifier;
//labeled-statement:
        identifier:statement; 

Example: 

#include <stdio.h>
int main()
{
    int i = 0;
    do
    {
        i++;
        printf("before the continue\n");
        continue;
        printf("after the continue, should never print\n");
     } while (i < 3);

     printf("after the do loop\n");
getch();
} 


exit() :-It is used to exit the program as a whole. In other words it returns control to the operating system.After exit () all memory and temporary storage areas are all flushed out and control goes out of program.exit () statement is placed as the last statement in a program since after this program is totally exited.


Syntax:-
exit(0);

Example:

#include<stdio.h>
#include<stdlib.h>
void f(){
printf("executing f\n");
exit(0);
}
void main(){
f();\\f() function calling in main() method
printf("back from f\n");
getch();
} 

Output:
Executing f


You never get "Back from f". Also notice the #include <stdlib.h> necessary to call the library function exit().Also notice that the parameter of exit() is an integer (it's the return status of the process that the launcher process can get; the conventional usage is 0 for success or any other value for an error).

return() :-It is used to return from a function and return control to the calling function.This statement can take its presence anywhere in the function. It need not be presented as the last statement of the function.If the function was declared with return type void, a return statement containing an expression generates a warning and the expression is not evaluated.

Syntax:-
 
return;//if u r returning a variable then
return(a); 

Example:

#include<stdio.h>
void f(){
printf("executing f\n");
return;
}
void main(){
f();\\f() function calling in main() method
printf("back from f\n");
getch();
}

Output:
Executing f
Back from f

Comments

Popular posts from this blog