Skip to main content

GO to Statement in C #DOTC_Mdb

Go to Statement in C.

Hi Everyone,

Today we will discuss about go to statement in C which is rarely used in Program but sometimes we need to use this to jump statement in C. Before going through this blog please go through my previous C blogs for your reference so that you can easy to understand the program . You will find the blogs in archive folder from below blog:

     dheeraj60.blogspot.com

Let’s start with go to statement in C.

goto statement in C is known as jump statement in C.  Go to statement is used to transfer the program control to a predefined . go to  also be used to break the multiple loops which can't be done by using a single break statement.  Go to statement is rarely used in program because it makes program confusing  and complex as the control of the program difficult to trace, so for testing it’s bit confusing.

Defining go to we need to put label name and can use it in program

Syntax of go to statement:

label:  

// part of code;  

goto label; 

Let’s understand go to statement through one program table where we can pass input from user and take the output by using go to statement.

Below is the Program :

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

 int tab,i=1;

  printf(" Please enter the number for table\n");

  scanf("%d",&tab);

  table:

  printf("%d x %d = %d\n",tab,i,tab*i);

  i++;

  if(i<=10)

  goto table;

    return 0;

}

 

Here table is label and when the value of condition (i<=10) then statements jumps to label table and it will run till 10 times and give the output pass by the user.

Below is the output of above program in compiler code block.

 

 

So in above program we can see how go to statement works and through label table which we write in program it's directly jump to condition and give the output against the parameter passed by the user.


Let’s take another example of addition of number by using go to statement in this program. In this program we use the same as above program logic , After this program we will see how go to statements works in multiple loops.

 

Below is the Program .

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

 int add=0;

   for(int i = 0; i<=10; i++){

            add = add+i;

            if(i==10){

               goto addition;

            }

   }

 

   addition:

   printf("%d \n", add);

    return 0;

}

 

In this example, we used label addition and when the value of i (inside loop) is equal to 10 then we are jumping to go to addition label. This is reason the sum is displaying the sum of numbers till 10 even the loop is set to run from 0 to 10.

and after each iteration of loop add=add+i; so here we have given condition if i equals to 10 so output will be 55 and if we give the condition i equals to 5 it will give 15 you can check with other number also.

 

Below is the output of above program in Code Block Compiler.


Hope you are able to understand go to statement. Now go to statement we can say is good when we use multiple loops in a program as go to break the multiple loops using a single statement at the same time.

Let’s  see the below program where we use multiple loops with go to statement.


#include <stdio.h>

#include <stdlib.h>

int main()

{

 int i, j, k;

for (i=0; i<10; i++)

{

 

    for(j=0;j<4;j++)

    {

      for(k=0;k<4;k++)

      {

        printf("%d %d %d\n",i,j,k);

        if(j == 2)

        {

          goto terminate;

        }

      }

    }

  }

  terminate:

  printf("terminate from the loop");

    return 0;

}

 

Here we can see how go to is useful as we use multiple loops in the program and mark label here terminate and through condition we can come out from the loops as per condition , we can give any condition as per requirement and it’s work better than break statement in this case .

Below is the output of above program in compiler code block for your reference.


Guys Hope you are able to understand go to statement in C , Please go through this blog and write as much program as you can as per your requirement and let me know if you have any doubt and also go through all my previous blogs in C for better understanding.

In next blog I will come up with uses of String in C program and some useful programs in C before i am going to start function and Pointer in C program.

 

Thanks.


Comments

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Basic SQL Commands in Oracle Database #Diksha Oracle Training Centre

  Basic SQL Commands in Oracle Database.   Hello Everyone, In my previous blog I discussed about SQL and Classification of SQL . Today I will discuss about SQL basic commands which widely used in RDBMS. The Topics of SQL command which I am going to cover in this blog are mainly divided into four Categories: ·         DDL:   DDL consists of commands which are used to define and design the database. ·         DML:   DML consists of commands which are mainly used to manipulate the data in the database. ·        DCL: DCL Consists of commands which deal with the user permissions/access and controls in the database. ·       TCL:   TCL Consist of commands which deal with the transaction in the database. If you want to explore theory part please follow my below blog as in today blog I will discuss about how to use the commands. h...

Pseudo Column in Oracle #Diksha Oracle Training Centre

  Pseudo Column in Oracle. Hello Everyone, Today I will discuss about Pseudo column in Oracle. This topic is very important so before going to read this blog, I suggest you to please see my previous blogs which is in archive folder for your reference so that it’s easy for you to understand this topic. Let’s discuss about Pseudo Column in Oracle. Pseudo column: A pseudo-column is dynamic in Oracle as it behaves like a table column but is not stored in the table. Pseudo column behaves like a table column, but is not actually stored in the table. You can select from pseudo columns, but you cannot insert, update, or delete their values. A pseudo column is also similar to a function without arguments. In   Simple word we can say Pseudo column is a column that yields a value when selected, but which is not an actual column of the table. An example is RowID or SysDate. It can be use in combination with the DUAL table. Below are the Pseudo Column commonly used in Oracle Dat...