Julia Quick Note [06] Loop processing

Loop processing (writing example)

note06



◆ while ~ end statement
    i = 0               #i initialization
    while i <= 10
        println(i)
        global i += 1   #Global variable
    end

◆ for ~ end statement(=When using equal)
    for i = 1:10
        println(i)
    end

◆ for ~ end statement(When using in)
    list = [1, 5, 10]
    for i in list
        println(i)
    end

Commentary

-In Julia, ** whilte ~ end ** statement and ** for ~ end ** statement can be used for loop processing. Iterative processing is executed in the part between whilte and for until end. -The major difference from Python is that there is ** end **. -Variables used inside blocks such as while and for cannot be used outside blocks because they are local variables.

(1) The execution result of the while-end statement is as follows.

image.png In the 4th line, ** global ** is added to the variable i.

If you want to use the variable i not only inside the loop processing (between while and end) but also outside, add global like this. Also, the value cannot be changed inside the loop processing unless global is added.

On the contrary, if you do not change the variable i, you do not need to add global. Only read is possible inside the loop process.

(2) The execution result of the for to end statement (= when using equal) is as follows.

image.png The variable i is repeated from 1 to 10. Please note that Julia will execute up to the last 10 of the slices ** 1: 10 **. (Python only runs 1-9)

(3) The execution result of the for to end statements (when using in) is as follows.

image.png By setting a list type variable in the list part of the second line, you can also loop with the in operator.

Table of contents

Julia Quick Look Note [01] How to use variables and constants Julia Quick Look Note [02] Arithmetic Expressions, Operators [Julia Quick Note [03] Complex Numbers] (https://qiita.com/ttabata/items/225c77a4d71fafc3e482) Julia Quick Look Note [04] Regular Expression [Julia quick note [05] if statement] (https://qiita.com/ttabata/items/4f0bcff1e32f60402dfb) [Julia fast-drawing note [06] loop processing] (https://qiita.com/ttabata/items/2a53825101b0b75fb589) [Julia Quick Note [07] try, catch, finally] (https://qiita.com/ttabata/items/1d6fe990526c99b65b5f)

(* We will continue to increase the content)

Related information

: paperclip: Julia --Official page https://julialang.org/

: paperclip: Julia --Japanese official document https://julia-doc-ja.readthedocs.io/ja/latest/index.html

: paperclip: First time Julia and installation (Windows & Linux) https://qiita.com/ttlabo/items/b05bb43d06239f968035

:paperclip: Julia - Mathematics https://docs.julialang.org/en/v1/base/math/

Opinions etc.

If you have any opinions or corrections, please let us know.

Recommended Posts

Julia Quick Note [06] Loop processing
Julia Quick Note [03] Complex Numbers
Julia Quick Note [10] Function (2) Application
Julia quick note [05] if statement
Julia Quick Note [04] Regular Expression
Julia Quick Note [09] Function (1) Basics
Julia Quick Note [07] try, catch, finally
Julia Quick Note [02] Arithmetic formulas, operators
Julia Quick Note [22] Calling Python functions and Python modules
Julia Quick Note [01] How to use variables and constants
Julia Quick Note [08] Variable Type (Int, Float, Bool, Char, String)
Paiza Python Primer 3: Learn Loop Processing