Selection Statement in C# Language

Submitted by: 
Language: 

Objective

In programming language, selection statement is very important. With selection statement, you can control the flow of program, and produce a valid output. This section will guide you how to use some selection statements in basic.

Let's go

Selection statement enable you to execute specific blocks of code based on the results of a condition. Here we have two basic selection statement: If statement and switch statement.

If statement

The basic structure of If statement is listed bellow:

  1. If (condition) {
  2. //statement control you operation
  3. }
  4. //optional
  5. else if {
  6. //statement control you operation
  7. }
  8. //optional
  9. else {
  10. //statement control you operation
  11. }

Note that if you have “else if” in the if block, so you must need “else” in the end of the block. The sample bellow describes more detail of If statement:

  1. class Polygon{
  2. static void Main(string[] args)
  3. {
  4. Polygon mPolygon = new Polygon();
  5. mPolygon.checkPolygon(3);
  6. mPolygon.checkPolygon(5);
  7.  
  8. }
  9.  
  10. /*
  11. * this function will check whether a polygon is a triangle or a rectagle
  12. */
  13. private void checkPolygon(int num_of_edge)
  14. {
  15. if(num_of_edge==3){//if number of edge is 3, then polygon is triangle
  16. Console.WriteLine("polygon is triangle");
  17. }
  18. else if (num_of_edge == 4)//if number of edge is 4, then polygon is rectangle
  19. {
  20. Console.WriteLine("polygon is rectangle");
  21. }
  22. else//if not, don't know what it is.
  23. {
  24. Console.WriteLine("let me check...");
  25. }
  26. }
  27. }

We can also use nested statement to more flexible in controlling the flow. Let’s write the above example again:

  1. class Polygon
  2. {
  3. static void Main(string[] args)
  4. {
  5. Polygon mPolygon = new Polygon();
  6. mPolygon.checkPolygon(3);
  7. mPolygon.checkPolygon(5);
  8. }
  9.  
  10. /*
  11.  * this function will check whether a polygon is a triangle or a rectagle
  12. */
  13. private void checkPolygon(int num_of_edge)
  14. {
  15. if(num_of_edge==3){ //if number of edge is 3, then polygon is triangle
  16. Console.WriteLine("polygon is triangle");
  17. }
  18. else
  19. {
  20. if (num_of_edge == 4){//if number of edge is 4, then polygon is rectangle
  21.  
  22. Console.WriteLine("polygon is rectangle");
  23. }else
  24. {
  25. Console.WriteLine("let me check...");
  26. }
  27.  
  28. }
  29. }
  30.  
  31. }


Switch statement

C# provides a much easier way to modify program flow based on multiple values staored in a variable: the switch statement. The format of the switch statement is as follows:

  1. Switch (value)
  2. {
  3. Case value_1:
  4. //do stuff for value_1
  5. break;
  6. Case value_2:
  7. //do stuff for value_2
  8. break;
  9. ………
  10. Default:
  11. //do stuff for default case
  12. break;
  13. }

Note that if you want to do the same stuff for two case value, for example value_1 and value_2, you can remove “break” keyword between two case (or you don’t need to remove, but the block of code will be wordy. Example of switch statement is list as follows:

  1. Random rn = new Random();//create a Random object
  2. int roll = rn.Next(1, 10);//generate a ramdom value between 1 and 10.
  3. switch (roll)
  4. {
  5. case 1:
  6. Console.WriteLine("roll is 1");
  7. break;
  8. case 2:
  9. Console.WriteLine("roll is 2");
  10. break;
  11. case 3:
  12. Console.WriteLine("roll is 3");
  13. break;
  14. case 4://here, we assume that when random number is 4 or 5
  15. case 5://so we out the same string
  16. Console.WriteLine("roll is 4 or 5");
  17. break;
  18. default:
  19. Console.WriteLine("roll is greater than 5");
  20. break;
  21. }

Summary

In this tutorial, we have learnt a about two selection statement: if statement and switch statement. There is no different output between these statement. You just note that if you have multiple flow need to be controled, so you might want to use switch statement for short code.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3>
  • 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.
  • Lines and paragraphs break automatically.