if
if (Condition1)
{
Enter the loop processing;
}
else if (Condition2)
{
Enter the loop processing;
}
else
{
if (Condition3)
{
Enter the loop processing;
}
} |
switch
switch (Formula)
{
case Condition1:
Enter the loop processing;
break;
case Condition2:
Enter the loop processing;
break;
default:
Enter the loop processing;
break;
} |
for
for (Variable; LoopCondition; Processing)
{
Enter the loop processing;
} |
foreach
foreach (DataType Variable in Collction)
{
Enter the loop processing;
} |
while
while (LoopCondition)
{
Enter the loop processing;
} |
do-while
do
{
Enter the loop processing;
}
while (LoopCondition); |
break
1. Break the loop midway. |
2. Use break when nesting. |
continue
1. Skip processing in loop. |
2. Use continue when nesting. |
|
|
if - example
int iNum = 5;
if (iNum == 0)
{
Console.WriteLine("0");
}
else if (iNum == 1)
{
Console.WriteLine("1");
}
else
{
if (iNum < 10)
{
Console.WriteLine(iNum);
}
} |
switch - example
int iColor= 2;
switch (iColor)
{
case "1":
Console.WriteLine("White");
break;
case "2":
Console.WriteLine("Blue");
break;
default:
Console.WriteLine("Black");
break;
} |
for - example
for (int iNum = 0; iNum < 3; iNum++)
{
Console.Write(iNum);
} |
foreach - example
var color = new[] { "White", "Blue", "Black" };
foreach (string strColor in color)
{
Console.Write(strColor);
} |
Result : WhiteBlueBlack
while - example
int i = 0;
while (i < 5)
{
Console.Write(i + ",");
i++;
} |
break - example
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
if (j == 0)
{
break;
}
Console.WriteLine("j =" + j);
}
Console.WriteLine("i =" + i);
} |
Only the inner for statement is removed.
do-while - example
int i = 0;
do
{
Console.Write(i + ",");
i++;
}
while (i < 5); |
break - example - do-while
int i = 0;
do
{
if (i == 3)
{
break;
}
Console.Write(i + ",");
i++;
}
while (true); |
The condition is true.
Warning: Without break, it becomes an infinite loop.
continue - example
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
if (j == 0)
{
continue;
}
Console.WriteLine("j =" + j);
}
Console.WriteLine("i =" + i);
} |
Return to the declaration line of the inner for statement.
continue - example - while
int i = 0;
while (i < 5)
{
if (i == 3)
{
continue;
}
Console.Write(i + ",");
i++;
} |
|
|
Array
DataType[] VariableName
= New DataType[ElementNumber]; |
1.VariableName[Index].
2.Index starts from 0.
Array - example
string[] srtColors1 = new string[] {
"White", "Snow", "GhostWhite"};
Console.Write(srtColors1[0] + ",");
Console.Write(srtColors1[1] + ",");
Console.WriteLine(srtColors1[2]);
var srtColors2 = new string[] {
"LightSkyBlue", "Blue", "Navy"};
Console.Write(srtColors2[0] + ",");
Console.Write(srtColors2[1] + ",");
Console.WriteLine(srtColors2[2]);
var srtColors3 = new[] {
"WhiteSmoke", "Silver", "Black"};
Console.Write(srtColors3[0] + ",");
Console.Write(srtColors3[1] + ",");
Console.WriteLine(srtColors3[2]); |
Result : White,Snow,GhostWhite
Result : LightSkyBlue,Blue,Navy
Result : WhiteSmoke,Silver,Black
Array - example
string[] srtColors = new string[3];
srtColors[0] = "Snow";
srtColors[1] = "Navy";
srtColors[2] = "Silver";
Console.Write(srtColors[0] + ",");
Console.Write(srtColors[1] + ",");
Console.WriteLine(srtColors[2]); |
Result : Snow,Navy,Silver
Array - example
string[] srtColors = new string[] {
"White", "Snow", "GhostWhite"};
Array.Resize(ref srtColors , 4);
srtColors[3] = "WhiteSmoke";
foreach (string srtColor in srtColors)
{
Console.WriteLine(srtColor);
} |
Result : White
Snow
GhostWhite
WhiteSmoke
Array - example
static void Main()
{
string[] srtColors = new string[3] {
"LightSkyBlue", "Blue", "Navy"};
string[] b = GetColor(srtColors);
}
private static string[] GetColor(string[] strColor)
{
return strColor;
} |
Array - example
static void Main()
{
string[] srtBule = new string[] {
"LightSkyBlue", "Blue", "Navy"};
List<string> ColorList
= new List<string>(srtBule);
ColorList.Add("RoyalBlue");
foreach (string sColor in ColorList)
{
Console.WriteLine(sColor);
}
} |
Result : LightSkyBlue
Blue
Navy
RoyalBlue
2D Array - example
string[,] colors = new string[2,3]
{
{"LightSkyBlue", "Blue", "Navy"},
{"WhiteSmoke", "Silver", "Black"}
};
Console.Write(colors[0, 0]+ ",");
Console.Write(colors[0, 1]+ ",");
Console.WriteLine(colors[0, 2]);
Console.Write(colors[1, 0]+ ",");
Console.Write(colors[1, 1]+ ",");
Console.WriteLine(colors[1, 2]); |
Result : LightSkyBlue,Blue,Navy
WhiteSmoke,Silver,Black
|