Skip to main content

Jump Statement used in Computer Programming, C Language

After discussing about looping statements in computer programming, programmer should know about jump statements. These statements are used to jump forward/backward, on a given condition. This article will give you a brief overview in c language.

Sometimes, during the execution of a loop it may be desirable to skip execution of some statements or leave the loop when a certain condition occur. For example, during searching process, if the desired item found then there is no need to search further. In such cases, we have to terminate the loop using jump. The various jumps in loops are as follows:


Jump Statement used in Computer Programming, C Language

goto statement

goto statement is a jump statement that transfers the control of the specified statement in a program. This is an unconditional branch statement. The specified statement is identified by a symbolic name (any identifier) ending with colon ':'.
Syntax:
    goto label:
Where
  • goto – is a reserve word or keyword
  • label – is an identifier ending with colon ‘:’
Disadvantages:
  • Using goto statement in a program is an unstructured programming style.
  • The unstructured program written using goto statement are very difficult to read and understand.
  • It is also very difficult to debug the program.
Example: A C program to add all the natural numbers using goto statement.
main()
{
  int n,i,sum;
  clrscr();
  printf(“Enter the number od terms:”);
  scanf(“%d”,&n);
  sum=i=0;
  top:    sum=sum+i;
    i=i+1;
    if(i<=n) goto top;
    printf(“Sum of series = %d”,sum);
  getch();
}


Jump Statement used in Computer Programming, C Language output

Break Statement

The break statement is another jump statement which is frequently used in switch statement and loops. The break statement works as follows:
  • The 'break' statement is part of switch statement causes control to exit the switch statement. Usually, in any case, the break statement will be the last statement.
  • If break is executed in any type of loop like for, while or do-while, the control comes out of the loop and the statement following the loop will be executed. The break statement is used mainly to terminate the loop when a specific condition is reached.
Note: If ‘break’ appears in the inner loop of a nested loop, control only comes out of the inner loop.
Consider the following program segment:
    i = 1;
    for(; ; )
    {
      if(i==5) break;
      printf(“%d”,i++);
    }
In the given for loop, there is no initialization, condition and re-initialization part. Such loop is called as infinite loop. Even though it appears as an infinite loop, as the value of i reaches to 5, the control immediately comes out of the loop. When the value of i is compared with 5, the break statement is executed and the control immediately comes out of the loop.

Continue statement:

The continue statement is used only in the loops to terminate the current iteration. During execution of a loop, it may be necessary to skip a part of the loop based on some condition e.g. during processing of student records, it may be necessary to exclude student names whose marks are less than or equal to 35. In such case, a test is made to see whether the marks scored are less than or equal to 35. If so, the part of the program that processes the student details can be skipped using continue. 

But, the execution continues with next iteration of the loop. In the while statement and do-while statement, whenever a continue statement is executed, the rest of the statements within the body of the loop are skipped and the conditional expression (conditional statement) is executed. But, in for loop, the updating statement will be executed. For example, consider the statements shown below:
    for(i=1;i<=5;i++)
    {
      if(i==2) continue;
      printf(“%d”,i);
    }
Note that when i takes the value 2, the continue statement is executed and the statements following continue are skipped. The control is transferred to updating i.e. i++ statement and the execution continues from that point. The output of this program is 1 3 4 5.

Difference between break and continue statement


Difference between jump statements in computer programming

Array in Computer Programming

Comments

Popular posts from this blog

difference between structure and union in C Language

In c language article we will see the difference between union and structure. Both are the user define datatype in c language. See the table which is mentioned below: ASP.NET Video Tutorial Series Structure Union1.The keywordstruct is used to define a structure 1. The keyword union is used to define a union. 2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum ofsizes of its members. The smaller members may end with unused slack bytes. 2. When a variable is associated with a union, thecompiler allocates thememory by considering the size of the largest memory. So, size of union is equal to the size of largest member. 3. Each member within a structure is assigned unique storage area of location. 3. Memory allocated is shared by individual members of union. 4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset v…

Difference between Linear search and Binary Search in c language

SQL Video Channel : Download all SQL Video



Binary Search Linear Search Works only on sorted items. such as  1,2,3,4,5,6  etc
Works on sorted as well as unsorted items. 12,4,5,3,2,1 etc Very efficient if the items are sorted Very efficient if the items are less and present in the beginning of the list. such as Suppose your list items are : 12,3,4,5,1 and you want to search 12 number then you get beginning in the list. Works well with arrays and not on linked lists. Works with arrays and linked lists.
Number of comparisons are less More number of comparisons are required if the items are present in the later part of the array or its elements are more.

Memory representation of Linked List Data Structures in C Language

Memory representation of Linked List

             In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.

               Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:


      Suppose next node is allocated at an address 506, so the list becomes,



  Suppose next node is allocated with an address with an address 10,s the list become,


The other way to represent the linked list is as shown below:




 In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of next node. The last node …