By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

help please...Unreachable code detcted...

P: 19
Hi,
I am getting "Unreachable code dected " for my cariable n.Can you please let me know how to fix the problem.Thanks
Console.WriteLine();
Console.Write("Decoded word :");
for (int k = 0; k <len; k++)
{

for (int m = 0; m < 26; m++)
{
for (int n = 0; n < 26;n++ )
{
if (encode[m] == original[n])
{

Console.Write(original[n]);


}
break;




}
}


}
Feb 12 '09 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 750
PRR
You need to explain more ... I cant understand what exactly you are trying to do...
Unreachable code basically means code which wont get executed at all... (for given all condition a part of code may not at all get executed..).
Feb 12 '09 #2

tlhintoq
Expert 2.5K+
P: 3,525
Expand|Select|Wrap|Line Numbers
  1.         private void Test()
  2.         {
  3.             int len = 0;
  4.             string[] encode = {};
  5.             string[] original = {};
  6.             Console.WriteLine();
  7.             Console.Write("Decoded word :");
  8.             for (int k = 0; k < len; k++)
  9.             {
  10.                 for (int m = 0; m < 26; m++)
  11.                 {
  12.                     for (int n = 0; n < 26; n++)
  13.                     {
  14.                         if (encode[m] == original[n])
  15.                         {
  16.                             Console.Write(original[n]);
  17.                         }
  18.                         break;
  19.                     }
  20.                 }
  21.             }
  22.  
I had to make a couple assumptions about variables len, encode[] and original[].
(lines 3,4,5)

This is a good example of walking through code line-by-line to find a problem. My suggestion for how to find the problem would be to place a breakpoint at the beginning of the first 'for' loop at line 8. Then use [F10] to walk through the code line by line.

Execution goes like this...
LIne 8
Line 10 - M = 0
Line 12 - n = 0
Line 14 - do comparrison
Line 16 - Maybe write to console depending on result of line 14
Line 18 - BREAK out of loop n
Line 10 - Increment m, now m = 1
[...]


You will discover that your break statement on line 18 is reached the first time your most interior loop ('n') is executed. Since the loop is escaped during its first run, it never executes the n++ in line 12, thus n++ is unreachable. It also means this loop never actually loops: It executes one time only for each 'm' loop.

TIP: pay attention when Visual Studio brings something to your attention. When you typed 'break;' in line 18 it would have made that line bold along with line 12, letting you know which line the break statement applied to. This would have alerted you to what would happen when this code was executed.
Feb 13 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.