このレッスンの内容

順番に足して合計を出す

合計をゼロから始めて、数を一つ読むたびに足していきます。

簡単な例

最初の状態2 + 4
結果6
lesson.jl
function main()
    total = 0
    total += 2
    total += 4
    println(total)
end

main()
出力
6
広告

身近な例

三つのクラスから、それぞれ 5 冊、2 冊、4 冊の本が寄付されました。図書館の棚には全部で何冊並べられますか。

最初の状態5 + 2 + 4
結果11
lesson.jl
function main()
    books = [5, 2, 4]
    total = 0
    i = 0
    while i < 3
        total += books[1 + i]
        i += 1
    end
    println(total)
end

main()
出力
11
例を実行する
ターミナル
julia lesson.jl
公式ドキュメント