-
Notifications
You must be signed in to change notification settings - Fork 7
QyuStory edited this page Aug 22, 2016
·
4 revisions
- if문의 조건식을 평가하여 그 식이 참이면 그 다음 블록을 평가하고, 아니면 다음 가지를 검사하거나 실행하는 과정을 반복한다.
// src/main/scala/progscala2/rounding/if.sc
if (2 + 2 == 5) {
println("Hello from 1984.")
} else if (2 + 2 == 3) {
println("Hello from Remedial Math class?")
} else {
println("Hello from a non-Orwellian future.")
}
해당 조건식 중 옳은 것이 없으므로 다음 결과를 도출
$ scala src/main/scala/progscala2/rounding/if.sc
Hello from a non-Orwellian future.
- Scala와 Java의 다른 부분은 대부분의 Scala의 문장이 값을 결과로 돌려주는 식이다.
- if문의 결과값을 다른 변수에 저정할 수 있다.
// src/main/scala/progscala2/rounding/assigned-if.sc
val configFile = new java.io.File("somefile.txt")
val configFilePath = if (configFile.exists()) {
configFile.getAbsolutePath()
} else {
configFile.createNewFile()
configFile.getAbsolutePath()
}
println(configFilePath)
위 configFilePath의 경우 설정 파일이 존재하지 않는 경우 내부에서 처리하여 절대경로를 return하는 if문의 값이다.
$ scala src/main/scala/progscala2/rounding/assigned-if.sc
/home/qyu/Workspace/scala/somefile.txt