Selection Statement in C# 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:
- If (condition) {
- //statement control you operation
- }
- //optional
- else if {
- //statement control you operation
- }
- //optional
- else {
- //statement control you operation
- }
- class Polygon{
- static void Main(string[] args)
- {
- mPolygon.checkPolygon(3);
- mPolygon.checkPolygon(5);
- }
- /*
- * this function will check whether a polygon is a triangle or a rectagle
- */
- private void checkPolygon(int num_of_edge)
- {
- if(num_of_edge==3){//if number of edge is 3, then polygon is triangle
- Console.WriteLine("polygon is triangle");
- }
- else if (num_of_edge == 4)//if number of edge is 4, then polygon is rectangle
- {
- Console.WriteLine("polygon is rectangle");
- }
- else//if not, don't know what it is.
- {
- Console.WriteLine("let me check...");
- }
- }
- }
We can also use nested statement to more flexible in controlling the flow. Let’s write the above example again:
- class Polygon
- {
- static void Main(string[] args)
- {
- mPolygon.checkPolygon(3);
- mPolygon.checkPolygon(5);
- }
- /*
- * this function will check whether a polygon is a triangle or a rectagle
- */
- private void checkPolygon(int num_of_edge)
- {
- if(num_of_edge==3){ //if number of edge is 3, then polygon is triangle
- Console.WriteLine("polygon is triangle");
- }
- else
- {
- if (num_of_edge == 4){//if number of edge is 4, then polygon is rectangle
- Console.WriteLine("polygon is rectangle");
- }else
- {
- Console.WriteLine("let me check...");
- }
- }
- }
- }
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:
- Switch (value)
- {
- Case value_1:
- //do stuff for value_1
- break;
- Case value_2:
- //do stuff for value_2
- break;
- ………
- Default:
- //do stuff for default case
- break;
- }
- int roll = rn.Next(1, 10);//generate a ramdom value between 1 and 10.
- switch (roll)
- {
- case 1:
- Console.WriteLine("roll is 1");
- break;
- case 2:
- Console.WriteLine("roll is 2");
- break;
- case 3:
- Console.WriteLine("roll is 3");
- break;
- case 4://here, we assume that when random number is 4 or 5
- case 5://so we out the same string
- Console.WriteLine("roll is 4 or 5");
- break;
- default:
- Console.WriteLine("roll is greater than 5");
- break;
- }
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
- 551 views