diff --git a/Fortran/HelloWorld.f90 b/Fortran/HelloWorld.f90 new file mode 100644 index 0000000..39ca4e4 --- /dev/null +++ b/Fortran/HelloWorld.f90 @@ -0,0 +1,4 @@ + ! | Like ... in XML +program hello ! Start the program | + print *, "Hello, World!" ! Print "Hello, World!" | Hello, World +end program hello ! End the program | diff --git a/Fortran/IO.f90 b/Fortran/IO.f90 new file mode 100644 index 0000000..3f38cc8 --- /dev/null +++ b/Fortran/IO.f90 @@ -0,0 +1,43 @@ +program io_example + CHARACTER*10 :: input + CHARACTER*20 :: file_input + ! + ! Output + ! + + ! The print command is used to print text on screen. + print *, "Hello, World!" + ! write(*,*) is equivilent + write (*,*) "Hello, World!" + + ! File IO + ! Open a file descriptor 20 for my.txt + ! NOTE: usually you would have a constant for it, but you don't have to. + open (unit=20, file="my.txt", action="write", status="replace") + ! Write to it + write (20,*) "Hello, my.txt!" + ! Close it + close (20) + ! (that was basically a fopen(3), fwrite(3) and fclose(3)) + + ! + ! Input + ! + + ! Get input from standard input + read (*, *) input + ! Spit it out to standard output + print *, input + + ! File IO + ! Open a file descriptor 12 for my.txt + open (unit=12, file="my.txt", action="read", status="old") + ! Read it + ! (the '(A)' makes it read the whole line) + read (12,'(A)') file_input + ! Spit it out to stdout + print *, file_input + ! Close it + close (12) + ! (aka fopen(3), fread(3) and fclose(3)) +end program io_example diff --git a/Fortran/Logic.f90 b/Fortran/Logic.f90 new file mode 100644 index 0000000..87be5d6 --- /dev/null +++ b/Fortran/Logic.f90 @@ -0,0 +1,65 @@ +program logic_example + ! Variables + LOGICAL :: myVar + ! Integer(16) + INTEGER (16) :: myInt = 4, myNewInt + + ! + ! if statement + ! + myVar = .TRUE. + + if (myVar) then + print *, "It works!" + end if + + ! + ! Comparison Operators + ! + if (myInt .GT. 1) then ! or if (myInt > 1) then... + print *, "It works!" + end if + + if (myInt .LT. 5) then ! or if (myInt < 5) then... + print *, "It works!" + end if + + if (myInt .GE. 4 .AND. myInt .LE. 4) then ! or if (myInt >= 4 .AND. myInt <= 4) then... + print *, "It works!" + end if + + if (myInt .EQ. 4 .AND. myInt .NE. 3) then ! or if (myInt == 4 .AND. myInt /= 3) then... + print *, "It works!" + end if + + ! + ! select case () [switch] + ! + select case (myInt) + case (1) + print *, "Nope!" + case (2) + print *, "Nope!" + case (3) + print *, "Nope!" + case (4) + print *, "We have a winner!" + case default + print *, "Nooope!" + end select + + ! + ! do loop [for-like] + ! + do myInt=0, 20 + print *, myInt ** 2 + end do + + ! Can be nested + do myInt=1, 20 + ! Third argument means count by twos + do myNewInt=1, 10, 2 + print *, myInt ** myNewInt + end do + end do +end program logic_example diff --git a/Fortran/Math.f90 b/Fortran/Math.f90 new file mode 100644 index 0000000..e0e454c --- /dev/null +++ b/Fortran/Math.f90 @@ -0,0 +1,20 @@ +program math_example + ! Variables + INTEGER :: a, s, d, m + REAL :: dd + DOUBLE PRECISION :: dpd + + a = 1 + 1 ! Set a to 1+1 + s = 2 - 1 ! Set s to 2-1 + d = 2 / 3 ! Set d to 2/3 or 0 (round down) + dd= 2.0 / 3.0 ! Set dd to 2/3 or 0.666666687 + dpd=2.0 / 3.0 ! Set dpd to 2/3 or 0.66666668653488159 + m = 2 * 2 ! Set m to 2*2 + + print *,a + print *,s + print *,d + print *,dd + print *,dpd + print *,m +end program math_example diff --git a/Fortran/README.md b/Fortran/README.md new file mode 100644 index 0000000..2cf63cc --- /dev/null +++ b/Fortran/README.md @@ -0,0 +1,13 @@ +# Fortran +> Fortran is a programming language mainly used by the scientific community. Its name is a contraction of FORmula TRANslation, and its aim is to provide a way to tell computers to calculate complicated mathematical expressions, with more ease than assembly language. +> +> ~[WikiBooks](https://wikibooks.org/wiki/Fortran) +> +Fortran is a language originally made in 1957, and since updated in 1958, 1962, 1966, 1977, 1991, 2003 and finally 2008. It was one of the first programming languages for an IBM computer. + + +> ### Note +> I'm going to be using Fortran 90, from 1991. +> +> Also, Fortran is case-insensitive, but I will be using lowercase. + diff --git a/Fortran/Variables.f90 b/Fortran/Variables.f90 new file mode 100644 index 0000000..220e5de --- /dev/null +++ b/Fortran/Variables.f90 @@ -0,0 +1,37 @@ +program variable_example + ! + ! Integers, strings and booleans + ! + INTEGER :: myNumber = 0 ! Declare an integer, with value 0 + INTEGER (16) :: myBigNumber ! Declare a 8-byte (64-bit) integer + INTEGER (1) :: myTinyNumber = 9 ! Fix a compile error + LOGICAL :: myBoolean = .TRUE. ! Declare a boolean + CHARACTER*3 :: myWord = "Pig" ! Declare a word myWord to Pig + ! Note that all variables by default are + ! mutable. For constant, try ,PARAMETER + LOGICAL, PARAMETER :: myConstantBoolean = .FALSE. ! A constant boolean. + + REAL :: myFloat = 3.1415 ! Declare a REAL, aka a float + DOUBLE PRECISION :: myDouble = 3.141526 ! Declare a DOUBLE PRECISION, aka a double + + ! + ! Reassignment + ! + myInteger = 1 ! Change myInteger from 0 to 1 + myBigNumber = 11**myTinyNumber ! Set myBigNumber to 11^9 (overflows to negative) + myBoolean = .FALSE. ! Change myBoolean from .TRUE. to .FALSE. + myWord = "Dog" + + !myConstantBoolean = .TRUE. ! This will not compile, as myConstantBoolean is constant! + + myWord = "Food!" ! This does compile, but myWord only keeps Foo + + ! Output the values + print *, myNumber ! => 0 + print *, myBigNumber ! => + print *, myBoolean ! => F + print *, myWord ! => Foo + print *, myConstantBoolean !=> F + print *, myFloat ! => 3.14150000 + print *, myDouble ! => 3.1415259838104248 +end program variable_example