<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Vikram Lakhotia : Goto</title><link>http://weblogs.asp.net/vikram/archive/tags/Goto/default.aspx</link><description>Tags: Goto</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>C Sharp goto statement for breaking the nested loop</title><link>http://weblogs.asp.net/vikram/archive/2009/04/06/c-sharp-goto-statement-for-breaking-the-nested-loop.aspx</link><pubDate>Mon, 06 Apr 2009 05:33:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7028776</guid><dc:creator>vik20000in</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/vikram/rsscomments.aspx?PostID=7028776</wfw:commentRss><comments>http://weblogs.asp.net/vikram/archive/2009/04/06/c-sharp-goto-statement-for-breaking-the-nested-loop.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;Hi, 
&lt;/p&gt;&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;Two days back, while discussing 
with a friend came a small interesting (programming) situation where he wanted 
to stop (break) a loop which was inside another loop. &lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;You can think of the situation as 
he was first looping through all the rows in a grid view and then in the entire 
column in the grid view. Now he was trying to look into the text of each cell, 
and if the text matched a given value, he needed to break from both the loop and 
execute the statement after the loop. &lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;His first idea was to use a flag 
which will be checked in the outer loop every time to see if the value has been 
found in the inner loop. The code would be something like below.&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;&lt;br&gt;bool flag = false;&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;for(int i = 
0;i&amp;lt;grd.Rows.count;i++)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(flag==true)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { break; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
for(int j= 0;j&amp;lt;grd.Columns.count;j++)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
if(grd.Rows[i][j].ToString() == “someValue”;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; flag = true;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
Break;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;}&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;// Statement to executed after 
loop.&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;I am sure most of us have faced 
some or other situation where we had some requirement like this and the solution 
provided was also similar.&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;The code does the work that we 
want it to do, but if you look deeply it has a problem. I have to check the 
value of the flag for each time there is a loop for the row and also to maintain 
a tedious flag for a just breaking the loop. &lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;The same work can be easily done 
without using a flag, by using a goto statement like this.&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;for(int i = 
0;i&amp;lt;grd.Rows.count;i++)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int j= 
0;j&amp;lt;grd.Columns.count;j++)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(grd.Rows[i][j].ToString() 
== “someValue”;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; goto foundcell;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
}&lt;br&gt;}&lt;br&gt;foundcell:&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;&amp;nbsp;&amp;nbsp; // Statement to executed after 
loop.&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;This way, I could break from both 
(if required even more than 2 loops) the loops easily without maintaining any 
flag with the help of a goto statement.&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;Remember the goto statement 
should only be used when we have nested loops. If you have only one loop than 
its best to use the break keyword to stop, break the loop.&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in 0in 0pt;" class="MsoNormal"&gt;Vikram&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7028776" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/vikram/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/vikram/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/vikram/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/vikram/archive/tags/How+To/default.aspx">How To</category><category domain="http://weblogs.asp.net/vikram/archive/tags/Goto/default.aspx">Goto</category></item></channel></rss>