• We just launched and are currently in beta. Join us as we build and grow the community.

C# language syntax and statement

CheckMate1

Game Dev
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
156
Likes
50
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Submitted by
planetsourcecode

on
Sun, 07/05/2009 - 21:10

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

 

452,496

326,977

326,985

Top