if 또는 else if 문 안에 다른 if 또는 else if 문을 사용하십시오. 중첩된 if 문의 구문은 다음과 같습니다. -
if( boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
} 다음은 C#에서 중첩된 if 문의 사용법을 보여주는 예입니다. 여기에 두 가지 조건을 확인하는 두 개의 if 문이 있습니다.
if (a == 5) {
/* if condition is true then check the following */
if (b == 10) {
/* if condition is true then print the following */
Console.WriteLine("Value of a is 5 and b is 10");
}
} 다음은 전체 예입니다.
예
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
//* local variable definition */
int a = 5;
int b = 10;
/* check the boolean condition */
if (a == 5) {
/* if condition is true then check the following */
if (b == 10) {
/* if condition is true then print the following */
Console.WriteLine("Value of a is 5 and b is 10");
}
}
Console.WriteLine("Exact value of a is : {0}", a);
Console.WriteLine("Exact value of b is : {0}", b);
Console.ReadLine();
}
}
} 출력
Value of a is 5 and b is 10 Exact value of a is : 5 Exact value of b is : 10