-4

I am creating an executable in visual studio.

My code goes like this:

if(condition)
    goto Step 1
else
    goto Step 2


Step 1:
code


Step 2:
code

I want to make this in a way that if Step 1 has run then Step 2 must be skipped.

Should it be done using functions?

SoftwareCarpenter
  • 3,835
  • 3
  • 25
  • 37
ABX
  • 113
  • 1
  • 4
  • 12
  • 1
    Please don't use goto unless you have a very good reason. Functions, in this case, are almost certainly better – Joel Apr 27 '13 at 20:14

1 Answers1

1

Within your class It can be placed in two functions and called from the if - else logic or you can place the code in between the if and else. If the code is large then it would be better to create two functions.

If (condition)
   call function step1
else
   call function step2

or 

if (condition)
  code...
else
  code...

C# Example of defining a method and calling it:

    public void Caller()
{
    int numA = 4;
    // Call with an int variable. 
    int productA = Square(numA);

    int numB = 32;
    // Call with another int variable. 
    int productB = Square(numB);

    // Call with an integer literal. 
    int productC = Square(12);

    // Call with an expression that evaulates to int.
    productC = Square(productA * 3);
}

int Square(int i)
{
    // Store input argument in a local variable. 
    int input = i;
    return input * input;
}
SoftwareCarpenter
  • 3,835
  • 3
  • 25
  • 37
  • I did that with your second suggestion but was unable to use functions :( It is more complex to use functions in C# than in C. – ABX Apr 27 '13 at 20:21
  • 1
    @ABX Post your actual code if you're having issues. Using functions in C# isn't really any more complex than in C. Both are quite simple (and fundamental aspects of the languages). – Kitsune Apr 27 '13 at 20:24
  • Updated answer with link and example of how to create and call a function in C# – SoftwareCarpenter Apr 27 '13 at 20:34