Kotlin

Kotlin 공부 - 조건문 if, when

자다르 2022. 4. 13. 11:35

※ 이해와 학습을 위해 원본 코드의 추가/수정이 있을 수 있습니다.

※ 인프런 강의: 개복치개발자님의 [입문편] 안드로이드를 위한 코틀린(Kotlin) 문법의 학습 기반으로 작성했습니다.

※ InterviewBit Kotlin 설명을 참고합니다.

※ 정보의 공유 사회(https://ddolcat.tistory.com/) 설명 및 예제를 참고합니다.

※ Programize(http://learning.coreref.com/www.programiz.com/kotlin-programming) 설명 및 예제를 참고합니다.

※ Udemy The Complete Android 12 & Kotlin Development Masterclass 설명 및 예제를 참고합니다.


  • 조건문 if/else if/else
fun main() {
   val score = 80
   var grade : String
   if(score > 100){
       grade = "A"
   } else if (score > 90) {
       grade = "B"
   } else if (score > 80) {
       grade = "C"
   } else if (score > 70) {
       grade = "D"
   } else {
       grade = "F"
   }
   println(grade)
}

결과
=======================
D
  • 조건문 when의 사용
fun main() {
   val score = 80
   when(score){
   	100 -> {
    	println("100점")
    }
    90 -> {
    	println("90점")
    }
    80 -> {
    	println("80점")
    }
    else -> {
    	println("70점미만")
    }
   }
}

결과
==================
80점