본문 바로가기
학부 강의 노트/포트란 프로그래밍 (보충예제)

포트란 기본 예제5 (선택구조 IF)

by Dr. STEAM 2022. 6. 8.
반응형

선택구조를 사용한 프로그램의 사용 예

  1. 간단한 IF 문 사용
  2. 블록 IF 문 사용

 

1. 간단한 IF문 사용

 

program mainprocedure
    implicit none
    integer :: month, days
10  print *, ' Tyep a month ?'   
    read *, month    ! 표준 입력장치(키보드)를 통해서 입력 받을 경우
    
    if(month ==1) days=31
    if(month ==2) days=29
    if(month ==3) days=31
    if(month ==4) days=30
    if(month ==5) days=31
    if(month ==6) days=30
    if(month ==7) days=31
    if(month ==8) days=31
    if(month ==9) days=30
    if(month ==10) days=31
    if(month ==11) days=30
    if(month ==12) days=31
    
    if (month < 1 .or. month > 12) stop    ! 해당하는 달이 존재하지 않으면 무조건 종료한다.
    print *, ' Month:days ', month, days
    goto 10   !! 결과 출력 후 무조건 goto 문에 의해 다음 자료를 입력받기 위해 문번호 10으로 분기한다. 
    
end program mainprocedure

 

 

 

2. 블록 IF문 사용  

앞의 코드를 아래와 같이 수정할 수 있다.

program mainprocedure
    implicit none
    integer :: month, days
10  print *, ' Tyep a month ?'
    read *, month
    
    if(month ==1) then ; days=31
    else if(month ==2) then ;  days=29
    else if(month ==3) then ; days=31
    else if(month ==4) then ;  days=30
    else if(month ==5) then ;  days=31
    else if(month ==6) then ;  days=30
    else if(month ==7) then ;  days=31
    else if(month ==8) then ;  days=31
    else if(month ==9) then ;  days=30
    else if(month ==10) then ;  days=31
    else if(month ==11) then ;  days=30
    else if(month ==12)  then ; days=31
    else ; stop
    endif
        
    print *, ' Month:days ', month, days
    goto 10
    
end program mainprocedure

 

 

 

728x90
반응형