C# Program to Demonstrate the Goto Statement
Posted by Superadmin on August 15 2022 07:58:33

C# Program to Demonstrate the Goto Statement

 

This is a C# Program to illustrate the concept of goto.

Problem Description

This C# Program Illustrates the Concept of Goto.

Problem Solution

Here the goto statement transfers the program control directly to a labeled statement that is Loop.

Program/Source Code

Here is source code of the C# Program to Illustrate the Concept of Goto. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Illustrate the Concept of Goto
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            int no, per, option;
            float ans;
        loop:
            Console.Write("Enter a Number :\t");
            no = Convert.ToInt32(Console.ReadLine());
            Console.Write("\nEnter Percentage Value : \t");
            per = Convert.ToInt32(Console.ReadLine());
            ans = (float)(no * per) / 100;
            Console.WriteLine("Percentage Value is:\t{0}", ans);
            Console.Write("\nCalculate again press 1.   To quit press digit:\t");
            option = Convert.ToInt32(Console.ReadLine());
            if (option == 1)
            {
                goto loop;
            }
            Console.WriteLine("Press Enter for quit");
            Console.ReadLine();
        }
    }
}
Program Explanation

In this C# program we are reading a number and percentage value using ‘no’ and ‘per’ variables respectively. The ‘ans’ variable is used to compute the multiplication of the value of ‘no’ and ‘per’ variable and divide the value by 100. To continue the process we are entering the value as 1 to option variable.

 

If condition statement is used to check the value of ‘option’ variable is equal to 1. If the condition is true then execute the statement. The goto statement is used to transfer the program control directly to a labeled statement and print the value.

Runtime Test Cases
 
Enter a number : 320
Enter Percentage Value : 10
Percentage value is : 32
Calculate again press 1. To quit Press digit: 1
Enter a number : 730
Enter Percentage Value: 10
Percentage value is: 73
Calculate again press 1. To quit press digit: 6
Press Enter for quit