Introduction to Fortran

Simple Program Construction in Fortran » Looping Through Arrays

It would be significantly easier to make an array of the pieces of clothing as character variables as well as keeping the wardrobe and outfits available in the integer formats.

 character clothing_types(8) clothing_types = (/ Short-sleeved top, Long-sleeved dress top, T-shirt, Long-sleeved top,
Shorts, Pants, Athletic shorts, Pajama pants /)

Then since the clothing_types, wardrobe, and outfit all have the same number of elements (8), we can loop through one of them and use the element index as a way to call the other arrays data.

However, first, we need to understand looping. Loops are iterations of the exact same process either until a criteria is met, while a criteria is true, or for all of the elements in an array. In Fortran, the most common type of looping structure is

 do var = start, end      expression end do 

where the var in this case would be an iterator that counts from start to end.

Question 1 of 2

If we wanted to loop through our outfit array, what would our start and end values be?

a) Start
b) End

Because our outfit array has 8 elements and Fortran arrays, unless otherwise specified, start at 1, we would start at 1 and end at 8.

Challenge Question

Write a do loop to check your clean wardrobe for any clothing types that you have run out of since selecting your outfit. If you find any clothing types that you have run out of, print out an alert for yourself to do laundry.


 do j = 1, 8      if (wardrobe(j) .EQ. 0) then           print *, “Do laundry today!”      end if end do 

Notice that the print statement is wrapped in double quotes (“). This is a way to tell the program that these words should not be interpreted as code, but just output “as is”.