This article will show and explain you the C# language syntax and statement.
1). Declaring a variable
int a;
int age,salary;
int num=8;
string name;
string fullname=”amar”;
2). Loop
int i=0;
While(i5)
{
Console.WriteLine(i);
++i;
}
Output:--
0
1
2
3
4
3). For Loop
int i=0;
for(int i=0;i5;i++)
{
Console.WriteLine(i);
}
Output:--
0
1
2
3
4
4). Do While
int i=0;
do
{
Console.WriteLine(i);
i++;
}
while(i5);
output:--
0
1
2
3
4
Explanation: The output is same as the while loop but main difference is that condition is checked after exceuting the code inside the loop statement
5). Foreach
string [] names=new string[]{"ram","varun","aman"};
for each(string name in names)
{
Console.WriteLine(name);
}
output:--
ram
varun
aman
Explanation: foreach loop will print the name as it can be used to iterate through a collection like array etc.
6). if...else
int age=10;
if (age==10)
{
Console.WrietLine("your age is 10");
}
else
{
Console.WriteLine("I don't know");
}
Output:--
your age is 10
Explanation: Execute a statement based on condition
7). Break
string [] names=new string[]{"ram","varun","aman"};
for each(string name in names)
{
Console.WriteLine(name);
if(name=="aman")
break;
}
Output:--
ram
varun
Explanation: Break statement is used to break from Loop statement
8). Continue
string [] names=new string[]{"ram","varun","aman"};
for each(string name in names)
{
Console.WriteLine(name);
if(name=="varun")
continue;
Console.WriteLine("Address");
Console.WriteLine("City");
}
Output:--
ram
varun
Explanation: Continue statement will move to the next iteration of the loop, in above case it will execute the codes, if a continue statement comes it will not execute the code after that and move to the next iteration.
8).Switch statement
int age=3;
switch(age)
{
case 10:
Console.WriteLine("age=10");
break;
case 20:
Console.WriteLine("age=20");
break;
case 30:
Console.WriteLine("age=30");
break;
default:
Console.WriteLine("No age");
break;
}
Output:--
age=10
About the author:
PlanetSourceCode.in is a place for all developer providing free source codes, articles, MCA complete projects,complete application in PHP, C/C++, Javascript, Visual Basic, Cobol, Pascal, ASP/VBScript, AJAX, SQL, Perl, Python, Ruby, Mobile Development
- Add new comment
- 26 views