C#.Net find output programs (goto) | set 1

Find the output of C#.Net programs | goto | Set 1: Enhance the knowledge of C#.Net goto concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 06, 2021

Question 1:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int X = 5;
            int I = 1;

        XYZ:
            Console.WriteLine(X * I);
        I++;
        if (I <= 10)
            goto XYZ;
        }
    }
}

Output:

5
10
15
20
25
30
35
40
45
50
Press any key to continue . . .

Explanation:

In the above program, we created two variables X and I that are initialized with 5 and 1 respectively. Here, we created a label XYZ, and we used the "goto" statement to print the table of 5.

The table of 5 will be printed in multiple iterations.

Iteration1:

X =5, I=1

Then X*I that is 5*1 = 5 will print and increase the value of I by one that is 2.

Here condition will be true, then control of the program transferred to the label "XYZ".

Iteration2:

X =5, I=2

Then X*I that is 5*2 = 10 will print and increase the value of I by one that is 3.

Here condition will be true, and then control of the program transferred to the label "XYZ".

Iteration3:

X =5, I=3

Then X*I that is 5*3 = 15 will print and increase the value of I by one that is 4.

Here condition will be true, and then control of the program transferred to the label "XYZ".

Like that when the value of I will 11, and the condition gets false and the loop will be terminated.

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            goto X;
            Y:
                Console.WriteLine("HELLO");
                goto OUT;
            X:
                Console.WriteLine("INDIA");
                goto Y;
            OUT:
                Console.WriteLine("BYE");
        }
    }
}

Output:

INDIA
HELLO
BYE
Press any key to continue . . .

Explanation:

In the above program, we created three labels (X, Y, OUT) for control transfer. Here, "goto X" will transfer the control to the label "X" and print "INDIA" on the console screen. then "goto Y" will transfer control to the label "Y" and print "HELLO" and then "goto OUT" will transfer the control to the label "OUT" and print "BYE" on the console screen.

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            goto X;
            Y:
                Console.WriteLine("HELLO");
                goto OUT;
            X:
                Console.WriteLine("INDIA");
                goto Y;
            OUT:
        }
    }
}

Output:

main.cs(18,8): error CS1525: Unexpected symbol `}'

Explanation:

The above program will generate syntax error because we need to write at least one statement after a defined label in C#. Here, we did not write any statement after OUT: label.

Question 4:

using System;

namespace Demo
{
    class Program
    {
        static void SayHello()
        {
        ABC:
            Console.WriteLine("Hello");
        }
        //Entry point of the program
        static void Main(string[] args)
        {
            goto X;
            X:
                Console.WriteLine("INDIA");
                goto ABC;
        }
    }
}

Output:

main.cs(9,9): warning CS0164: This label has not been referenced
main.cs(18,17): error CS0159: The label `ABC:' could not be found 
within the scope of the goto statement

Explanation:

The above program will generate a syntax error and a warning because we cannot transfer program control from one method to another method using a "goto" statement.

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int I = 0;

        XYZ:
            Console.Write(++I * I+" ");
        if (I <= 5)
            goto XYZ;

        }
    }
} 

Output:

1 4 9 16 25 36 
Press any key to continue . . .

Explanation:

In the above program, we created a variable I, which is initialized with 0, and we defined a label XYZ.

Console.Write(++I * I+" ");

The above statement will increase the value of the variable I and multiply by itself. The process will repeat until the value of I will be greater than 5.

Iteration1:

I=0;

++I then I will be 1.

I*I, it will print 1*1 = 1, and then the condition will true and control of the program will move to the label XYZ.

Iteration2:

I=1;

++I then I will be 2.

I*I, it will print 2*2 = 4, and then the condition will true and control of the program will move to the label XYZ.

Iteration3:

I=2;

++I then I will be 3.

I*I, it will print 3*3 = 9, and then the condition will true and control of the program will move to the label XYZ.

Iteration4:

I=3;

++I then I will be 4.

I*I, it will print 4*4 = 16, and then the condition will true and control of the program will move to the label XYZ.

Iteration5:

I=4;

++I then I will be 5.

I*I, it will print 5*5 = 25, and then the condition will true and control of the program will move to the label XYZ.

Iteration6:

I=5;

++I then I will be 6.

I*I, it will print 6*6 = 36, and then the condition will false and the program gets terminated.

Question 6:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int i = 0;
	     char ch='0';
            string STR="ABCD";
        XYZ:
            ch = STR[i++];
            Console.Write((ch+32)+" ");

        if (i < STR.Length)
            goto XYZ;
        }
    }
}

Output:

97 98 99 100 
Press any key to continue . . .

Explanation:

In the above program, we created three variables, i , ch, and STR initialized with 0, '0', and "ABCD" respectively. Then we defined a label "XYZ", then extract characters from string one by using the index and add 32 to each character and print on the console screen.

We know that the ASCII value of character 'A' is 65 and we added 32 then it becomes 97, like that.

='A' + 32;
= 65 +32;
= 97.

Similarly, it will print "97 98 99 100" on the console screen.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.