티스토리 뷰
Loops¶
출처¶
https://github.com/JuliaComputing/JuliaBoxTutorials/tree/master/introductory-tutorials/intro-to-julia (github : JuliaComputing/JuliaBoxTutorials/introductory-tutorials/intro-to-julia/)
Topics:
함께보기¶
- http://deepstat.tistory.com/45 (01. Getting started)(in English)
- http://deepstat.tistory.com/46 (01. Getting started(한글))
- http://deepstat.tistory.com/47 (02. Strings)(in English)
- http://deepstat.tistory.com/48 (02. Strings(한글))
- http://deepstat.tistory.com/49 (03. Data structures)(in English)
- http://deepstat.tistory.com/50 (03. Data structures(한글))
- http://deepstat.tistory.com/51 (04. Loops)(in English)
while 반복문 ¶
구문:
<font color=green>while<\font> *조건*
*반복 내용*
<font color=green>end<\font>
In [1]:
숫자 = 0
while 숫자 < 10
숫자 += 1
println(숫자)
end
숫자
Out[1]:
In [2]:
친구목록 = ["균우", "상완", "동훈", "진", "재현"]
ㅁ = 1
while ㅁ <= length(친구목록)
친구 = 친구목록[ㅁ]
println("안녕 $친구, 만나서 반가워!")
ㅁ += 1
end
for 반복문 ¶
구문:
<font color=green>for<\font> *변수명* <font color=green>in<\font> *반복시 변수에 넣을 목록*
*반복 내용*
<font color=green>end<\font>
In [3]:
for ㅁ in 1:10
println(ㅁ)
end
In [4]:
친구목록 = ["균우", "상완", "동훈", "진", "재현"]
for 친구 in 친구목록
println("안녕 $친구, 만나서 반가워!")
end
이제 for 문을 이용해서 특정 표를 만들어보자. 이 표는 각 원소가 행 위치와 열 위치의 합으로 되어있다.
먼저 표를 0값으로 이루어진 array로 선언한다.
In [5]:
행, 열 = 5, 5
표 = fill(0, (행, 열))
Out[5]:
In [6]:
for ㄱ in 1:행
for ㄴ in 1:열
표[ㄱ, ㄴ] = ㄱ + ㄴ
end
end
표
Out[6]:
같은 결과를 낼 수 있는 다른 문법도 있다.
In [7]:
표2 = fill(0, (행, 열))
Out[7]:
In [8]:
for ㄱ in 1:행, ㄴ in 1:열
표2[ㄱ, ㄴ] = ㄱ + ㄴ
end
표2
Out[8]:
array 안에 반복문이 들어가있는 좀 더 "Julia"스러운 문법이 있다.
In [9]:
표3 = [ㄱ + ㄴ for ㄱ in 1:행, ㄴ in 1:열]
Out[9]:
연습문제 ¶
4.1¶
1부터 100까지 반복문을 돌리면서 제곱값을 출력하자.
In [10]:
수 = 0
while 수 < 100
수 += 1
print("$(수^2) ")
end
In [11]:
for 수 in 1:100
print("$(수^2) ")
end
In [12]:
제곱1 = Dict(수 => 수^2 for 수 in 1:100)
Out[12]:
In [13]:
제곱1[10] == 100
Out[13]:
In [14]:
제곱2 = Dict()
수 = 0
while 수 < 100
수 += 1
제곱2[수] = 수^2
end
In [15]:
제곱2
Out[15]:
In [16]:
제곱2[10] == 100
Out[16]:
In [17]:
제곱3 = Dict()
for 수 in 1:100
제곱3[수] = 수^2
end
In [18]:
제곱3
Out[18]:
In [19]:
제곱3[10] == 100
Out[19]:
4.3¶
array 안에 반복문을 넣어서 1부터 100까지 수의 제곱을 array에 저장하자.
In [20]:
수_제곱 = [수^2 for 수 in 1:100]
println(수_제곱)
'Flux in Julia > Learning Julia (Intro_to_Julia)' 카테고리의 다른 글
05. Conditionals (한글) (0) | 2018.09.18 |
---|---|
05. Conditionals (0) | 2018.09.18 |
04. Loops (0) | 2018.09.15 |
03. Data structures (한글) (0) | 2018.09.13 |
03. Data structures (0) | 2018.09.13 |