swicth & case?
-
i need to convert C++ code into C#. Then the C++ in the form: number=1 switch(number) { case 1: //do something number++ case 2: //dosomething number++; case 3: if ( ) { //do something number++; } else { number= 100; } case 4: ............ case 120: //do something break; } what is the easiest way to convert these code ? thanks
-
i need to convert C++ code into C#. Then the C++ in the form: number=1 switch(number) { case 1: //do something number++ case 2: //dosomething number++; case 3: if ( ) { //do something number++; } else { number= 100; } case 4: ............ case 120: //do something break; } what is the easiest way to convert these code ? thanks
this will be same in C# but I would like to prefer
if
statement.TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
----------------------------------------------- 128 bit encrypted signature, crack if you can
-
i need to convert C++ code into C#. Then the C++ in the form: number=1 switch(number) { case 1: //do something number++ case 2: //dosomething number++; case 3: if ( ) { //do something number++; } else { number= 100; } case 4: ............ case 120: //do something break; } what is the easiest way to convert these code ? thanks
Hi, you can't fall through from one case to the next (except for empty cases), there must be an explicit change of program flow (break, return, throw, goto, ...) your C++ code looks like a sequence of operations with a selectable start. the easiest way IMO to get that done in C# is by putting everything in a for( ; ; ) {} and using a continue after each number++, plus of course providing an exit somehow. However doing so there will be a performance hit since now the switch dispatching code gets executed over and over. The ugly but high-performance alternative uses goto and labels; something like:
case 76:
label76:
...
goto label77;
case 77:
label77:
...
goto label78;This adds two lines per case instead of one, but executes the switch only once. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Hi, you can't fall through from one case to the next (except for empty cases), there must be an explicit change of program flow (break, return, throw, goto, ...) your C++ code looks like a sequence of operations with a selectable start. the easiest way IMO to get that done in C# is by putting everything in a for( ; ; ) {} and using a continue after each number++, plus of course providing an exit somehow. However doing so there will be a performance hit since now the switch dispatching code gets executed over and over. The ugly but high-performance alternative uses goto and labels; something like:
case 76:
label76:
...
goto label77;
case 77:
label77:
...
goto label78;This adds two lines per case instead of one, but executes the switch only once. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
Luc Pattyn wrote:
goto label77;
Why not
goto case 77;
? -
Hi, you can't fall through from one case to the next (except for empty cases), there must be an explicit change of program flow (break, return, throw, goto, ...) your C++ code looks like a sequence of operations with a selectable start. the easiest way IMO to get that done in C# is by putting everything in a for( ; ; ) {} and using a continue after each number++, plus of course providing an exit somehow. However doing so there will be a performance hit since now the switch dispatching code gets executed over and over. The ugly but high-performance alternative uses goto and labels; something like:
case 76:
label76:
...
goto label77;
case 77:
label77:
...
goto label78;This adds two lines per case instead of one, but executes the switch only once. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Luc Pattyn wrote:
goto label77;
Why not
goto case 77;
?PIEBALDconsult wrote:
goto case 77
O great, didn't know that. That will get me going places. Thanks. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
PIEBALDconsult wrote:
goto case 77
O great, didn't know that. That will get me going places. Thanks. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
Glad to be of service.
-
Is label needed? Can i do this way: case 1: // do something goto case 2; case 2: //do something goto case 3 ....... thanks!
Why does my skin crawl whenever I see a goto in code X|. I guess it is legitimate in this structure but I would never encourage the use of goto's, the potential for disasterous spaghetti dumps is too high.
Never underestimate the power of human stupidity RAH
-
Why does my skin crawl whenever I see a goto in code X|. I guess it is legitimate in this structure but I would never encourage the use of goto's, the potential for disasterous spaghetti dumps is too high.
Never underestimate the power of human stupidity RAH
# define proceedto goto
:-D -
# define proceedto goto
:-D -
I was merely demonstrating that (by using the C pre-processor) one could use
goto
without actually seeing thegoto
keyword. It was a joke.