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 etc. SQL allows

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you query the ma

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li