help please...Unreachable code detcted...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alireza6485
    New Member
    • Jan 2009
    • 19

    help please...Unreachable code detcted...

    Hi,
    I am getting "Unreachabl e code dected " for my cariable n.Can you please let me know how to fix the problem.Thanks
    Console.WriteLi ne();
    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(o riginal[n]);


    }
    break;




    }
    }


    }
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    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..).

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Please use [CODE] tags

      Code:
              private void Test()
              {
                  int len = 0;
                  string[] encode = {};
                  string[] original = {};
                  Console.WriteLine();
                  Console.Write("Decoded word :");
                 [HIGHLIGHT] for (int k = 0; k < len; k++)[/HIGHLIGHT]
                  {
                      for (int m = 0; m < 26; m++)
                      {
                          [B]for (int n = 0; n < 26; [U]n++[/U])[/B]
                          {
                              if (encode[m] == original[n])
                              {
                                  Console.Write(original[n]);
                              }
                             [B] break;[/B]
                          }
                      }
                  }
      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.

      Comment

      Working...