다음을 통해 공유


if-else(C# 참조)

if 문은 문을 실행 하는 값에 따라 한 Boolean 식입니다.다음 예제에서는 Boolean 변수 result를 true로 설정한 다음 if 문에서 이를 확인합니다.출력은 The condition is true입니다.

bool condition = true;

if (condition)
{
    Console.WriteLine("The variable is set to true.");
}
else
{
    Console.WriteLine("The variable is set to false.");
}

에 배치 하 여이 항목의 예제를 실행할 수는 Main 메서드는 콘솔 응용 프로그램입니다.

if C# 문은 다음 예제와 같이 두 개의 폼을 걸릴 수 있습니다.

// if-else statement
if (condition)
{
    then-statement;
}
else
{
    else-statement;
}
// Next statement in the program.


// if statement without an else
if (condition)
{
    then-statement;
}
// Next statement in the program.

에 if-else 문 경우 condition true로 평가 되는 then-statement 실행 합니다.경우 condition false는 else-statement 실행 합니다.때문에 condition 동시에 true 및 false 일 수 없습니다는 then-statement , else-statement 의 if-else 문은 절대로 둘 다 실행할 수 있습니다.후에 then-statement 또는 else-statement 실행을 제어 하려면 다음에 있는 문으로 전송 되는 if 문.

에 if 문이 포함 되지 않은 else 문 경우 condition 참이 then-statement 실행 합니다.경우 condition 제어가 다음 문으로 후 false는 if 문.

모두는 then-statement , else-statement 는 단일 또는 중괄호 안에 여러 문을 구성할 수 있습니다 ({}).단문에 중괄호는 선택 사항 이지만 권장 되는 있습니다.

문이나 문에서 then-statement , else-statement 다른 비롯 한 모든 종류의 수 있습니다 if 문은 원래 안에 중첩 if 문.중첩 if 문 각 else 절에 속한 마지막 if 해당 없는 else.다음 예제에서 Result1 모두 나타납니다 m > 10 및 n > 20 true로 평가 합니다.경우 m > 10 그렇습니다 하지만 n > 20 false 이면 Result2 나타납니다.

// Try with m = 12 and then with m = 8.
int m = 12;
int n = 18;

if (m > 10)
    if (n > 20)
    {
        Console.WriteLine("Result1");
    }
    else
    {
        Console.WriteLine("Result2");
    }

대신, 원하는 경우 Result2 표시 (m > 10) 설정의 시작과 끝에는 중첩 된 중괄호를 사용 하 여 해당 연결을 지정할 수 있습니다 false입니다 if 문으로 다음 예제를 보여 줍니다.

// Try with m = 12 and then with m = 8.
if (m > 10)
{
    if (n > 20)
        Console.WriteLine("Result1");
}
else
{
    Console.WriteLine("Result2");
}

Result2나타나면 조건 (m > 10) false로 평가 합니다.

예제

다음 예제에서는 문자를 키보드에서 입력 한 프로그램에서 사용 하는 중첩 된 if 문이 입력된 문자는 영문자 인지 여부를 확인 합니다.입력된 문자는 영문자 이면 프로그램 입력된 문자 대 / 소문자 인지 여부를 확인 합니다.각 사례에 대 한 메시지를 나타납니다.

Console.Write("Enter a character: ");
char c = (char)Console.Read();
if (Char.IsLetter(c))
{
    if (Char.IsLower(c))
    {
        Console.WriteLine("The character is lowercase.");
    }
    else
    {
        Console.WriteLine("The character is uppercase.");
    }
}
else
{
    Console.WriteLine("The character isn't an alphabetic character.");
}

//Sample Output:

//Enter a character: 2
//The character isn't an alphabetic character.

//Enter a character: A
//The character is uppercase.

//Enter a character: h
//The character is lowercase.

중첩할 수도 있습니다는 if 는 else 블록 내의 문을 다음과 같은 일부 코드와 같이 합니다.예제에서는 중첩 if 문 두 else 블록과 다음 블록 내.조건은 true 또는 false 각 블록에는 메모를 지정 합니다.

// Change the values of these variables to test the results.
bool Condition1 = true;
bool Condition2 = true;
bool Condition3 = true;
bool Condition4 = true;

if (Condition1)
{
    // Condition1 is true.
}
else if (Condition2)
{
    // Condition1 is false and Condition2 is true.
}
else if (Condition3)
{
    if (Condition4)
    {
        // Condition1 and Condition2 are false. Condition3 and Condition4 are true.
    }
    else
    {
        // Condition1, Condition2, and Condition4 are false. Condition3 is true.
    }
}
else
{
    // Condition1, Condition2, and Condition3 are false.
}

다음 예제에서는 입력된 문자가 소문자, 대문자, 또는 숫자 인지 여부를 결정 합니다.모든 세 가지 조건이 false 인 경우 문자는 영숫자 문자 되지 않습니다.각 사례에 대 한 메시지를 표시 하는 예제입니다.

Console.Write("Enter a character: ");
char ch = (char)Console.Read();

if (Char.IsUpper(ch))
{
    Console.WriteLine("The character is an uppercase letter.");
}
else if (Char.IsLower(ch))
{
    Console.WriteLine("The character is a lowercase letter.");
}
else if (Char.IsDigit(ch))
{
    Console.WriteLine("The character is a number.");
}
else
{
    Console.WriteLine("The character is not alphanumeric.");
}

//Sample Input and Output:
//Enter a character: E
//The character is an uppercase letter.

//Enter a character: e
//The character is a lowercase letter.

//Enter a character: 4
//The character is a number.

//Enter a character: =
//The character is not alphanumeric.

모든 유효한 문을 else 블록 또는 다음 블록에 있는 문은 방금 수 있으므로 모든 유효한 부울 식에 대 한 조건을 사용할 수 있습니다.같은 논리 연산자를 사용할 수 있습니다 & &, , |,|and ! 복합 조건을 만들 수 있습니다.다음 코드 예제를 보여 줍니다.

// NOT
bool result = true;
if (!result)
{
    Console.WriteLine("The condition is true (result is false).");
}
else
{
    Console.WriteLine("The condition is false (result is true).");
}

// Short-circuit AND
int m = 9;
int n = 7;
int p = 5;
if (m >= n && m >= p)
{
    Console.WriteLine("Nothing is larger than m.");
}

// AND and NOT
if (m >= n && !(p > m))
{
    Console.WriteLine("Nothing is larger than m.");
}

// Short-circuit OR
if (m > n || m > p)
{
    Console.WriteLine("m isn't the smallest.");
}

// NOT and OR
m = 4;
if (!(m >= n || m >= p))
{
    Console.WriteLine("Now m is the smallest.");
}
// Output:
// The condition is false (result is true).
// Nothing is larger than m.
// Nothing is larger than m.
// m isn't the smallest.
// Now m is the smallest.

C# 언어 사양

자세한 내용은 C# 언어 사양을 참조하십시오. 이 언어 사양은 C# 구문 및 사용법에 대한 신뢰할 수 있는 소스입니다.

참고 항목

참조

C# 키워드

?: 연산자(C# 참조)

if-else 문을 (C++)

switch(C# 참조)

개념

C# 프로그래밍 가이드

기타 리소스

C# 참조