JosephG420
Release Engineer
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
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:
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:
We can also use nested statement to more flexible in controlling the flow. Let’s write the above example again:
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:
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:
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.
Book traversal links for Selection Statement in C# Language
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
- }
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:
- class
Polygon{
- static
void
Main(
string
[
]
args)
- {
- Polygon mPolygon =
new
Polygon(
)
;
- 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)
- {
- Polygon mPolygon =
new
Polygon(
)
;
- 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
;
- }
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:
- Random rn =
new
Random(
)
;
//create a Random object
- 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.
Book traversal links for Selection Statement in C# Language
- ‹ Multiple Forms in C#
- Up
- Stack and Queue in C# ›