using System;
using System.Collections.Generic;
using System.Text;
namespace UsingGoTo
{
class UsingGoTo
{
static void Main(string[] args)
{
int i = 0;
repeat: // the label
Console.WriteLine("i: {0}", i+1);
i++;
if (i < 15)
{
goto repeat;
}
return;
}
}
}
#region using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace Whileloop
{
class WhileLoop
{
static void Main(string[] args)
{
int i = 0;
while (i < 10)
{
Console.WriteLine("i: {0}", i);
i++;
}
return;
}
}
}
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace DoWhile
{
class DoWhile
{
static int Main(string[] args)
{
int i = 11;
do
{
Console.WriteLine("i: {0}", i);
i++;
} while (i < 10);
return 0;
}
}
}
//i is initialized to 11 and the while test fails, but only after the body of the loop has run once
using System;
using System.Collections.Generic;
using System.Text;
namespace ForLoop
{
class ForLoop
{
static void Main(string[] args)
{
Console.WriteLine("In a for loop, the condition is tested before the statements are executed");
for (int i = 0; i < 100; i++)
{
Console.Write("{0} ", i);
if (i % 10 == 0)
{
Console.WriteLine("\t{0}", i);
}
}
return;
}
}
}
#region using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace ForLoopScope
{
class ForLoopScope
{
static void Main(string[] args)
{
for(int i=0; i < 100; i++)
{
Console.Write("{0}", i);
if(i % 10==0)
{
Console.WriteLine("\t{0}", i);
}
}
//Console.WriteLine("\n Final value of i: {0}", i);
//this line above fails, because the variable i is not available outside the scope of the loop itself
}
}
}
No comments:
Post a Comment