4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Fortran if文 for文 print文 真偽値 PythonからのFortran

4
Posted at

はじめに

前回Fortranを使って簡単なプログラムを作るやり方を紹介しましたが、今回は基本的な文法をつらつら書き並べていこうと思います。

 ちなみに、コンパイル環境がローカルにない場合は、こちらのサイトでコードを試すことができます。

以下で紹介するコードは全て、「!!!!!!!!!!!!!!!!!!!」の部分に書くものとします。

program main
implicit none
!!!!!!!!!!!!!!!!!!!
end program main

print文

fortranでのコメントアウトは「!」です

print *, "My ", "name ", "is ", "hoge" ! My name is hoge

if文

絶対値は組み込みのabs( )を使える

integer x
x = -100
if (abs(x) >= 10) then
    print *, "very big or very small value"
else
    print *, "-10 < x < 10"
end if

for文

Fortranではfor文というよりdo文です

integer i
integer n_iter
real x

n_iter = 10
x = 0

do i=0, n_iter-1
    x = x + i
    print *, x
end do

論理演算

integer x
logical is_true
x = 10
is_true = .false.

! 値の大小
if ((x >= 10) .or. (x <= -10)) then
    print *, "bigger than 10 or smaller than -10"
end if

! true or false
if (is_true) then
    print *, "true dayo!"
else
    print*, "false dayo!"
end if

終わりに

次はサブルーチン(Pythonでのfunction的な扱い?)を使って何かやりたいと思います。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?