Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Thursday 10 March 2022

26.Goto

 

goto:

It is an unconditional control structure inherited from the first high level language BASIC.

It is used to send the program control from one part of the program to another part.

The name of location is specified with the goto statement.

The name of a location is defined with an identifier terminating with colon (:)

It is used as a jumping/skipping and iterative control structure.

 


 

 

 

 

 

 

 

 

 

 

 

 

Example:

<pre><code>

#include&lt;stdio.h&gt;

void main()

{

 

printf("One");

printf("\nTwo");

goto abc;

printf("\nThree");

printf("\nFour");

abc:

printf("\nFive");

 

}

 </code></pre>

Output:

Two

Five

 

 

 

 

 

 

 

 

Example:

<pre><code>

#include&lt;stdio.h&gt;

void main()

{

 

nice:

  printf("\nHello World");

goto nice;

 

}

 </code></pre>

Output:

Hello World

Hello World

--------------

--------------

 

Example explained:

In the above example printf("Hello World"); executes repeatedly called loop.

It happens continuously until the program is terminated, so it is also called unlimited or infinite loop.

 

Note:

Press ctrl + break to terminate the program execution.

 

Example:

<pre><code>

#include&lt;stdio.h&gt;

void main()

{

int x;

 

x=1;

abc:

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

   x=x+1;

goto abc;

 

}

 </code></pre>

Output:

1

2

3

.

.

Note: Press ctrl+break to terminate the program execution

Example explained:

printf("%d",x); is repeatedly executing and incrementing one by one.

The output is printing natural numbers continuously.

Example:

<pre><code>

#include&lt;stdio.h&gt;

void main()

{

int x;

 

x=1;

abc:

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

   x=x+1;

if(x<=5)

   goto abc; 

 

}

</code></pre>

Output:

1

2

3

4

5

Example explained:

The goto abc; executes until "x" becomes 5. When "x" becomes 6 then goto abc; skips from the execution results termination of program.

Here loop is executing for limited number of times so it is called limited loop.

 

Specification1:

Print 10 to 15 natural numbers.

 

Program:

<pre><code>

#include&lt;stdio.h&gt;

void main()

{

int x;

 

x=10;

nice:

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

   x=x+1;

 

if(x<=15)

   goto nice;

 

}

</code></pre>

Output:

10

11

12

13

14

15

 

Specification2:

Print 21 to 15 that are in reverse order. (21   20   19   18   17   16   15)

 

Program:

<pre><code>

#include&lt;stdio.h&gt;

void main()

{

int i;

 

i=21;

xyz:

   printf("%10d",i);

   i=i-1;

if(i>=15)

   goto xyz;

 

}

 </code></pre>

Output:

21        20        19        18        17        16        15

 

Example explained:

Here the variable "i" is used to build the iteration, so is called iteration variable.

%10d is to print the terms with in 10 width. It provides horizontal spacing among the terms.

 

Specification3:

Print the natural numbers from 1 to the specified limit.

 

Program:

<pre><code>

#include&lt;stdio.h&gt;

void main()

{

int n,i;

 

printf("Enter the limit:");

scanf("%d",&n);

i=1;

xyz:                

   printf("%10d",i);

   i=i+1;

if(i<=n)

  goto xyz;

 

}

 </code></pre>

Execution1:

Enter the limit: 5

1          2          3          4          5

 

Execution2:

Enter the limit: 3

1          2          3

 

Specification4:

Print the natural numbers with in the range.

 

Program:

<pre><code>

#include&lt;stdio.h&gt;

void main()

{

int i,ll,ul;

 

printf("Enter the lower limit:");

scanf("%d",&ll);

printf("Enter the upper limit:");

scanf("%d",&ul);

i=ll;

pqr:

    printf("%10d",i);

    i++;

if(i<=ul)

   goto pqr;

 

}

</code></pre>

Execution1:

Enter the lower limit: 5

Enter the upper limit: 10

5          6          7          8          9          10

Execution2:

Enter the lower limit: -5

Enter the upper limit: -1

-5         -4         -3         -2         -1

0 comments:

Post a Comment