visual studio 2010 - When using GoTo Statment in if else blocks the expected result is not obtained in C# -
i have below program.
using system; using system.collections.generic; using system.linq; using system.text; namespace mylabeledstatement { class program { static void main(string[] args) { int x = 100, y = 4; int count = 0; string[,] myarray = new string[x, y]; (int = 0; < x; i++) { (int j = 0; j < y; j++) { myarray[i, j] = (++count).tostring(); //console.writeline(myarray[i, j]); } } console.writeline("enter number find:"); int mynumber = int.parse(console.readline()); (int = 0; < x; i++) { (int j = 0; j < y; j++) { if (myarray[i, j].equals(mynumber)) { goto found; } else { goto finish; } } } found: console.writeline("the number searched {0}", mynumber); finish: console.writeline("end of search "); console.readline(); } } }
i have used goto statement in if else blocks. when execute code, not getting expected result.
if give input 1 400, output "end of search".
you should'nt use goto statement. try changing boolean variables , break statements:
class program { private static void main(string[] args) { int x = 100, y = 4; int count = 0; string[,] myarray = new string[x, y]; for(int = 0; < x; i++) { for(int j = 0; j < y; j++) { myarray[i, j] = (++count).tostring(); //console.writeline(myarray[i, j]); } } bool found = false; bool finish = false; console.writeline("enter number find:"); int mynumber = int.parse(console.readline()); for(int = 0; < x; i++) { for(int j = 0; j < y; j++) { if(myarray[i, j].equals(mynumber)) { found = true; break; } else { finish = true; break; } } } if(found) { console.writeline("the number searched {0}", mynumber); } else if(finish) { console.writeline("end of search "); } console.readline(); } }
Comments
Post a Comment