954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Exit from for loop

I want to just exit from the loop, I m facing the probs,see bolded part...

protected void btnSellProduct_Click(object sender, EventArgs e)
    {

        string sPath = Server.MapPath("~/");
        string sFileName = sPath + "item.txt";
        string[] sArr;
        string[] sArr2;
        int lCount;
        int lCurrentStock;
        int lSellingStock;
        

        if (File.Exists(sFileName.Trim()) == true)
        {
            sArr = System.IO.File.ReadAllLines(sFileName);
            for (lCount =0;lCount <= sArr.Length -1; lCount ++)
            {
                sArr2 = sArr[lCount].Split(new string[] { "^^^^" }, StringSplitOptions.None);

                if (sArr2[0].Trim() == txtSellProdID.Text.Trim())
               <strong>  //   Once Control go inside this condition,i want to exit from the for loop. For Dat  
                 //       i Write break,but 
                 //for (lCount =0;lCount <= sArr.Length -1; lCount ++)
                 //    in the above line , lCount ++ is underline - Unreachable code detected.</strong>
                {
                    lCurrentStock = Convert.ToInt32(sArr2[3]);
                    lSellingStock = Convert.ToInt32(txtQtyToSold.Text);
                    if (lSellingStock > lCurrentStock)
                        lblStatus.Text = "Could not Sell this Product.Out of Stock";
                     else
                    {
                        WriteToTransactionFile();
                        DecrementFromItemFile();
                        lblStatus.Text = "Transaction Processed Successfully";
                    }                   
                }
                break; 
             }
        }
       }
sonia sardana
Posting Whiz
326 posts since Mar 2008
Reputation Points: 0
Solved Threads: 8
 

You have put the break statement after closing the 'if' block and before the closing braces of 'for' loop.

So the' for' loop will be executed only once and the counter lCount++ will never be executed. Therefore it displays 'Unreachable code detected' error.

You should put the break statement inside the 'if' block.

I have changed your code.

protected void btnSellProduct_Click(object sender, EventArgs e)
    {
        string sPath = Server.MapPath("~/");
        string sFileName = sPath + "item.txt";
        string[] sArr;
        string[] sArr2;
        int lCount;
        int lCurrentStock;
        int lSellingStock;
   
        if (File.Exists(sFileName.Trim()))
        {
            sArr = System.IO.File.ReadAllLines(sFileName);
            for (lCount = 0; lCount <= sArr.Length - 1; lCount++)
            {
                sArr2 = sArr[lCount].Split(new string[] { "^^^^" }, StringSplitOptions.None);

                if (sArr2[0].Trim() == txtSellProdID.Text.Trim())
                {
                    lCurrentStock = Convert.ToInt32(sArr2[3]);
                    lSellingStock = Convert.ToInt32(txtQtyToSold.Text);
                    if (lSellingStock > lCurrentStock)
                        lblStatus.Text = "Could not Sell this Product.Out of Stock";
                    else
                    {
                        WriteToTransactionFile();
                        DecrementFromItemFile();
                        lblStatus.Text = "Transaction Processed Successfully";
                    }
                    break;
                }

            }
        }

    }
Ramesh S
Posting Pro
583 posts since Jun 2009
Reputation Points: 165
Solved Threads: 113
 

break; - no need to post source code.
sonia sardana - read this,
http://catb.org/esr/faqs/smart-questions.html

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

hi ramesh thx very much,its working,hi adatapost, when i didnt put the code,i got the answers put the code,when i start putting it,i got not to put it...

sonia sardana
Posting Whiz
326 posts since Mar 2008
Reputation Points: 0
Solved Threads: 8
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You