1
+ /*
2
+ * Copyright (c) 2019 Razeware LLC
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
15
+ * distribute, sublicense, create a derivative work, and/or sell copies of the
16
+ * Software in any work that is designed, intended, or marketed for pedagogical or
17
+ * instructional purposes related to programming, coding, application development,
18
+ * or information technology. Permission for such use, copying, modification,
19
+ * merger, publication, distribution, sublicensing, creation of derivative works,
20
+ * or sale is expressly withheld.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
+ * THE SOFTWARE.
29
+ */
30
+
31
+ fun main () {
32
+ // RANGES
33
+ val closedRange = 0 .. 5
34
+
35
+ val halfOpenRange = 0 until 5 // 5 dahil değil
36
+
37
+
38
+ for (i in halfOpenRange) {
39
+ println (i)
40
+ }
41
+
42
+ println (" ------" )
43
+
44
+ val decreasingRange = 5 downTo 0 // 0 dahil.
45
+ for (i in decreasingRange) {
46
+ println (i)
47
+ }
48
+
49
+ println (" ------" )
50
+
51
+
52
+ val sampleRange = 3 downTo 0 // 0 dahil. ya da 0 until 3 ya da 0..3 dahillik durumları kritik.
53
+ for (i in sampleRange) {
54
+ println (i)
55
+ }
56
+
57
+ println (" ------" )
58
+
59
+ // FOR LOOPS
60
+ val count = 10
61
+
62
+ // TRIANGLE NUMBERS
63
+ var sum = 0
64
+ for (i in 1 .. count) {
65
+ sum + = i
66
+ }
67
+ println (sum)
68
+ println (" ------" )
69
+
70
+ // FIBONACCI
71
+ sum = 1
72
+ var lastSum = 0
73
+ repeat(10 ) { // 10 KERE BU İŞLEM TEKRAR EDER
74
+ val temp = sum
75
+ sum + = lastSum
76
+ lastSum = temp
77
+ }
78
+ println (sum)
79
+
80
+
81
+
82
+
83
+ // SUM ODD NUMBERS
84
+ sum = 0
85
+ for (i in 1 .. count step 2 ) {
86
+ sum + = i
87
+ }
88
+ println (sum)
89
+
90
+ // COUNT DOWN
91
+ sum = 0
92
+ for (i in count downTo 1 step 2 ) {
93
+ sum + = i
94
+ }
95
+ println (sum)
96
+
97
+ // CHESS BOARD ITERATION
98
+ sum = 0
99
+ for (row in 0 until 8 ) {
100
+ if (row % 2 == 0 ) {
101
+ continue
102
+ }
103
+
104
+ for (column in 0 until 8 ) {
105
+ sum + = row * column
106
+ }
107
+ }
108
+ println (sum)
109
+
110
+ sum = 0
111
+ rowLoop@ for (row in 0 until 8 ) {
112
+ columnLoop@ for (column in 0 until 8 ) {
113
+ if (row == column) {
114
+ continue @rowLoop
115
+ }
116
+ sum + = row * column
117
+ }
118
+ }
119
+ println (sum)
120
+
121
+
122
+ // WHEN EXPRESSION
123
+ val number = 10
124
+
125
+ when (number) {
126
+ 0 -> println (" Zero" )
127
+ else -> println (" Non-zero" )
128
+ }
129
+
130
+ when (number) {
131
+ 10 -> println (" It's ten!" )
132
+ }
133
+
134
+ val string = " Dog"
135
+ when (string) {
136
+ " Cat" , " Dog" -> println (" Animal is a house pet." ) // OR
137
+ else -> println (" Animal is not a house pet." )
138
+ }
139
+
140
+ val numberName = when (number) {
141
+ 2 -> " two"
142
+ 4 -> " four"
143
+ 6 -> " six"
144
+ 8 -> " eight"
145
+ 10 -> " ten"
146
+ else -> {
147
+ println (" Unknown number" )
148
+ " Unknown"
149
+ }
150
+ }
151
+
152
+ println (numberName) // > ten
153
+
154
+
155
+ val hourOfDay = 12
156
+ val timeOfDay: String
157
+
158
+ // timeOfDay = when (hourOfDay) {
159
+ // 0, 1, 2, 3, 4, 5 -> "Early morning"
160
+ // 6, 7, 8, 9, 10, 11 -> "Morning"
161
+ // 12, 13, 14, 15, 16 -> "Afternoon"
162
+ // 17, 18, 19 -> "Evening"
163
+ // 20, 21, 22, 23 -> "Late evening"
164
+ // else -> "INVALID HOUR!"
165
+ // }
166
+
167
+ timeOfDay = when (hourOfDay) {
168
+ in 0 .. 5 -> " Early morning"
169
+ in 6 .. 11 -> " Morning"
170
+ in 12 .. 16 -> " Afternoon"
171
+ in 17 .. 19 -> " Evening"
172
+ in 20 .. 23 -> " Late evening"
173
+ else -> " INVALID HOUR!"
174
+ }
175
+ println (timeOfDay)
176
+
177
+ when {
178
+ number % 2 == 0 -> println (" Even" )
179
+ else -> println (" Odd" )
180
+ }
181
+
182
+ val (x, y, z) = Triple (3 , 2 , 5 )
183
+
184
+ when {
185
+ x == 0 && y == 0 && z == 0 -> println (" Origin" )
186
+ y == 0 && z == 0 -> println (" On the x-axis." )
187
+ x == 0 && z == 0 -> println (" On the y-axis." )
188
+ x == 0 && y == 0 -> println (" On the z-axis." )
189
+ else -> println (" Somewhere in space" )
190
+ }
191
+
192
+ when {
193
+ x == 0 && y == 0 && z == 0 -> println (" Origin" )
194
+ y == 0 && z == 0 -> println (" On the x-axis at x = $x " )
195
+ x == 0 && z == 0 -> println (" On the y-axis at y = $y " )
196
+ x == 0 && y == 0 -> println (" On the z-axis at z = $z " )
197
+ else -> println (" Somewhere in space at x = $x , y = $y , z = $z " )
198
+ }
199
+
200
+ when {
201
+ x == y -> println (" Along the y = x line." )
202
+ y == x * x -> println (" Along the y = x^2 line." )
203
+ }
204
+ }
0 commit comments