-
Notifications
You must be signed in to change notification settings - Fork 1
/
ForLoops.kt
99 lines (80 loc) · 2.84 KB
/
ForLoops.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// For Loops and Switch Case in Kotlin
val hashMapOfNamesandAge= mapOf<String,Int>("Tanishq" to 20,"Saksham" to 20,"Varun" to 22)
val mapofdetails= mapOf<Any,Any>( 1 to mapOf<Any,Any>("NAME" to "Tanishq","Age" to 20,"Profession" to "Student","Nationality" to "Indian","Hobby" to "Android Development")
,2 to mapOf<Any,Any>("NAME" to "Saksham","Age" to 20,"Profession" to "Student","Nationality" to "Indian","Hobby" to "Android Development")
,3 to mapOf<Any,Any>("NAME" to "Varun","Age" to 22,"Profession" to "Student","Nationality" to "Indian","Hobby" to "Coding"))
val numberList= listOf<Int>(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
fun main(){
// I : forEach loop in kotlin - iterates through all the objects in a collection
println("Printing details through forEach loops\n")
hashMapOfNamesandAge.forEach{ Name , Age ->
println(Name + ": " + Age)
}
mapofdetails.forEach{
println(it)
// 'it' collects the entire collection in the Hashmap and print them serially
}
println("\nPrinting details through primitive for loops\n")
for(details in mapofdetails){
println(details)
}
mapofdetails.forEach{
println("\n"+it)
}
for(name in hashMapOfNamesandAge){
println(name)
}
for (number in numberList){
println(number)
}
for (x in 0..10){
println(x)
}
for(y in 0 until 5){
println(y)
}
//giving increments in for loop
for( x in 0 until 10 step 2){
println(x)
}
// giving decrements in for loop
for(x in 10 downTo 0 step 2){
println(x)
}
// Special Keywords in Kotlin
// 1. as - it casts a variable into specific type
val any:Any ="Tanishq Sehgal"
val any2:Any= any as String
// 2. in - used to specify an object in a particular range, specifies the object being iterated in a loop, used as an infix operator to check that a value belongs to a range or a collection of entities, used in when expressions
for(x in 5 downTo 0 step 2){
println(x)
}
//3. is - used in check expressions
if(any is String){
println(any.length)
}
// switch case in kotlin - when function
when(any){
"Sehgal"->{
println("Partially Matched")
}
"Tanishq"->{
"Partially Matched"
}
"Tanishq Sehgal"->{
println("Matched")
}
else ->{
println("Invalid Name")
}
}
// returning a value from when statement
val myName:Any="Tanishq"
val name= when(myName){
"Sehgal"-> "Tanishq $myName"
"Tanishq"-> "$myName Sehgal"
"Coder" -> "$myName"
else -> "Invalid Name"
}
println(name)
}