-9

Is there possible to skip a part of code when runing the program in C#? I want to run the program depends on the thecode value. If it equals to one, i will run the whole program. If it equals to two, i will skip a part of code.

if (theCode == 1 )
   //run code 1 to code 3 
if (the code == 2)
    //run code 2 to 3
if (the code == 3)
    //run code 3 only

code1(Str)
code1(Str)
code1(Str)   

code2(Str)
code2(Str)
code2(Str)

code3(Str)
code3(Str)
code3(Str)
swe
  • 1,416
  • 16
  • 26
king yau
  • 500
  • 1
  • 9
  • 28
  • try to >= in your comparison and putting // run code in {} – kenny Sep 29 '16 at 10:21
  • 1
    You can use `goto` keyword, but usage of it is not [recommended](http://stackoverflow.com/questions/11906056/goto-is-this-bad) – Jakub Jankowski Sep 29 '16 at 10:21
  • Strictly speaking, you can use `goto` and `label`, but this is widely considered as bad coding practice. – Abion47 Sep 29 '16 at 10:22
  • You could use `#if`: https://msdn.microsoft.com/en-us/library/4y6tbswk(VS.80).aspx – Krisztián Balla Sep 29 '16 at 10:22
  • please have a look at [how-to-ask](http://stackoverflow.com/help/how-to-ask). we are not here to do your homework. additionally this is a site for enthusiastic programmers... not for very beginners – swe Sep 29 '16 at 10:26
  • 2
    @swe, "additionally this is a site for enthusiastic programmers... not for very beginners" Drivel. It most definitely is a site for absolute beginners too. – David Arno Sep 29 '16 at 10:35
  • @kingyau Dear user, I am sorry for beeing somewhat not nice. But please, BEFORE you ask questions here, look at how-to-ask. There you will learn, that a question should be well researched etc. not just throw some code to your question and hope someone has time to answer. – swe Sep 29 '16 at 10:49

3 Answers3

3

As kenny says, the simplest way would be to use if blocks and compare your flag using >=.

if (theCode >= 1) Code1();
if (theCode >= 2) Code2();
if (theCode >= 3) Code3();
Abion47
  • 22,211
  • 4
  • 65
  • 88
1
function void Code1(){ //run code1 3 times }
function void Code2(){ //run code2 3 times }
function void Code3(){ //run code3 3 times }

if(theCode == 1 { Code1(); Code2(); Code3(); }
if(theCode == 2 { Code2(); Code3(); }
if(theCode == 3 { Code3(); }
Bert Persyn
  • 401
  • 4
  • 13
0

try the following code

if (theCode >= 1)
{
   Code1();
}
if (theCode >= 2)
{
   Code2();
}
if (theCode >= 3) 
{
   Code3();
}
Siby Sunny
  • 714
  • 6
  • 14