-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fortran.tex
executable file
·3259 lines (3055 loc) · 145 KB
/
Fortran.tex
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[10pt,t]{beamer}
\include{mypreamble}
%\usefonttheme[onlymath]{serif}
\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
\setsansfont[Ligatures={Common}]{Futura}
\setmonofont[Scale=0.8]{Monaco}
\beamertemplateballitem
\newcolumntype{a}{>{\columncolor{lulime}}c}
\newcolumntype{b}{>{\columncolor{lulime!50}}c}
\newcolumntype{d}{>{\columncolor{lulime!40}}c}
\newcolumntype{e}{>{\columncolor{lulime}}l}
\newcolumntype{f}{>{\columncolor{lulime!50}}l}
\hypersetup{
pdftitle={Modern Fortran}
pdfauthor={Alexander B. Pacheco, Research Computing, Lehigh University}
}
\title{Modern Fortran Programming}
\author[Alex Pacheco]{\large{Alexander~B.~Pacheco}}
\institute{\href{http://researchcomputing.lehigh.edu}{Research Computing}}
\date{June 28, 2021}
\subject{Talks}
\keywords{Lehigh Research Computing Resources, Fortran Programming}
% This is only inserted into the PDF information catalog. Can be left
% out.
% Delete this, if you do not want the table of contents to pop up at
% the beginning of each subsection:
\AtBeginSection[]
{
% \begingroup
% \setbeamertemplate{background canvas}[vertical shading][bottom=lubrown,top=lubrown]
% \setbeamertemplate{footline}[myfootline]
% \setbeamertemplate{section page}[mysection]
% \frame[c]{
% \sectionpage
% }
% \endgroup
\begin{frame}{Outline}
\tableofcontents
\end{frame}
}
% footer logo
\pgfdeclareimage[width=0.15\paperwidth]{university-logo}{lu}
%\trlogo{\pgfuseimage{university-logo}}
\titlegraphic{\includegraphics[scale=0.5]{lu}}
\begin{document}
\frame{\titlepage}
\begin{frame}{Outline}
\tableofcontents
\end{frame}
\section{Introduction}
\begin{frame}{What is Fortran?}
\begin{itemize}
\item Fortran is a general-purpose, imperative programming language that is especially suited to numeric computation and scientific computing.
\item Originally developed by IBM for scientific and engineering applications.
\item The name Fortran is derived from The IBM Mathematical \textbf{For}mula \textbf{Tran}slating System.
\item It was one of the first widely used "high-level" languages, as well as the first programming language to be standardized.
\item It is still the premier language for scientific and engineering computing applications.
\end{itemize}
\end{frame}
\begin{frame}{Many Flavors of Fortran}
\begin{itemize}
\item FORTRAN --- first released by IBM in 1956
\item FORTRAN II --- released by IBM in 1958
\item FORTRAN IV --- released in 1962, standardized
\item FORTRAN 66 --- appeared in 1966 as an ANSI standard
\item FORTRAN 77 --- appeared in 1977, structured features
\item Fortran 90 --- 1992 ANSI standard, free form, modules
\item Fortran 95 --- a few extensions
\item Fortran 2003 --- object oriented programming
\item Fortran 2008 --- a few extensions
\end{itemize}
The correct spelling of Fortran for 1992 ANSI standard and later (sometimes called Modern Fortran) is "Fortran". Older standards are spelled as "FORTRAN".
\end{frame}
\begin{frame}{Why Learn Fortran?}
\begin{itemize}
\item Fortran was designed by, and for, people who wanted raw number crunching speed.
\item There's a great deal of legacy code and numerical libraries written in Fortran,
\item attempts to rewrite that code in a more "stylish" language result in programs that just don't run as fast.
\item Fortran is the primary language for some of the most intensive supercomputing tasks, such as
\begin{itemize}
\item astronomy,
\item weather and climate modeling,
\item numerical linear algebra and libraries,
\item computational engineering (fluid dynamics),
\item computational science (chemistry, biology, physics),
\item computational economics, etc.
\end{itemize}
\item How many of you are handed down Fortran code that you are expected to further develop?
\end{itemize}
\end{frame}
\begin{frame}{Why learn Modern Fortran and not FORTRAN?}
\begin{itemize}
\item FORTRAN is a fixed source format dating back to the use of punch cards.
\item The coding style was very restrictive
\begin{itemize}
\item Max 72 columns in a line with
\item first column reserved for comments indicated by a character such as c or *,
\item the second through fifth columns reserved for statement labels,
\item the sixth column for continuation indicator, and
\item columns 7 through 72 for statements.
\item Variable names can consists of up to 6 alphanumeric characters (a-z,0-9)
\end{itemize}
\item Cannot process arrays as a whole, need to do it element by element.
\item Cannot allocate memory dynamically.
% \item Modern Fortran compilers are backwards compatible.
% \item[] i.e. you can call FORTRAN 77 subroutines and functions from newer code without need for complete rewrite.
\end{itemize}
\end{frame}
\begin{frame}[fragile]{FORTRAN 77 Example}
\begin{eblock}{SAXPY Code}
\lstinputlisting[language={[77]Fortran},basicstyle=\fontsize{6}{5}\selectfont\ttfamily]{./demo/saxpy.f}
\end{eblock}
\end{frame}
\begin{frame}{Why Learn Modern Fortran?}
\begin{itemize}
% \item Free-format source code and many other simple improvements.
% \item Arrays as first-class objects, whole-array expressions, assignments, and functions.
% \item Dynamic memory allocation; pointers to allow complex dynamic data structures to be constructed.
% \item User-defined data types; existing operators can be overloaded (re-defined) or new ones defined.
% \item The MODULE - a new program unit which can encapsulate data and a related set of procedures (subroutines or functions). Can implement classes and member functions for object-oriented programming.
% \item New control structures: SELECT CASE, CYCLE, and EXIT so labels and explicit jumps are rarely needed.
% \item Recursive functions, generic names for procedures, optional arguments, calls with keywords, and many other procedure call options.
\item Free-format source code with a maximum of 132 characters per line,
\item Variable names can consists of up to 31 alphanumeric characters (a-z,0-9) and underscores ( \_ ),
\item Dynamic memory allocation and Ability to operate on arrays (or array sections) as a whole,
% \item End of Line comments,
\item generic names for procedures, optional arguments, calls with keywords, and many other procedure call options,
\item Recursive procedures and Operator overloading,
\item Structured data or derived types,
\item Object Oriented Programming.
\item See \url{http://en.wikipedia.org/wiki/Fortran\#Obsolescence_and_deletions} for obsolete and deleted FORTRAN 77 features in newer standards.
\end{itemize}
\end{frame}
\begin{frame}[fragile]{FORTRAN 90 Example}
\begin{eblock}{SAXPY Code}
\lstinputlisting[language={[90]Fortran},basicstyle=\fontsize{6}{5}\selectfont\ttfamily]{./demo/saxpy.f90}
\end{eblock}
\end{frame}
\begin{frame}{Major Differences with C}
\begin{itemize}
\item \textbf{No standard libraries:} No specific libraries have to be loaded explicitly for I/O and math.
\item \textbf{Implicit type declaration:} In Fortran, variables of type real and integer may be declared implicitly, based on their first letter. {\em This behaviour is not recommended in Modern Fortran}.
\item \textbf{Arrays vs Pointers:} Multi-dimension arrays are supported (arrays in C are one-dimensional) and therefore no vector or array of pointers to rows of a matrices have to be constructed.
\item \textbf{Call by reference:} Parameters in function and subroutine calls are all passed by reference. When a variable from the parameter list is manipulated, the data stored at that address is changed, not the address itself. Therefore there is no reason for referencing and de-referencing of addresses (as commonly seen in C).
\end{itemize}
\end{frame}
\section{Basics}
\begin{frame}[fragile,allowframebreaks]{Fortran Source Code}
\begin{itemize}
\item Fortran source code is in ASCII text and can be written in any plain-text editor such as vi, emacs, etc.
\item For readability and visualization use a text editor capable of syntax highlighting and source code indentation.
\item Fortran source code is case insensitive i.e. PROGRAM is the same as Program.
\item Using mixed case for statements and variables is not considered a good programming practice. Be considerate to your collaborators who will be modifying the code.
\item Some Programmers use uppercase letters for Fortran keywords with rest of the code in lowercase while others (like me) only use lower case letters.
\item Use whatever convention you are comfortable with and be consistent throughout.
\framebreak
\item The general structure of a Fortran program is as follows
\begin{lstlisting}[language={[90]Fortran}]
PROGRAM name
IMPLICIT NONE
[specification part]
[execution part]
[subprogram part]
END PROGRAM name
\end{lstlisting}
\begin{enumerate}
\item A Fortran program starts with the keyword \lstfortran{PROGRAM} followed by program name,
\item This is followed by the \lstfortran{IMPLICIT NONE} statement (avoid use of implicit type declaration in Fortran 90),
\item Followed by specification statements for various type declarations,
\item Followed by the actual execution statements for the program,
\item Any optional subprogram, and lastly
\item The \lstfortran{END PROGRAM} statement
\end{enumerate}
\framebreak
\item A Fortran program consists of one or more program units.
\begin{itemize}
\item \lstfortran{PROGRAM}
\item \lstfortran{SUBROUTINE}
\item \lstfortran{FUNCTION}
\item \lstfortran{MODULE}
\end{itemize}
\item The unit containing the \lstfortran{PROGRAM} attribute is often called the {\em main program} or {\em main}.
\item The main program should begin with the \lstfortran{PROGRAM} keyword. This is however not required, but it's use if highly recommended.
\item A Fortran program should contain only one main program i.e. one \lstfortran{PROGRAM} keyword and can contain one or more subprogram units such as \lstfortran{SUBROUTINE}, \lstfortran{FUNCTION} and \lstfortran{MODULE}.
\item Every program unit, must end with a \lstfortran{END} keyword.
\end{itemize}
\framebreak
\begin{itemize}
\item Basic Character Set:
\begin{itemize}
\item the letters A$\cdots$Z and a$\cdots$z
\item the digits 0$\cdots$9
\item the underscore character (\_)
\item the special characters = \lstinline|: + blank - * / ( ) [ ] , . $ ' ! `` % & ; < > ?|
\end{itemize}
\item Identifier: name used to identify a variable, procedure, or any other user-defined item.
\begin{itemize}
\item cannot be longer than 31 characters
\item must be composed of letters, digits and underscores
\item first character must be a letter
\item case insensitive
\end{itemize}
\end{itemize}
\begin{center}
\fontsize{8}{10}\selectfont{
\begin{tabular}{ababa}
\rowcolor{lublue}\multicolumn{5}{c}{Non I/O Keywords}\\
allocatable & allocate & assign & assignment & block data \\
call & case & character & common & complex \\
contains & continue & cycle & data & deallocate \\
default & do & double precision & else & else if \\
elsewhere & end block data & end do & end function & end if \\
end interface & end module & end program & end select & end subroutine \\
end type & end where & entry & equivalence & exit \\
external & function & go to & if & implicit \\
in & inout & integer & intent & interface \\
intrinsic & kind & len & logical & module \\
namelist & nullify & only & operator & optional \\
out & parameter & pause & pointer & private \\
program & public & real & recursive & result \\
return & save & select case & stop & subroutine \\
target & then & type & type() & use \\
Where & While & & & \\
\rowcolor{lublue}\multicolumn{5}{c}{I/O Keywords}\\
backspace & close & endfile & format & inquire \\
open & print & read & rewind & Write \\
\end{tabular}
}
\end{center}
\end{frame}
\begin{frame}{Simple I/O}
\begin{itemize}
\item Any program needs to be able to read input and write output to be useful and portable.
\item In Fortran, the \lstfortran{print} command provides the most simple form of writing to standard output while,
\item the \lstfortran{read} command provides the most simple form of reading input from standard input
\item \lstfortran{print *, <var1> [, <var2> [, ... ]]}
\item \lstfortran{read *, <var1> [, <var2> [, ... ]]}
\item The $\ast$ indicates that the format of data read/written is unformatted.
\item In later sections, we will cover how to read/write formatted data and file operations.
\item variables to be read or written should be separated by a comma (,).
\end{itemize}
\end{frame}
\begin{frame}{Your first code in Fortran}
\begin{itemize}
\item Open a text editor and create a file helloworld.f90 containing the following lines
\lstinputlisting[language={[90]Fortran}]{./demo/helloworld.f90}
\item The standard extension for Fortran source files is .f90, i.e., the source files are named <name>.f90.
\item The .f extension implies fixed format source or FORTRAN 77 code.
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Compiling Fortran Code}
\begin{itemize}
\item To execute a Fortran program, you need to compile it to obtain an executable.
\item Almost all *NIX system come with GCC compiler installed. You might need to install the Fortran (gfortran) compiler if its not present.
\item Command to compile a fortran program
\item[] \lstinline|<compiler> [flags] [-o executable] <source code>|
\item The [...] is optional. If you do not specify an executable, then the default executable is \lstinline|a.out|
\begin{Verbatim}[fontsize=\fontsize{7}{8}\selectfont,formatcom=\color{indigo}]
altair:Exercise apacheco$ gfortran helloworld.f90
altair:Exercise apacheco$ ./a.out
Hello World!
\end{Verbatim}
\item Other compilers available on our clusters are Intel (ifort) and NVIDIA HPC SDK (nvfortran) compilers.
\item[] \Verb[fontsize=\fontsize{7}{8}\selectfont,formatcom=\color{indigo}]|ifort -o helloworld helloworld.f90; ./helloworld|
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Comments}
\begin{itemize}
\item To improve readability of the code, comments should be used liberally.
\item A comment is identified by an exclamation mark or bang (!), except in a character string.
\item All characters after ! upto the end of line is a comment.
\item Comments can be inline and should not have any Fortran statements following it
\end{itemize}
\begin{eblock}{}
\begin{lstlisting}[language={[90]Fortran}]
program hello
! A simple Hello World code
print *, 'Hello World!' ! Print Hello World to screen
! This is an incorrect comment if you want Hello World to print to screen ! print *, 'Hello World!'
end program hello
\end{lstlisting}
\end{eblock}
\end{frame}
\begin{frame}[fragile]{Fortran Data Types}
\begin{itemize}
\item Fortran provides five intrinsic data types
\begin{itemize}
\item[] \lstfortran{INTEGER}: exact whole numbers
\item[] \lstfortran{REAL}: real, fractional numbers
\item[] \lstfortran{COMPLEX}: complex, fractional numbers
\item[] \lstfortran{LOGICAL}: boolean values
\item[] \lstfortran{CHARACTER}: strings
\end{itemize}
\item and allows users to define additional types.
\item The \lstfortran{REAL} type is a single-precision floating-point number.
\item The \lstfortran{COMPLEX} type consists of two reals (most compilers also provide a \lstfortran{DOUBLE COMPLEX} type).
\item FORTRAN also provides \lstfortran{DOUBLE PRECISION} data type for double precision \lstfortran{REAL}. This is obsolete but is still found in several programs.
\end{itemize}
\end{frame}
\begin{frame}{Explicit and Implicit Typing}
\begin{itemize}
\item For historical reasons, Fortran is capable of implicit typing of variables.
\begin{gather*}
\underbrace{ABCDEFGH}_{REAL}\overbrace{IJKLMN}^{INTEGER}\underbrace{OPQRSTUVWXYZ}_{REAL}
\end{gather*}
\item You might come across old FORTRAN program containing \lstfortran{IMPLICIT REAL*8(a-h,o-z)} or \lstfortran{IMPLICIT DOUBLE PRECISION (a-h,o-z)}.
\item It is highly recommended to explicitly declare all variable and avoid implict typing using the statement \lstfortran{IMPLICIT NONE}.
\item The \lstfortran{IMPLICIT} statement must precede all variable declarations.
\end{itemize}
\end{frame}
\begin{frame}{Variables}
\begin{itemize}
\item Variables are the fundamental building blocks of any program.
\item A variable is nothing but a name given to a storage area that our programs can manipulate.
\item Each variable should have a specific type,
\begin{itemize}
\item which determines the size and layout of the variable's memory;
\item the range of values that can be stored within that memory; and
\item the set of operations that can be applied to the variable.
\end{itemize}
\item A variable name may consist of up to 31 alphanumeric characters and underscores, of which the first character must be a letter.
\item Names must begin with a letter and should not contain a space.
\item Allowed names: a, compute\_force, qed123
\item Invalid names: 1a, a thing, \$sign
\end{itemize}
\begin{center}
\begin{tabular}{ll}
\rowcolor{lublue}Type & Description \\
\rowcolor{lulime!50}Integer & It can hold only integer values. \\
\rowcolor{lulime}Real & It stores the floating point numbers. \\
\rowcolor{lulime!50}Complex & It is used for storing complex numbers. \\
\rowcolor{lulime}Logical & It stores logical Boolean values. \\
\rowcolor{lulime!50 }Character & It stores characters or strings. \\
\end{tabular}
\end{center}
\end{frame}
\begin{frame}[fragile]{Constants}
\begin{itemize}
\item The constants refer to the fixed values that the program cannot alter during its execution.
\item Constants can be of any of the basic data types
\item Literal Constants: has a value but no name
\begin{center}
\begin{tabular}{ab}
\rowcolor{lublue}Type & Example \\
Integer constants & 0 1 -1 300 123456789 \\
Real constants & 0.0 1.0 -1.0 123.456 7.1E+10 -52.715E-30 \\
Complex constants & (0.0, 0.0) (-123.456E+30, 987.654E-29) \\
Logical constants & .true. .false. \\
Character constants & "PQR" "a" "\lstinline|23'abc$%#@!|" \\
\end{tabular}
\end{center}
\item Named Constants:
\begin{itemize}
\item has a value as well as a name.
\item should be declared at the beginning of a program or procedure, indicating its name and type.
\item are declared with the parameter attribute
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile,allowframebreaks]{Variable Declarations}
\begin{itemize}
\item Variables must be declared before they can be used.
\item In Fortran, variable declarations must precede all executable statements.
\item To declare a variable, preface its name by its type.
\item[] \lstfortran{TYPE Variable}
\item A double colon may follow the type.
\item[] \lstfortran{TYPE[, attributes] :: Variable}
\item This is the new form and is recommended for all declarations. If attributes need to be added to the type, the double colon format must be used.
\item A variable can be assigned a value at its declaration.
\framebreak
\item \textbf{Numeric Variables:}
\begin{lstlisting}[language={[90]Fortran}]
INTEGER :: i, j = 2
REAL :: a, b = 4.d0
COMPLEX :: x, y
\end{lstlisting}
\item In the above examples, the value of j and b are set at compile time and can be changed later.
\item If you want the assigned value to be constant that cannot change subsequently, add the attribute \lstfortran{PARAMETER}
\begin{lstlisting}[language={[90]Fortran}]
INTEGER, PARAMETER :: j = 2
REAL, PARAMETER :: pi = 3.14159265
COMPLEX, PARAMETER :: ci = (0.d0,1.d0)
\end{lstlisting}
\item \textbf{Logical:} Logical variables are declared with the \lstfortran{LOGICAL} keyword
\begin{lstlisting}[language={[90]Fortran}]
LOGICAL :: l, flag=.true.
\end{lstlisting}
\framebreak
\item \textbf{Character:} Character variables are declared with the \lstfortran{CHARACTER} type; the length is supplied via the keyword \lstfortran{LEN}.
\item The length is the maximum number of characters (including space) that will be stored in the character variable.
\item If the \lstfortran{LEN} keyword is not specified, then by default \lstfortran{LEN=1} and only the first character is saved in memory.
\begin{lstlisting}[language={[90]Fortran}]
CHARACTER :: ans = 'yes' ! stored as y not yes
CHARACTER(LEN=10) :: a
\end{lstlisting}
\item FORTRAN programmers: avoid the use of \lstfortran{CHARACTER*10} notation.
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Array Variables}
\begin{itemize}
\item Arrays (or matrices) hold a collection of different values at the same time.
\item Individual elements are accessed by subscripting the array.
\item Arrays are declared by adding the \lstfortran{DIMENSION} attribute to the variable type declaration which can be integer, real, complex or character.
\item Usage: \lstfortran{TYPE, DIMENSION(lbound:ubound) :: variable_name}
\item[] Lower bounds of one can be omitted
\begin{lstlisting}[language={[90]Fortran}]
INTEGER, DIMENSION(1:106) :: atomic_number
REAL, DIMENSION(3, 0:5, -10:10) :: values
CHARACTER(LEN=3), DIMENSION(12) :: months
\end{lstlisting}
\item In Fortran, arrays can have upto seven dimension.
\item In contrast to C/C++, Fortran arrays are column major.
\item We'll discuss arrays in more details later.
\end{itemize}
\end{frame}
\begin{frame}{DATA Statments}
\begin{itemize}
\item In FORTRAN, a \lstfortran{DATA} statement may be used to initialize a variable or group of variables.
\item It causes the compiler to load the initial values into the variables at compile time i.e. a nonexecutable statment
\item General form
\item[] \lstfortran{DATA varlist /varlist/ [, varlist /varlist/]}
\item[] Example \lstfortran{DATA a,b,c /1.,2.,3./}
\item \lstfortran{DATA} statements can be used in Fortran but it is recommended to to eliminate this statement by initializing variables in their declarations.
\item In Fortran 2003, variables may be initialized with intrinsic functions (some compilers enable this in Fortran 95)
\item[] \lstfortran{REAL, PARAMETER :: pi = 4.0*atan(1.0)}
\end{itemize}
\end{frame}
\begin{frame}[fragile,allowframebreaks]{KIND Parameter}
\begin{itemize}
\item In FORTRAN, types could be specified with the number of bytes to be used for storing the value:
\begin{itemize}
\item \lstfortran{real*4} - uses 4 bytes, roughly $\pm10^{-38}$ to $\pm10^{38}$.
\item \lstfortran{real*8} - uses 8 bytes, roughly $\pm10^{-308}$ to $\pm10^{308}$.
\item \lstfortran{complex*16} - uses 16 bytes, which is two \lstfortran{real*8} numbers.
\end{itemize}
\item Fortran 90 introduced \lstfortran{kind} parameters to parameterize the selection of different possible machine representations for each intrinsic data types.
\item The \lstfortran{kind} parameter is an integer which is processor dependent.
\item There are only 2(3) kinds of reals: 4-byte, 8-byte (and 16-byte), respectively known as single, double (and quadruple) precision.
\item The corresponding \lstfortran{kind} numbers are 4, 8 and 16 (most compilers)
\end{itemize}
\begin{minipage}{\textwidth}
\begin{center}
\begin{tabular}{abb}
\rowcolor{lublue}KIND & Size (Bytes) & Data Type \\
1 & 1 & integer, logical, character (default) \\
2 & 2 & integer, logical \\
4\footnote{\tiny default for all data types except character} & 4 & integer, real, logical, complex \\
8 & 8 & integer, real, logical, complex \\
16 & 16 & real, complex \\
\end{tabular}
\end{center}
\end{minipage}
\begin{itemize}
\item You might come across FORTRAN codes with variable declarations using \lstfortran{integer*4}, \lstfortran{real*8} and \lstfortran{complex*16} corresponding to \lstfortran{kind=4} (integer) and \lstfortran{kind=8} (real and complex).
\item The value of the \lstfortran{kind} parameter is usually not the number of decimal digits of precision or range; on many systems, it is the number of bytes used to represent the value.
\item The intrinsic functions \lstfortran{selected\_int\_kind} and \lstfortran{selected\_real\_kind} may be used to select an appropriate \lstfortran{kind} for a variable or named constant.
\item \lstfortran{selected_int_kind(R)} returns the kind value of the smallest integer type that can represent all values ranging from $-10^R$ (exclusive) to $10^R$ (exclusive)
\item \lstfortran{selected\_real\_kind(P,R)} returns the kind value of a real data type with decimal precision of at least P digits, exponent range of at least R. At least one of P and R must be specified, default R is 308.
\end{itemize}
\lstinputlisting[language={[90]Fortran},basicstyle=\fontsize{6}{7}\selectfont\ttfamily]{./demo/kindfns.f90}
\begin{Verbatim}[fontsize=\fontsize{5}{6}\selectfont,formatcom=\color{indigo}]
[apacheco@qb4 Exercise] ./kindfns
Kind of i = 4 with range = 9
Kind of j = 8 with range = 18
Kind of k = 8 with range = 18
Kind of real a = 4 with precision = 6 and range = 37
Kind of real b = 8 with precision = 15 and range = 307
Kind of real c = 8 with precision = 15 and range = 307
\end{Verbatim}
\end{frame}
\begin{frame}{Operators}
Fortran defines a number of operations on each data type.
\begin{columns}[t]
\column{0.4\textwidth}
\begin{eblock}{\scriptsize Arithmetic Operators}
\begin{itemize}
\footnotesize
\item[+]: addition
\item[-]: subtraction
\item[*]: multiplication
\item[/]: division
\item[**]: exponentiation
\end{itemize}
\end{eblock}
\column{0.4\textwidth}
\begin{eblock}{\scriptsize Relational Operators (FORTRAN versions)}
\begin{itemize}
\footnotesize
\item[==]: equal to (.eq.)
\item[/=]: not equal to (.ne.)
\item[<]: less than (.lt.)
\item[<=]: less than or equal to (.le.)
\item[>]: greater than (.gt.)
\item[>=]: greater than or equal to (.ge.)
\end{itemize}
\end{eblock}
\end{columns}
\begin{columns}[t]
\column{0.4\textwidth}
\begin{eblock}{\scriptsize Logical Expressions}
\begin{itemize}
\footnotesize
\item[]{\color{lubrown}.AND.} intersection
\item[]{\color{lubrown}.OR.} union
\item[]{\color{lubrown}.NOT.} negation
\item[]{\color{lubrown}.EQV.} logical equivalence
\item[]{\color{lubrown}.NEQV.} exclusive or
\end{itemize}
\end{eblock}
\column{0.4\textwidth}
\begin{eblock}{\scriptsize Character Operators}
\begin{itemize}
\footnotesize
\item[//]: concatenation
\end{itemize}
\end{eblock}
\end{columns}
\end{frame}
\begin{frame}{Operator Evaluations}
\begin{itemize}
\item In Fortran, all operator evaluations on variables is carried out from left-to-right.
\item Arithmetic operators have a highest precedence while logical operators have the lowest precedence
\item The order of operator precedence can be changed using parenthesis, '(' and ')'
\item In Fortran, a user can define his/her own operators.
\item User defined monadic operator has a higher precedence than arithmetic operators, while
\item dyadic operators has a lowest precedence than logical operators.
\end{itemize}
\end{frame}
\begin{frame}{ Operator Precedence}
\begin{center}
\begin{tabular}{aba}
\rowcolor{lublue}Operator & Precedence & Example \\
expression in () & Highest & (a+b) \\
user-defined monadic & - & .inverse.a \\
** & - & 10**4 \\
* or / & - & 10*20 \\
monadic + or - & - & -5 \\
dyadic + or - & - & 1+5 \\
// & - & str1//str2 \\
relational operators & - & a > b \\
.not. & - & .not.allocated(a) \\
.and. & - & a.and.b \\
.or. & - & a.or.b \\
.eqv. or .neqv. & - & a.eqv.b \\
user defined dyadic & Lowest & x.dot.y\\
\end{tabular}
\end{center}
\end{frame}
\begin{frame}[fragile]{Expressions}
\begin{itemize}
\item An expression is a combination of one or more operands, zero or more operators, and zero or more pairs of parentheses.
\item There are three kinds of expressions:
\begin{itemize}
\item An arithmetic expression evaluates to a single arithmetic value.
\item A character expression evaluates to a single value of type character.
\item A logical or relational expression evaluates to a single logical value.
\end{itemize}
\item Examples:
\end{itemize}
\begin{lstlisting}
x + 1.0
97.4d0
sin(y)
x*aimag(cos(z+w))
a .and. b
'AB' // 'wxy'
\end{lstlisting}
\end{frame}
\begin{frame}[fragile, allowframebreaks]{Statements}
\begin{itemize}
\item A statement is a complete instruction.
\item Statements may be classified into two types: executable and non-executable.
\item Non-executable statements are those that the compiler uses to determine various fixed parameters such as module use statements, variable declarations, function interfaces, and data loaded at compile time.
\item Executable statements are those which are executed at runtime.
\item A statements is normally terminated by the end-of-line marker.
\item If a statement is too long, it may be continued by the ending the line with an ampersand (\&).
\item Max number of characters (including spaces) in a line is 132 though it's standard practice to have a line with up to 80 characters. This makes it easier for file editors to display code or print code on paper for reading.
\item Multiple statements can be written on the same line provided the statements are separated by a semicolon.
\item Examples:
\begin{lstlisting}[language={[90]Fortran}]
force = 0d0 ; pener = 0d0
do k = 1, 3
r(k) = coord(i,k) - coord(j,k)
\end{lstlisting}
\item Assignment statements assign an expression to a quantity using the equals sign (=)
\item The left hand side of the assignment statement must contain a single variable.
\item $x + 1.0 = y$ is not a valid assignment statement.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Intrinsic Functions}
\begin{itemize}
\item Fortran provide a large set of intrinsic functions to implement a wide range of mathematical operations.
\item In FORTRAN code, you may come across intrinsic functions which are prefixed with \texttt{i} for integer variables, \texttt{d} for double precision, \texttt{c} for complex single precision and \texttt{cd} for complex double precision variables.
\item In Modern Fortran, these functions are overloaded, i.e. they can carry out different operations depending on the data type.
\item For example: the \lstfortran{abs} function equates to $\sqrt{a^2}$ for integer and real numbers and $\sqrt{\Re^2 + \Im^2}$ for complex numbers.
\end{itemize}
\end{frame}
\begin{frame}{Arithmetic Functions}
\footnotesize
\begin{minipage}{\textwidth}
\begin{center}
\begin{tabular}{abd}
\rowcolor{lublue}Function & Action & Example \\
INT & conversion to integer & J=INT(X) \\
REAL & conversion to real & X=REAL(J) \\
& return real part of complex number & X=REAL(Z) \\
DBLE\footnote{\tiny use real(x,kind=8) instead} & convert to double precision & X=DBLE(J) \\
CMPLX & conversion to complex & A=CMPLX(X[,Y]) \\
AIMAG & return imaginary part of complex number & Y=AIMAG(Z) \\
ABS & absolute value & Y=ABS(X) \\
MOD & remainder when I divided by J & K=MOD(I,J) \\
CEILING & smallest integer $\ge$ to argument & I=CEILING(a) \\
FLOOR & largest integer $\le$ to argument & I=FLOOR(a) \\
MAX & maximum of list of arguments & A=MAX(C,D) \\
MIN & minimum of list of arguments & A=MIN(C,D) \\
SQRT & square root & Y=SQRT(X) \\
EXP & exponentiation & Y=EXP(X) \\
LOG & natural logarithm & Y=LOG(X) \\
LOG10 & logarithm to base 10 & Y=LOG10(X) \\
\end{tabular}
\end{center}
\end{minipage}
\end{frame}
\begin{frame}{Trignometric Functions}
\footnotesize
\begin{center}
\begin{tabular}{abd}
\rowcolor{lublue}Function & Action & Example \\
SIN & sine & X=SIN(Y) \\
COS & cosine & X=COS(Y) \\
TAN & tangent & X=TAN(Y) \\
ASIN & arcsine & X=ASIN(Y) \\
ACOS & arccosine & X=ACOS(Y) \\
ATAN & arctangent & X=ATAN(Y) \\
ATAN2 & arctangent(a/b) & X=ATAN2(A,B) \\
SINH & hyperbolic sine & X=SINH(Y) \\
COSH & hyperbolic cosine & X=COSH(Y) \\
TANH & hyperbolic tangent & X=TANH(Y) \\
\end{tabular}
{\tiny hyperbolic functions are not defined for complex argument}
\end{center}
\end{frame}
\begin{frame}{Character Functions}
\footnotesize
\begin{center}
\begin{tabular}{ef}
\rowcolor{lublue}Function & Description \\
len(c) & length \\
len\_trim(c) & length of c if it were trimmed \\
lge(s1,s2) & returns .true. if s1 follows or is equal to s2 in lexical order \\
lgt(s1,s2) & returns .true. if s1 follows s1 in lexical order \\
lle(s1,s2) & returns .true. if s2 follows or is equal to s1 in lexical order \\
llt(s1,s2) & returns .true. if s2 follows s1 in lexical order \\
adjustl(s) & returns string with leading blanks removed and \\
& \quad same number of trailing blanks added \\
adjustr(s) & returns string with trailing blanks removed and \\
& \quad same number of leading blanks added \\
repeat(s,n) & concatenates string s to itself n times \\
scan(s,c) & returns the integer starting position of string c within string s \\
trim(c) & trim trailing blanks from c \\
\end{tabular}
\end{center}
\end{frame}
\begin{frame}<0>{Array Intrinsic Functions}
\footnotesize
\begin{description}
\item[{size(x[,n])}] The size of x (along the $n^{th}$ dimension, optional)
\item[{sum(x[,n])}] The sum of all elements of x (along the $n^{th}$ dimension, optional)
\item[] $sum(x) = \sum_{i,j,k,\cdots}x_{i,j,k,\cdots}$
\item[{product(x[,n])}] The product of all elements of x (along the $n^{th}$ dimension, optional)
\item[] $prod(x) = \prod_{i,j,k,\cdots}x_{i,j,k,\cdots}$
\item[transpose(x)] Transpose of array x: $ x_{i,j}\Rightarrow x_{j,i}$
\item[dot\_product(x,y)] Dot Product of arrays x and y: $ \sum_{i} x_i* y_i $
\item[matmul(x,y)] Matrix Multiplication of arrays x and y which can be 1 or 2 dimensional arrays: $ z_{i,j} = \sum_k x_{i,k} * y_{k,j}$
\item[conjg(x)] Returns the conjugate of x: $ a + \imath b \Rightarrow a - \imath b$
\end{description}
\end{frame}
\begin{frame}[fragile]
\frametitle{Simple Temperature Conversion Problem}
\begin{itemize}
\item A simple program that
\begin{enumerate}
\item Converts temperature from celsius to fahrenheit
\item Converts temperature from fahrenheit to celsius
\end{enumerate}
\begin{columns}[t]
\column{0.4\textwidth}
\lstinputlisting[language={[90]Fortran},basicstyle=\fontsize{6}{5}\selectfont\ttfamily]{./demo/simple.f90}
\column{0.6\textwidth}
\begin{lstlisting}[basicstyle=\fontsize{6}{5}\selectfont\ttfamily]
altair:Exercise apacheco$ gfortran simple.f90
altair:Exercise apacheco$ ./a.out
10C = 42.0000000 F
40F = 0.00000000 C
\end{lstlisting}
\end{columns}
\item So what went wrong? $10C = 50F$ and $40F = 4.4C$
\end{itemize}
\end{frame}
\begin{frame}[fragile,allowframebreaks]{Type Conversion}
\begin{itemize}
\item In computer programming, operations on variables and constants return a result of the same type.
\item In the temperature code, $9/5=1$ and $5/9=0$. Division between integers is an integer with the fractional part truncated.
\item In the case of operations between mixed variable types, the variable with lower rank is promoted to the highest rank type.
\end{itemize}
\begin{center}
\footnotesize
\begin{tabular}{abb}
\rowcolor{lublue}Variable 1 & Variable 2 & Result \\
Integer & Real & Real \\
Integer & Complex & Complex \\
Real & Double Precision & Double Precision \\
Real & Complex & Complex\\
\end{tabular}
\end{center}
\framebreak
\begin{itemize}
\item As a programmer, you need to make sure that the expressions take type conversion into account
\begin{columns}[t]
\column{0.4\textwidth}
\lstinputlisting[language={[90]Fortran},basicstyle=\fontsize{6}{5}\selectfont\ttfamily]{./demo/temp.f90}
\column{0.6\textwidth}
\begin{lstlisting}[basicstyle=\fontsize{6}{5}\selectfont\ttfamily]
altair:Exercise apacheco$ gfortran temp.f90
altair:Exercise apacheco$ ./a.out
10C = 50.0000000 F
40F = 4.44444466 C
\end{lstlisting}
\end{columns}
\item The above example is not a good programming practice.
\item 10, 40 and 32 should be written as real numbers (10., 40. and 32.) to stay consistent.
\end{itemize}
\end{frame}
\section{Control Constructs}
\begin{frame}[fragile] {Control Constructs}
\begin{itemize}
\item A Fortran program is executed sequentially
\begin{lstlisting}[language={[90]Fortran},mathescape]
program somename
variable declarations
statement 1
statement 2
$\cdots$
end program somename
\end{lstlisting}
\item Control Constructs change the sequential execution order of the program
\begin{enumerate} %\itemsep1pt \parskip0pt \parsep0pt
\item Conditionals: \lstfortran{IF}
\item Loops: \lstfortran{DO}
\item Switches: \lstfortran{SELECT/CASE}
\item Branches: \lstfortran{GOTO} (obsolete in Fortran 95/2003, use CASE instead)
\end{enumerate}
\end{itemize}
\end{frame}
\begin{frame}[fragile]{If Statement}
\begin{itemize}
\item The general form of the \lstfortran{if} statement
\item[] \lstfortran{if ( expression ) statement}
\item When the \lstfortran{if} statement is executed, the logical expression is evaluated.
\item If the result is true, the statement following the logical expression is executed; otherwise, it is not executed.
\item The statement following the logical expression \textbf{cannot} be another \lstfortran{if} statement. Use the \lstfortran{if-then-else} construct instead.
\item[] \lstfortran{if (value < 0) value = 0 }
\end{itemize}
\end{frame}
\begin{frame}[fragile,allowframebreaks]{If-then-else Construct}
\begin{itemize}
\item The \lstfortran{if-then-else} construct permits the selection of one of a number of blocks during execution of a program
\item The \lstfortran{if-then} statement is executed by evaluating the logical expression.
\item If it is true, the block of statements following it are executed. Execution of this block completes the execution of the entire \lstfortran{if} construct.
\item If the logical expression is false, the next matching \lstfortran{else if}, \lstfortran{else} or \lstfortran{end if} statement following the block is executed.
\begin{lstlisting}[language={[90]Fortran},mathescape,basicstyle=\fontsize{6}{5}\selectfont\ttfamily]
if ( expression 1) then
executable statements
else if ( expression 2 ) then
executable statements
else if $\cdots$
$\vdots$
else
executable statements
end if
\end{lstlisting}
\framebreak
\item Examples:
\begin{lstlisting}[language={[90]Fortran},basicstyle=\fontsize{6}{5}\selectfont\ttfamily]
if ( x < 50 ) then
GRADE = 'F'
else if ( x >= 50 .and. x < 60 ) then
GRADE = 'D'
else if ( x >= 60 .and. x < 70 ) then
GRADE = 'C'
else if ( x >= 70 .and. x < 80 ) then
GRADE = 'B'
else
GRADE = 'A'
end if
\end{lstlisting}
\item The \lstfortran{else if} and \lstfortran{else} statements and blocks may be omitted.
\item If \lstfortran{else} is missing and none of the logical expressions are true, the \lstfortran{if-then-else} construct has no effect.
\item The \lstfortran{end if} statement must not be omitted.
\item The \lstfortran{if-then-else} construct can be nested and named.
\end{itemize}
% \framebreak
\begin{columns}
\column{6cm}
\vspace{-0.5cm}
\begin{block}{\scriptsize no \bftt{else if}}
\begin{lstlisting}[language={[90]Fortran},basicstyle=\fontsize{6}{5}\selectfont\ttfamily]
[outer_name:] if ( expression ) then
executable statements
else
executable statements
[inner_name:] if ( expression ) then
executable statements
end if [inner_name]
end if [outer_name]
\end{lstlisting}
\end{block}
\column{5cm}
\vspace{-0.5cm}
\begin{block}{\scriptsize no \bftt{else}}
\begin{lstlisting}[language={[90]Fortran},basicstyle=\fontsize{6}{5}\selectfont\ttfamily]
if ( expression ) then
executable statements
else if ( expression ) then
executable statements
else if ( expression ) then
executable statements
end if
\end{lstlisting}
\end{block}
\end{columns}
\end{frame}
\begin{frame}[fragile,allowframebreaks]{Case Construct}
\begin{itemize}
\item The \lstfortran{case} construct permits selection of one of a number of different block of instructions.
\item The value of the expression in the \lstfortran{select case} should be an integer or a character string.
\begin{lstlisting}[language={[90]Fortran},basicstyle=\fontsize{6}{7}\selectfont\ttfamily]
[case_name:] select case ( expression )
case ( selector )
executable statement
case ( selector )
executable statement
case default
executable statement
end select [case_name]
\end{lstlisting}
\item The \lstfortran{selector} in each \lstfortran{case} statement is a list of items, where each item is either a single constant or a range of the same type as the expression in the \lstfortran{select case} statement.
\item A range is two constants separated by a colon and stands for all the values between and including the two values.
\item The \lstfortran{case default} statement and its block are optional.
\item The \lstfortran{select case} statement is executed as follows:
\begin{enumerate}
\item Compare the value of expression with the case selector in each case. If a match is found, execute the following block of statements.
\item If no match is found and a \lstfortran{case default} exists, then execute those block of statements.
\end{enumerate}
\end{itemize}
\begin{ablock}{Notes}
\begin{itemize}
\item The values in selector must be unique.
\item Use \lstfortran{case default} when possible, since it ensures that there is something to do in case of error or if no match is found.
\item \lstfortran{case default} can be anywhere in the \lstfortran{select case} construct. The preferred location is the last location in the \lstfortran{case} list.
\end{itemize}
\end{ablock}
\framebreak
\begin{itemize}
\item Example for character case selector
\begin{lstlisting}[language={[90]Fortran},basicstyle=\fontsize{6}{7}\selectfont\ttfamily]
select case ( traffic_light )
case ( "red" )
print *, "Stop"
case ( "yellow" )
print *, "Caution"
case ( "green" )
print *, "Go"
case default
print *, "Illegal value: ", traffic_light
end select
\end{lstlisting}
\item Example for integer case selector
\begin{lstlisting}[language={[90]Fortran},basicstyle=\fontsize{6}{7}\selectfont\ttfamily]
select case ( score )
case ( 50 : 59 )
GRADE = "D"
case ( 60 : 69 )
GRADE = "C"
case ( 70 : 79 )
GRADE = "B"
case ( 80 : )
GRADE = "A"
case default
GRADE = "F"
end select
\end{lstlisting}
\end{itemize}
\end{frame}
\begin{frame}[fragile,allowframebreaks]{Do Construct}
\begin{itemize}
\item The looping construct in fortran is the \lstfortran{do} construct.
\item The block of statements called the loop body or \lstfortran{do} construct body is executed repeatedly as indicated by loop control.
\item A \lstfortran{do} construct may have a construct name on its first statement
\begin{lstlisting}[language={[90]Fortran},basicstyle=\fontsize{6}{7}\selectfont\ttfamily]
[do_name:] do loop_control
execution statements
end do [do_name]
\end{lstlisting}
\item There are two types of loop control:
\begin{enumerate}
\item Counting: a variable takes on a progression of integer values until some limit is reached.
\begin{itemize}
\item[$\vardiamond$] \textit{variable = start, end[, stride] }
\item[$\vardiamond$] \textit{stride} may be positive or negative integer, default is 1 which can be omitted.
\end{itemize}
\item General: a loop control is missing
\end{enumerate}
\item Before a \lstfortran{do} loop starts, the expression \textit{start, end} and \textit{stride} are evaluated. These values are not re-evaluated during the execution of the \lstfortran{do} loop.
\item \textit{stride} cannot be zero.
\item If \textit{stride} is positive, this \lstfortran{do} counts up.
\begin{enumerate}
\scriptsize
\item The \textit{variable} is set to \textit{start}
\item If \textit{variable} is less than or equal to \textit{end}, the block of statements is executed.
\item Then, \textit{stride} is added to \textit{variable} and the new \textit{variable} is compared to \textit{end}
\item If the value of \textit{variable} is greater than \textit{end}, the \lstfortran{do} loop completes, else repeat steps 2 and 3
\end{enumerate}
\item If \textit{stride} is negative, this \lstfortran{do} counts down.
\begin{enumerate}
\scriptsize
\item The \textit{variable} is set to \textit{start}
\item If \textit{variable} is greater than or equal to \textit{end}, the block of statements is executed.
\item Then, \textit{stride} is added to \textit{variable} and the new \textit{variable} is compared to \textit{end}
\item If the value of \textit{variable} is less than \textit{end}, the \lstfortran{do} loop completes, else repeat steps 2 and 3
\end{enumerate}
\end{itemize}
\end{frame}
\begin{frame}[fragile,allowframebreaks]{Do Construct: Nested}
\begin{itemize}
\item The \lstfortran{exit} statement causes termination of execution of a loop.
\item If the keyword \lstfortran{exit} is followed by the name of a do construct, that named loop (and all active loops nested within it) is exited and statements following the named loop is executed.
\item The \lstfortran{cycle } statement causes termination of the execution of \textit{one iteration} of a loop.