Monday, March 30, 2015

Is there a way of specifying which block or loop to break out of when working with nested loops?

Question:Is there a way of specifying which block or loop to break out of when working with nested loops??

Answer: </P><P align=justify>The easiest way is to use goto: using System;</P><P align=justify>class BreakExample </P><P align=justify>{</P><P align=justify>public static void Main(String[] args) </P><P align=justify>{</P><P align=justify>for(int i=0; i&lt;3; i++) </P><P align=justify>{</P><P align=justify>Console.WriteLine("Pass {0}: ", i);</P><P align=justify>for( int j=0 ; j&lt;100 ; j++ ) </P><P align=justify>{</P><P align=justify>if ( j == 10) goto done;</P><P align=justify>Console.WriteLine("{0} ", j);</P><P align=justify>}</P><P align=justify>Console.WriteLine("This will not print");</P><P align=justify>}</P><P align=justify>done:</P><P align=justify>Console.WriteLine("Loops complete.");</P><P align=justify>}</P><P align=justify>}

Tags:Interview Questions, Microsoft, C#,

Please submit your Interview Answer for the above Question via replying to this post. Thank you for visiting to our blog, Have a Good Day!.



Sponsor Advertisement



Customer Relationship Management Software

Customer Resource Management Software

CRM software is designed to help businesses in manageing their customer data and customer interaction, access business information, automate sales, marketing and customer support and also manage employee, vendor and partner relationships. ...

No comments:

Post a Comment