Introduction to Computer Programming Loops 1/2
Chap 4 Loops and Character Manipulation Fortran 95/2003 for scientists and Engineers
by Stephen J. Chapman
Prepared by Walter Chen, Dept. of Civil Engineering, NTUT
For classroom teaching purpose
Control constructs: Loops Loops are Fortran constructs that permit us to execute a sequence
of statements more than once While loops
The code in a while loop is repeated an indefinite number of times until some user-specified condition is satisfied
Iterative loops (counting loops) The code in an interactive loop is repeated a specified
number of times, and the number of repetitions is known before the loop starts
While Loops
The While Loop
Fortran code
Flow chart
While is not a keyword
EXIT statement A while loop may contain one or more EXIT statements to
terminate its execution Each EXIT statement is usually a part of an IF statement or
block IF construct If the logical_expr in the IF is true when the statement is
executed, control transfers immediately to the first statement after the END DO
Good programming practice
In a well-structured program, every while loop should have a single entry point and a single exit point
The entry point for a while loop is the DO statement, and the exit point is the EXIT statement
Example 4-1 statistical analysis
Average or arithmetic mean
Standard deviation
Requirements
Implement an algorithm that reads in a set of measurements and calculates the mean and the standard deviation of the input data set
We do not know in advance how many measurements are included in the data set
The program must be able to read in an arbitrary number of measurements
Analysis We can use a while loop to accumulate the input
measurements before performing the calculations We must have some way of telling the program that there
are no more data values to enter We will assume that all the input measurements are
either positive or zero, and we will use a negative input value as a flag to indicate that there are no more data values to read
Procedures State the problem Define the inputs and outputs
Input: unknown number of positive or zero real numbers Output: original data, mean, standard deviation
Design the algorithm See flow chart
Turn the algorithm into Fortran statements Test the program
Flow Chart
Codes
Declare all variables
Beginning
End
Common Technique
Test values and Results
Fatal flaw in the preceding program
If we enter either no numbers or only one number, then we are dividing by zero in the equations
The division-by-zero error causes the program to abort
We need to modify the program to detect this problem and stop gracefully
Fixed program
Check at least 2 numbers to compute the standard deviation
Do While Loop
The DO WHILE loop
If the logical expression is true, statements 1 through n are executed
The process is repeated until the logical expression becomes false
The iterative or counting loop
Each of the three DO loop parameters istart, iend, and incr may be a constant, a variable, or an expression
If index*incr ≦iend*incr, the program body executes
Afterwards, the control variable is recalculated as index = index + incr
Flow chart
Execute 10 times Execute 5 times
Execute 0 times Execute 4 times
Examples:
Example 4-3 calculating the day of year
The day of year is the number of days (including the current day) that have elapsed since the beginning of a given year
Write a Fortran program that accepts a day, month, and year and calculates the day of year corresponding to that date
Solution To determine the day of year, this program needs to sum the
number of days in each month preceding the current month, plus the number of elapsed days in the current month
A DO loop is used to perform this sum A SELECT CASE is used to determine the proper number of days
to add for each month During a leap year, an extra day must be added to the day of
year for any month after February
Gregorian calendar The most widely used calendar in the world today Was decreed by Pope Gregory XIII, after whom it was named A reform of the Julian calendar, in which all years exactly
divisible by 4 are leap years The Julian calendar is a reform of the Roman calendar, and
was introduced by Julius Caesar in 46 BC Dropped 10 days to bring the calendar back into
synchronization with the seasons
Source: wikipedia
Adoption in UK
Britain and the British Empire (including the eastern part of what is now the United States) adopted the Gregorian calendar in 1752 by which time it was necessary to correct by 11 days
Wednesday, 2 September 1752 was followed by Thursday, 14 September 1752 to account for 29 February 1700 (Julian)
Source: wikipedia
Adoption in Russia
In Russia the Gregorian calendar was accepted after the October Revolution (so named because it took place in October 1917 in the Julian calendar)
On 24 January 1918 the Council of People's Commissars issued a Decree that Wednesday, 31 January 1918 was to be followed by Thursday, 14 February 1918
Source: wikipedia
Adoption in Eastern Asia
Japan is the first eastern Asia country to adopt the the Gregorian calendar on 1 January 1873
The Republic of China (ROC Taiwan) formally adopted the Gregorian calendar at its founding on 1 January 1912
Source: wikipedia
Adoption timeline
Source: wikipedia
Leap year In the Gregorian calendar
Years evenly divisible by 400 are leap years Years evenly divisible by 100 but not by 400 are not leap
years All years divisible by 4 but not by 100 are leap years All other years are not leap years
We will use the MOD function to determine whether or not a year is evenly divisible by a given number
Simply put
1 year = 365.242199 days We need to add 1 day every 4 years in order to keep
the seasons right But this will create an extra day after 100 years So we subtract 1 day every 100 years But this again cause problems One extra day will be created after 400 years So we subtract 1 day every 400 years
Program codes
Do you understand the if statement here?
Accumulate days using a loop
If it is a leap year, leap_day = 1
The name of the program is doy not day. It means "Day Of Year"
Test cases
Example 4-4 statistical analysis
Implement an algorithm that reads in a set of measurements and calculates the mean and the standard deviation of the input data set, when any value in the data set can be positive, negative, or zero
Ask the user for the number of input values
Flow chart
Codes
Notes concerning the DO loop
The index variable and control parameter of a DO loop should always be of type integer
If a DO loop completes normally, the value of the index variable is undefined when the loop is completed
Program findmaxDeclare variables
max = 0
Beginning of the loopAsk user to enter a number, xIf x is less than 0, leave the loopIf x is greater than max, set x to max
End of the loop
Write out the max. valueend program findmax
Write a Fortran program to find the maximum value
Test Values
3862111857
Encryption and decryption
Pemod n=?
Test P = 115 e = 137 n = 3737
Hint:You don't really have to compute P^e1* 115 mod 3737 = 115115*115 mod 3737 = 20142014*115 mod 3737 = 36533653*115 mod 3737 = 15511551*115 mod 3737 = 2726...