C# Jump Statements



Recently, I was going through  C# forum and I stumbled on a question.

The question goes thus: "Generate numbers from 1 to 100 without using loop statement and functions. The program can only have one entry method"?

Of course, what first came to my mind was that I will need to create a console application since I can only have one method which is the entry point.

What bothered me was that how can I generate numbers from 1..100 without using a loop?
Then jump statement came to my mind, of course, a jump statement helps to transfer control from a certain part of the program. Example of Jump statement in C# is:
  • Break
  • Continue
  • Throw
  • Return
  • Goto 
Let us take a quick look at these statements.

Break Statement

The Break Statement is used in loop or switch statement to terminate and pass program control to any statement that follows.
 
for(int i=1; i<=20;i++)
{

if(i==8)
{
break;
}

Console.WriteLine(i);

}

int a=0;
switch(a)
{
case 1:
Console.WriteLine("I am One");
break;
default:
Console.WriteLine("I am Default");
break;
}

Continue Statement

The Continue statement will pass the program control to the next iteration of a loop. 

 
for(int i=1; i<=20;i++)
{
if(i==8) continue;
Console.WriteLine(i);
}

Return Statement

The return statement terminates execution of the method and returns program control to where the method is been called.
 
static double TestMethod(int a)

{
double test=10*a;

return test;
}

static void Main()
{
int a=4;
double result=Testmethod(a);
Console.WriteLine(result);
}

Throw Statement

The Throw statement indicates the occurrence of exception during program execution. The program control will be transfer back to the calling method in which try-catch can be used to manage the exception.  

Goto Statement

The Goto statement transfer program controls directly to a label. Can be used to escape the deeply nested loop. This control is considered to be harmful and of course, too much jumping around in a program can be messy.

static int Test()
    {
        int t= 0;
        for (int i = 0; i < 11; i++)
        {
            for (int j = 0; j < 11; j++) // Run until condition.
         {
                for (int x = 0; x < 12; x++) // Run until condition.
                {
                    if (x == 5 &&
                        j == 5)
                    {
                        goto Outer
                    }
                }
                t++;
            }
        Outer:
            continue;
        }
        return t;
    }

Having gone through all these jump statements, let us now generate numbers between 1...100 without using loop nor functions.
The following code generates a number between 1 to 100 using if statement and goto statement.




 static void Main(string[] args)
        {
            int i = 1;
            repeat:
            if (i <= 100)
            {
                Console.WriteLine(i);
                i++;
                goto repeat;
            }
            Console.Read();
        }

I hope the write up has helped to re-freshened your knowledge of jump statements in C#.
Thanks for your time.

C# Jump Statements C# Jump Statements Reviewed by Akintunde Toba on June 05, 2019 Rating: 5

No comments:

Home Ads

Powered by Blogger.