Skip to main content
Engineering LibreTexts

9.5: Conditionally Controlled Loop Example

  • Page ID
    54283
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In this example, we will write a Fortran program that will read a valid date from the user. The date will consist of three values, one for each of the month, day, and year. This example will use some of the previous example fragments.

    Understand the Problem

    For this limited example, we will request a date where the year is between 1970 and 2020. The month must be between 1 and 12. The date will depend on the month since some months have 30 or 31 days. February has either 28 days or 29 days if it is a leap year. Due to the limited allowable range of the year, the determination of a leap year can be performed by checking if the year is evenly divisible by 4 (which implies a leap year).

    Create the Algorithm

    For this problem, we will need to read the three values (month, day, and year). Then, we will check the values to ensure that date is valid. Then, we will check the month first and then the year since it will be used to check the date.

    The months January, March, May, July, August, October, and December have 31 days. The months April, June, September, and November have 30 days. February has 28 days unless the year is evenly divisible by 4, in which case February has 29 days.

    ! declare variables; integer -> month, day, year
    ! display initial header
    
    ! loop
    !     request month, day, and year
    !     read month, day, and year
    !     check month (1-12)
    !     check year (1970-2020)
    !     check day
    !         1,3,5,7,8,10,12 → 31 days
    !         4,6,9,11 → 30 days
    !         2 → if modulo of year/4 is 0 → 29 days
    !         2 → if modulo of year/4 is not 0 → 28 days
    !         if invalid, display error and loop to try again
    !     end loop
    ! display results
    

    For convenience, the steps are written as program comments.

    Implement the Program

    Based on the algorithm, the program below could be created.

    program dateCheck
    
    ! declare variables
    implicit none
    integer :: month, day, year, dayMax
    
    ! display initial header
        write (*,*) "Date Verification Example"
    
    ! loop
        do
            ! request month, day, and year
            write (*,*) "Enter month, day, and year"
    
            ! read month, day, and year
            read (*,*) month, day, year
    
            ! check month (1-12)
            if ( month < 1 .or. month > 12 ) then
                write (*,*) "Error, invalid month"
                cycle
            end if
    
            ! check year (1970-2020)
            if ( year < 1970 .or. year > 2020 ) then
                write (*,*) "Error, invalid year"
                cycle
            end if
    
            ! check day
            !     1,3,5,7,8,10,12 → 31 days
            !     4,6,9,11 → 30 days
            !     2 → if modulo of year/4 is 0 → 29 days
            !     2 → if modulo of year/4 is not 0 → 28 days
    
            select case (month)
                case (1,3,5,7,8,10,12)
                    dayMax = 31
                case (2)
                    if (mod(year,4)==0) then
                        dayMax = 29
                    else
                        dayMax = 28
                    end if
                case (4,6,9,11)
                    dayMax = 30
            end select
    
            ! if invalid, display error and loop to try again
            if ( day < 1 .or. day > dayMax ) then
                write (*,*) "Error, invalid day."
                cycle
            end if
            
            exit
            
    ! end loop
        end do
    
    ! display results
        write (*,*) "Valid Date is:", month, day, year
    
    end program dateCheck
    

    The spacing and indentation are not required, but help to make the program more readable.

    Test/Debug the Program

    For this problem, the testing would be to ensure that the results match the expected value. This will require entering a series of different dates and verifying that the displayed output is correct for the given input data.


    This page titled 9.5: Conditionally Controlled Loop Example is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?