C# continue

continue skips a step of a loop, such as for loop, foreach loop and while loop.

using System.Windows.Forms;
int sum=0;
for (int i=0;i < 10;i++)
{
sum += i;
}
MessageBox.Show(sum.ToString()); //45
sum=0;
for (int i=0;i < 10;i++)
{
if (i == 5) continue;
sum += i;
}
MessageBox.Show(sum.ToString()); //40

Skip in foreach loop.
using System.Collections.Generic;
using System.Windows.Forms;
Dictionary<string, int> d = new Dictionary<string, int>();
d.Add("Sophia", 5);
d.Add("Mary", 14);
d.Add("Jacob", 40);
string str = "";
foreach (KeyValuePair<string, int> item in d)
{
if (item.Key == "Mary") continue;
str += item.Key + "," + item.Value + ". ";
}
MessageBox.Show(str); //Sophia,5. Jacob,40.



endmemo.com © 2024  | Terms of Use | Privacy | Home