Kotlin
Kotlin 공부 - 반복문 for, while
자다르
2022. 4. 13. 10:40
※ 이해와 학습을 위해 원본 코드의 추가/수정이 있을 수 있습니다.
※ 인프런 강의: 개복치개발자님의 [입문편] 안드로이드를 위한 코틀린(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 설명 및 예제를 참고합니다.
- for 반복문-#1
fun main() {
val testList6 = listOf("a","b","c","d","e","f")
for (i in testList6){
println(i)
}
}
결과
====================================
a
b
c
d
e
f
- for 반복문-#2
fun main(){
for(i in 1..10){
println(i)
}
}
결과
================================
1
2
3
4
5
6
7
8
9
10
- for 반복문-#3 : 특정 기준으로 높일 때는 step
fun main(){
for(i in 1..10 step 2){
println(i)
}
}
결과
========================
1
3
5
7
9
- for 반복문 - downTo
fun main(){
for( i in 3 downTo 1 ){
println(i)
if( i == 1 ){
println("fire!")
}
}
}
결과
==================
3
2
1
fire!
- for 반복문-#4
fun main(){
for(i in 1..3){
println("i의 값은 : "+i)
}
for(i in 1..3){
println("i의 값은 : $i")
}
}
결과
============================
i의 값은 : 1
i의 값은 : 2
i의 값은 : 3
i의 값은 : 1
i의 값은 : 2
i의 값은 : 3
- for 반복문-#5 for의 중복 사용
fun main(){
for(i in 1..3){
for(j in 1..3){
println("i is $i and j is $j")
}
}
}
결과
=========================
i is 1 and j is 1
i is 1 and j is 2
i is 1 and j is 3
i is 2 and j is 1
i is 2 and j is 2
i is 2 and j is 3
i is 3 and j is 1
i is 3 and j is 2
i is 3 and j is 3
- for with if break
fun main() {
for( i in 1 until 20 ){
println( "$i" )
if( i/2 == 5){
break
}
}
println("Done with the loop")
}
결과
========================
1
2
3
4
5
6
7
8
9
10
Done with the loop
- (활용) 구구단 출력하기
fun main(){
println("구구단 출력하기")
for(i in 2..9){
println("$i 단===")
for(j in 1..9){
println("$i * $j ="+i*j)
}
}
}
결과
=====================
구구단 출력하기
2 단===
2 * 1 =2
2 * 2 =4
2 * 3 =6
2 * 4 =8
2 * 5 =10
2 * 6 =12
2 * 7 =14
2 * 8 =16
2 * 9 =18
3 단===
3 * 1 =3
3 * 2 =6
3 * 3 =9
3 * 4 =12
3 * 5 =15
3 * 6 =18
3 * 7 =21
3 * 8 =24
3 * 9 =27
4 단===
4 * 1 =4
4 * 2 =8
4 * 3 =12
4 * 4 =16
4 * 5 =20
4 * 6 =24
4 * 7 =28
4 * 8 =32
4 * 9 =36
5 단===
5 * 1 =5
5 * 2 =10
5 * 3 =15
5 * 4 =20
5 * 5 =25
5 * 6 =30
5 * 7 =35
5 * 8 =40
5 * 9 =45
6 단===
6 * 1 =6
6 * 2 =12
6 * 3 =18
6 * 4 =24
6 * 5 =30
6 * 6 =36
6 * 7 =42
6 * 8 =48
6 * 9 =54
7 단===
7 * 1 =7
7 * 2 =14
7 * 3 =21
7 * 4 =28
7 * 5 =35
7 * 6 =42
7 * 7 =49
7 * 8 =56
7 * 9 =63
8 단===
8 * 1 =8
8 * 2 =16
8 * 3 =24
8 * 4 =32
8 * 5 =40
8 * 6 =48
8 * 7 =56
8 * 8 =64
8 * 9 =72
9 단===
9 * 1 =9
9 * 2 =18
9 * 3 =27
9 * 4 =36
9 * 5 =45
9 * 6 =54
9 * 7 =63
9 * 8 =72
9 * 9 =81
- while 반복문
fun main() {
var count = 0
while ( count < 10) {
count++ // count = count +1
println(count)
}
}
결과
============================
1
2
3
4
5
6
7
8
9
10
- do while
fun main(){
var x : Int = 1
do{
println("$x")
x++
} while ( x <= 10)
println("\ndo while loop is done")
}
결과
=======================
1
2
3
4
5
6
7
8
9
10
do while loop is done
- while + condition
// source: The Complete Android 12 & Kotlin Development Masterclass
fun main(){
var feltTemp : String = "cold"
var roomTemp : Int = 10
if( feltTemp == "cold" ){
println("Current temparature is $roomTemp. It feels "+feltTemp+" Heating up please")
while ( feltTemp == "cold" ){
roomTemp++
if( roomTemp >= 20 ){
feltTemp = "comfy"
println("Current temparature is $roomTemp. It's comfy now")
}
}
}
}
결과
=======================
Current temparature is 10. It feels cold Heating up please
Current temparature is 20. It's comfy now