본문 바로가기
C#

if, else, else if 분기문 IfElse

by JAESEONG LEE- developer 2022. 7. 19.

if문에서 사용하는 조건식은 true 또는 false의 값을 가지는 bool 형식이어야 합니다. 이 조건식이 참인 경우에만 if 문 뒤에 따라오는 코드가 실행됩니다. 거짓이면 아무 일도 일어나지 않습니다.

else if 는 if문처럼 조건식을 가지며 if문에 종속되어 사용됩니다. 

다음은 if 분기문의 예제 프로그램입니다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
 
namespace IfElse
{
    class MainApp
    {
        static void Main(string[] args)
        {
            Console.Write("숫자를 입력하세요. : ");
 
            string input = Console.ReadLine();
            int number = Int32.Parse(input);
 
            if (number < 0)
                Console.WriteLine("음수");
            else if (number > 0)
                Console.WriteLine("양수");
            else
                Console.WriteLine("0");
 
            if (number % 2 == 0)
                Console.WriteLine("짝수");
            else
                Console.WriteLine("음수");
        }
    }
}
cs

 

데이터 입력 값을 음수와 짝수로 구분하였습니다.

'C#' 카테고리의 다른 글

switch 문  (0) 2022.07.20
if문 중첩해서 사용하기 IfIf  (0) 2022.07.19
할당 연산자 Assignment0perator  (0) 2022.07.19
비트 논리 연산자 Bitwise0perator  (0) 2022.07.19
비트 연산자 Shift0perator  (0) 2022.07.19