Exit from for loop

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Exit from for loop

 
-1
  #1
Jul 17th, 2009
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())
                 //   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.
                {
                    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; 
             }
        }
       }
Last edited by peter_budo; Jul 18th, 2009 at 12:50 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 446
Reputation: Ramesh S will become famous soon enough Ramesh S will become famous soon enough 
Solved Threads: 82
Ramesh S Ramesh S is offline Offline
Posting Pro in Training

Re: Exit from for loop

 
0
  #2
Jul 18th, 2009
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.

  1. protected void btnSellProduct_Click(object sender, EventArgs e)
  2. {
  3. string sPath = Server.MapPath("~/");
  4. string sFileName = sPath + "item.txt";
  5. string[] sArr;
  6. string[] sArr2;
  7. int lCount;
  8. int lCurrentStock;
  9. int lSellingStock;
  10.  
  11. if (File.Exists(sFileName.Trim()))
  12. {
  13. sArr = System.IO.File.ReadAllLines(sFileName);
  14. for (lCount = 0; lCount <= sArr.Length - 1; lCount++)
  15. {
  16. sArr2 = sArr[lCount].Split(new string[] { "^^^^" }, StringSplitOptions.None);
  17.  
  18. if (sArr2[0].Trim() == txtSellProdID.Text.Trim())
  19. {
  20. lCurrentStock = Convert.ToInt32(sArr2[3]);
  21. lSellingStock = Convert.ToInt32(txtQtyToSold.Text);
  22. if (lSellingStock > lCurrentStock)
  23. lblStatus.Text = "Could not Sell this Product.Out of Stock";
  24. else
  25. {
  26. WriteToTransactionFile();
  27. DecrementFromItemFile();
  28. lblStatus.Text = "Transaction Processed Successfully";
  29. }
  30. break;
  31. }
  32.  
  33. }
  34. }
  35.  
  36. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,703
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 483
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Exit from for loop

 
0
  #3
Jul 18th, 2009
break; - no need to post source code.
sonia sardana - read this,
http://catb.org/esr/faqs/smart-questions.html
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Re: Exit from for loop

 
0
  #4
Jul 18th, 2009
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...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC