Nội dung bài học không khai báo từ viết tắt bằng thẻ abbr.
Nội dung bài học
Decision making structures requires the programmer to specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Following is the general form of a typical decision making structure found in most of the programming languages −
decision_making-235x300
C# provides following types of decision making statements. Click the following links to check their detail.
Sr.No.
Statement & Description
1
An if statement consists of a boolean expression followed by one or more statements.
2
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
3
You can use one if or else if statement inside another if or else ifstatement(s).
4
A switch statement allows a variable to be tested for equality against a list of values.
5
You can use one switch statement inside another switchstatement(s).
If statement
An if statement consists of a boolean expression followed by one or more statements.
Syntax
The syntax of an if statement in C# is −
csharp
1if(boolean_expression) {
2 /* statement(s) will execute if the boolean expression is true */
3}
If the boolean expression evaluates to true, then the block of code inside the if statement is executed. If boolean expression evaluates to false, then the first set of code after the end of the if statement(after the closing curly brace) is executed.
Flow Diagram
if_statement-237x300
Example
csharp
1using System;
2
3namespace DecisionMaking {
4 class Program {
5 static void Main(string[] args) {
6 /* local variable definition */
7 int a = 10;
8
9 /* check the boolean condition using if statement */
10 if (a < 20) {
11 /* if condition is true then print the following */
12 Console.WriteLine("a is less than 20");
13 }
14 Console.WriteLine("value of a is : {0}", a);
15 Console.ReadLine();
16 }
17 }
18}
When the above code is compiled and executed, it produces the following result −
shellscript
1a is less than 20;
2value of a is : 10
If...else statement
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
Syntax
The syntax of an if...else statement in C# is −
csharp
1if(boolean_expression) {
2 /* statement(s) will execute if the boolean expression is true */
3} else {
4 /* statement(s) will execute if the boolean expression is false */
5}
If the boolean expression evaluates to true, then the if block of code is executed, otherwise else block of code is executed.
Flow Diagram
if_else_statement-235x300
Example
csharp
1using System;
2
3namespace DecisionMaking {
4 class Program {
5 static void Main(string[] args) {
6 /* local variable definition */
7 int a = 100;
8
9 /* check the boolean condition */
10 if (a < 20) {
11 /* if condition is true then print the following */
12 Console.WriteLine("a is less than 20");
13 } else {
14 /* if condition is false then print the following */
15 Console.WriteLine("a is not less than 20");
16 }
17 Console.WriteLine("value of a is : {0}", a);
18 Console.ReadLine();
19 }
20 }
21}
When the above code is compiled and executed, it produces the following result −
shellscript
1a is not less than 20;
2value of a is : 100
The if...else if...else Statement
An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
When using if, else if, else statements there are few points to keep in mind.
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of the remaining else if's or else's will be tested.
Syntax
The syntax of an if...else if...else statement in C# is −
csharp
1if(boolean_expression 1) {
2 /* Executes when the boolean expression 1 is true */
3}
4else if( boolean_expression 2) {
5 /* Executes when the boolean expression 2 is true */
6}
7else if( boolean_expression 3) {
8 /* Executes when the boolean expression 3 is true */
9} else {
10 /* executes when the none of the above condition is true */
11}
Example
csharp
1using System;
2
3namespace DecisionMaking {
4 class Program {
5 static void Main(string[] args) {
6 /* local variable definition */
7 int a = 100;
8
9 /* check the boolean condition */
10 if (a == 10) {
11 /* if condition is true then print the following */
12 Console.WriteLine("Value of a is 10");
13 }
14 else if (a == 20) {
15 /* if else if condition is true */
16 Console.WriteLine("Value of a is 20");
17 }
18 else if (a == 30) {
19 /* if else if condition is true */
20 Console.WriteLine("Value of a is 30");
21 } else {
22 /* if none of the conditions is true */
23 Console.WriteLine("None of the values is matching");
24 }
25 Console.WriteLine("Exact value of a is: {0}", a);
26 Console.ReadLine();
27 }
28 }
29}
When the above code is compiled and executed, it produces the following result −
shellscript
1None of the values is matching
2Exact value of a is: 100
Switch
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.
Syntax
The syntax for a switch statement in C# is as follows −
csharp
1switch(expression) {
2 case constant-expression1 :
3 statement(s);
4 break;
5 case constant-expression2 :
6 case constant-expression3 :
7 statement(s);
8 break;
9
10 /* you can have any number of case statements */
11 default : /* Optional */
12 statement(s);
13}
The following rules apply to a switch statement −
The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
Not every case needs to contain a break. If no break appears, then it will raise a compile time error.
A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true.
Flow Diagram
switch_statement-224x300
Example
csharp
1using System;
2
3namespace DecisionMaking {
4 class Program {
5 static void Main(string[] args) {
6 /* local variable definition */
7 char grade = 'B';
8
9 switch (grade) {
10 case 'A':
11 Console.WriteLine("Excellent!");
12 break;
13 case 'B':
14 case 'C':
15 Console.WriteLine("Well done");
16 break;
17 case 'D':
18 Console.WriteLine("You passed");
19 break;
20 case 'F':
21 Console.WriteLine("Better try again");
22 break;
23 default:
24 Console.WriteLine("Invalid grade");
25 break;
26 }
27 Console.WriteLine("Your grade is {0}", grade);
28 Console.ReadLine();
29 }
30 }
31}
When the above code is compiled and executed, it produces the following result −
shellscript
1Well done
2Your grade is B
The ? : Operator
We have covered conditional operator ? : in previous chapter which can be used to replace if...else statements. It has the following general form −
typescript
1Exp1 ? Exp2 : Exp3;
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.
The value of a ? expression is determined as follows: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.
NenTang.vn
Học lập trình - Làm chủ công nghệ
NenTang.vn là nền tảng học lập trình trực tuyến dành cho người Việt. Toàn bộ nội dung được biên soạn bởi chuyên gia, cập nhật liên tục và miễn phí cho cộng đồng.