Loading

C# language syntax and statement

Submitted by: 


This article will show and explain you the C# language syntax and statement.

1). Declaring a variable

  1. int a;
  2. int age,salary;
  3. int num=8;
  4. string name;
  5. string fullname=”amar”;

2). Loop

  1. int i=0;
  2. While(i<5)
  3. {
  4. Console.WriteLine(i);
  5. ++i;
  6. }

Output:--

0
1
2
3
4

3). For Loop

  1. int i=0;
  2. for(int i=0;i<5;i++)
  3. {
  4. Console.WriteLine(i);
  5. }

Output:--

0
1
2
3
4

4). Do While

  1. int i=0;
  2. do
  3. {
  4. Console.WriteLine(i);
  5. i++;
  6. }
  7. while(i<5);

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

  1. string [] names=new string[]{"ram","varun","aman"};
  2. for each(string name in names)
  3. {
  4. Console.WriteLine(name);
  5. }

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

  1. int age=10;
  2. if (age==10)
  3. {
  4. Console.WrietLine("your age is 10");
  5. }
  6. else
  7. {
  8. Console.WriteLine("I don't know");
  9. }

Output:--

your age is 10

Explanation: Execute a statement based on condition

7). Break

  1. string [] names=new string[]{"ram","varun","aman"};
  2. for each(string name in names)
  3. {
  4. Console.WriteLine(name);
  5. if(name=="aman")
  6. break;
  7. }

Output:--

ram
varun

Explanation: Break statement is used to break from Loop statement

8). Continue

  1. string [] names=new string[]{"ram","varun","aman"};
  2. for each(string name in names)
  3. {
  4. Console.WriteLine(name);
  5. if(name=="varun")
  6. continue;
  7.  
  8. Console.WriteLine("Address");
  9. Console.WriteLine("City");
  10. }

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

  1. int age=3;
  2. switch(age)
  3. {
  4. case 10:
  5. Console.WriteLine("age=10");
  6. break;
  7. case 20:
  8. Console.WriteLine("age=20");
  9. break;
  10. case 30:
  11. Console.WriteLine("age=30");
  12. break;
  13. default:
  14. Console.WriteLine("No age");
  15. break;
  16. }

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




Comments

planetsourcecode's picture

this lesson is dedicated to my friend Jay Pee. thanks buddy

please sir give me coding to make editable listview.

Good day sir
can u help me what is the code when using while loop infinite w/ switch case in one problem.please help me sir.

Pages

Add new comment

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.