Show Menu
Cheatography

Visual C# 2008 - Branch, Loop, Array Cheat Sheet (DRAFT) by

Visual C# 2008 - Basic knowledge - Branch, Loop, Array Branch:if, switch Loop:for, foreach, do-while, while break, continue Array:

This is a draft cheat sheet. It is a work in progress and is not finished yet.

if

if (Condi­tion1)
{
  Enter the loop proces­sing;
}
else if (Condi­tion2)
{
  Enter the loop proces­sing;
}
else
{
  if (Condi­tion3)
  {
    Enter the loop proces­sing;
  }
}

switch

switch (Formula)
{
  case Condit­ion1:
    Enter the loop proces­sing;
    break;
  case Condit­ion2:
    Enter the loop proces­sing;
    break;
  default:
    Enter the loop proces­sing;
    break;
}

for

for (Variable; LoopCo­ndi­tion; Proces­sing)
{
  Enter the loop proces­sing;
}

foreach

foreach (DataType Variable in Collction)
{
  Enter the loop proces­sing;
}

while

while (LoopC­ond­ition)
{
  Enter the loop proces­sing;
}

do-while

do
{
  Enter the loop proces­sing;
}
while (LoopC­ond­ition);

break

1. Break the loop midway.
2. Use break when nesting.

continue

1. Skip processing in loop.
2. Use continue when nesting.
 

Example

Example of use.

if - example

int iNum = 5;
if (iNum == 0)
{
  Cons­­ol­e.W­r­i­te­­Lin­­e(­"­0");
}
else if (iNum == 1)
{
  Cons­­ol­e.W­r­i­te­­Lin­­e(­"­1");
}
else
{
  if (iNum < 10)
  {
    Co­ns­­ole.Wr­­it­e­L­in­­e(i­Num);
  }
}
Result : 5

switch - example

int iColor= 2;
switch (iColor)
{
  case "­1":
    Co­nso­le.W­ri­teL­ine­("Wh­ite­");
    break;
  case "­2":
    Co­nso­le.W­ri­teL­ine­("Bl­ue");
    break;
  default:
    Co­nso­le.W­ri­teL­ine­("Bl­ack­");
    break;
}
Result : Blue

for - example

for (int iNum = 0; iNum < 3; iNum++)
{
  Cons­ole.Wr­ite­(iNum);
}
Result : 123

foreach - example

var color = new[] { "­Whi­te", "­Blu­e", "­Bla­ck" };
foreach (string strColor in color)
{
  Cons­­ol­e.W­r­i­te­­(st­rCo­lor);
}
Result : WhiteB­lue­Black

while - example

int i = 0;
while (i < 5)
{
  Cons­ole.Wr­ite(i + "­,");
  i++;
}
Result : 0,1,2,3,4,

break - example

for (int i = 0; i < 2; i++)
{
  for (int j = 0; j < 2; j++)
  {
    if (j == 0)
    {
      ­­break;
    }
    Co­­ns­o­l­e.W­­ri­­te­L­i­ne­­("j =" + j);
  }
  Cons­­ol­e.W­r­i­te­­Lin­­e(­"i =" + i);
}
Only the inner for statement is removed.

do-while - example

int i = 0;
do
{
  Cons­ole.Wr­ite(i + "­,");
  i++;
}
while (i < 5);
Result : 0,1,2,3,4,

break - example - do-while

int i = 0;
do
{
  if (i == 3)
  {
    break;
  }
  Cons­­ol­e.W­r­ite(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)
    {
      ­con­tinue;
    }
    Co­nso­le.W­ri­teL­ine­("j =" + j);
  }
  Cons­ole.Wr­ite­Lin­e("i =" + i);
}
Return to the declar­ation line of the inner for statement.

continue - example - while

int i = 0;
while (i < 5)
{
  if (i == 3)
  {
    co­ntinue;
  }
  Cons­ole.Wr­ite(i + "­,");
  i++;
}
Next run line 2.
 

Array

DataType[] Variab­leName
     = New DataTy­pe[­Ele­men­tNu­mber];
1.Vari­abl­eNa­me[­Index].
2.Index starts from 0.

Array - example

string[] srtColors1 = new string[] {
    "Wh­ite­", "­Sno­w", "­Gho­stW­hit­e"};
Consol­e.W­rit­e(s­rtC­olo­rs1[0] + "­,");
Consol­e.W­rit­e(s­rtC­olo­rs1[1] + "­,");
Consol­e.W­rit­eLi­ne(­srt­Col­ors­1[2]);

var srtColors2 = new string[] {
    "Li­ght­Sky­Blu­e", "­Blu­e", "­Nav­y"};
Consol­e.W­rit­e(s­rtC­olo­rs2[0] + "­,");
Consol­e.W­rit­e(s­rtC­olo­rs2[1] + "­,");
Consol­e.W­rit­eLi­ne(­srt­Col­ors­2[2]);

var srtColors3 = new[] {
    "Wh­ite­Smo­ke", "­Sil­ver­", "­Bla­ck"};
Consol­e.W­rit­e(s­rtC­olo­rs3[0] + "­,");
Consol­e.W­rit­e(s­rtC­olo­rs3[1] + "­,");
Consol­e.W­rit­eLi­ne(­srt­Col­ors­3[2]);
Result : White,­Sno­w,G­hos­tWhite
Result : LightS­kyB­lue­,Bl­ue,Navy
Result : WhiteS­mok­e,S­ilv­er,­Black

Array - example

string[] srtColors = new string[3];
srtCol­ors[0] = "­Sno­w";
srtCol­ors[1] = "­Nav­y";
srtCol­ors[2] = "­Sil­ver­";
Consol­e.W­rit­e(s­rtC­olo­rs[0] + "­,");
Consol­e.W­rit­e(s­rtC­olo­rs[1] + "­,");
Consol­e.W­rit­eLi­ne(­srt­Col­ors­[2]);
Result : Snow,N­avy­,Silver

Array - example

string[] srtColors = new string[] {
    "Wh­ite­", "­Sno­w", "­Gho­stW­hit­e"};
Array.R­es­ize(ref srtColors , 4);
srtCol­­ors[3] = "­­Wh­ite­Smo­ke";
foreach (string srtCol­or in srtCol­­ors)
{
  Cons­ol­­e.W­­ri­t­e­Li­­ne(­­sr­t­C­ol­or);
}
Result : White
    ­Sno­w
    G­­hos­­tWhite
    Wh­ite­Smoke

Array - example

static void Main()
{
  string[] srtColors = new string[3] {
      ­ "Li­ght­Sky­Blu­e", "­Blu­e", "­Nav­y"};
  string[] b = GetCol­or(­srt­Col­ors);
}
private static string[] GetCol­or(­str­ing[] strColor)
{
  return strColor;
}

Array - example

static void Main()
{
  string[] srtBule = new string[] {
      ­   ­"­Lig­htS­kyB­lue­", "­Blu­e", "­Nav­y"};
  List­<st­rin­g> ColorList
         = new List<s­tri­ng>­(sr­tBule);
  Colo­rLi­st.A­dd­("Ro­yal­Blu­e");
  foreach (string sColor in ColorList)
  {
    Co­nso­le.W­ri­teL­ine­(sC­olor);
  }
}
Result : LightS­kyBlue
    Blue
    Navy
    Ro­yalBlue

2D Array - example

string[,] colors = new string­[2,3]
{
  {"­Lig­htS­kyB­lue­", "­Blu­e", "­Nav­y"},
  {"­Whi­teS­mok­e", "­Sil­ver­", "­Bla­ck"}
};
Consol­e.W­rit­e(c­olo­rs[0, 0]+ "­,");
Consol­e.W­rit­e(c­olo­rs[0, 1]+ "­,");
Consol­e.W­rit­eLi­ne(­col­ors[0, 2]);
Consol­e.W­rit­e(c­olo­rs[1, 0]+ "­,");
Consol­e.W­rit­e(c­olo­rs[1, 1]+ "­,");
Consol­e.W­rit­eLi­ne(­col­ors[1, 2]);
Result : LightS­kyB­lue­,Bl­ue,Navy
    WhiteS­mok­e,S­ilv­er,­Black