Tag

## control statement

Browsing

In this blog, we’ll learn FOR loop in kotlin Adnroid, will see the exact flow of for loop. How it will work, Will understand the working of FOR loop in detail with the help of an example. So let’s started

#### FOR LOOP SYNTAX

FOR loop the syntax is for followed by space, bracket open and close. Inside this I simply used the variable of i followed by in operator and defined the range.

```for (initializer in ranges) {
}```
##### Lets take a simple example
```fun main(args: Array<String>) {
// For loop
for (i in 1..20) {
println(i)
}
}```

#### FOR loop example

Let take a very basic example of FOR loop, Open the IDE and paste below code

```fun main(args: Array<String>) {
for (i in 1..3) {
println("hi..")
}
}```

Now let us run the code, and see the output on the console

```hi..
hi..
hi..
Process finished with exit code 0```

So here we simply show that println() method has been executed three times. That is loop 1, loop 2 and loop3  has been executed. After the loop 3, loops actually terminated.

##### What is the exact flow of FOR loop.

For understanding the exact flow, let take an example, see below code

#### 1 Iteration

In the first iteration( loop 1) the value of i is actually 1 (i=0)and next step comes to the condition check so 1 falls inside the 1 to 3 range. So the condition becomes true then we simply ‘print hi’, and finally at end of the loop simply increment the value of i by 1, So now value of i becomes 2 (i=2).

#### 2 Iteration

Now value i=2 comes the vary initial point of 2 iterations. In the case in second iteration again we have to condition check 2 actually falls inside the range 1 to 3 range, and the condition become true again and again we ‘print hi’, at end of iteration simply increment the value of I and i become 3 (i=3)

Now, i=3 again come the initial value of loop 3, or you can say starting point of 3 loop. And gain condition becomes true. Because this 3 is actually present inside the 1 to 3 range right.

So again we’ll print hello. And finally value of i becomes 4 (i=4). Now at the end of loop 3 when the value of i becomes 4, This will try to initiate 4 loop. Which will never happen, Because i=4 simply make the condition as false. The 4 does not lie inside the range. So finally the loop terminates right.

#### Overview of FOR loop

Finally, the the complete overview looks like below figure.

#### FOR loop example

Let’s take another example for better understanding. Now suppose I ask to write a program using FOR loop print out all the even numbers starting from 1 to 20. For doing that I simply write if else condition. Such as below

```fun main(args: Array<String>) {
// For loop
for (i in 1..20) {
if (i % 2 == 0)
println(i)
}
}```

So let us now run  the code. So here we go 2,4,6,8…,20. So we prints all event number using this for loop example.

```2
4
..
..
18
20
Process finished with exit code 0
```

#### Conclusion

In this post, we have learned How does FOR loop works in the case of Kotlin. Thank for reading, Have a Good Day

in this post, we’ll learn when as expression, Now this when is actually the replacement of switch case statement similar to java, So let us explore when expression in Kotlin

```fun main(args: Array<String>) {
val x = 2
}```

Let the above code block, here we simply define one variable is x and it is final.

#### WHEN statement

Now instated of writing switch case statement that we used to in case of java, Here we have the when expression. Let us write code

Let say the value of x is equal to 1, then simply put dash followed by arrow than simply write the code.  Similarly the value of x is equal to 2 then execute line of code.

```fun main(args: Array<String>) {
val x = 2
when (x) {
1 -> print("x is 1")
2 -> print("x is 2")
}
}```

Now let us run the code, so we get the output as “x is 2”. Now here notice we don’t have to write any break statement or the continue statement.

#### Default Statement

Now similar to java we also have a default statement in case of when clause. The default statement in the case of when actually represented with help of else. So we need to write else part here. Now again syntax is a dash followed by an arrow (->).

```fun main(args: Array<String>) {
val x = 7
when (x) {
1 -> print("x is 1")
2 -> print("x is 2")
else -> {
print("x value is unknown")
}
}
}```

Now let us change the x value is 7 and run the code then we get

```x value is unknown
Process finished with exit code 0```

Just because value is 7 and we have written code just for 1 & 2, not for 7 that is why the else part is executed.

#### Multiple conditions in one statement

Now we can have also multiple conditions inside the statement. Means, In this block we can write multiple conditions Like below.

Now suppose x value is 0, and you want to execute multiple condition in just one statement, what you can do is, you can do  simply use 0,5 ->

```fun main(args: Array<String>) {
val x = 5
when (x) {
1, 5 -> print("x is 1 or 5")
2 -> print("x is 2")
else -> {
print("x value is unknown")
}
}
}```

In this example you define like 1,2,4, in is single line statement. Now lets run the code in output console you have like below

```x is 1 or 5
Process finished with exit code 0```

#### Range uses when expression

Apart from this, we can also use range in case of value here. Let’s say x is equal to 12 right. Now suppose here I’m used range 1..15. How we do that

``` fun main(args: Array<String>) {
val x = 12
when (x) {
in 1..15 -> print("x is 1 or 5")
2 -> print("x is 2")
else -> {
print("x value is unknown")
}
}
}```

So this 1..15 is contains value 1 to 15, which also include value 12 as well , So this condition simply return true

#### Operator with Range

In the same way we can also use not(!) operator here, Just put this operator like below

```fun main(args: Array<String>) {
val x = 13
when (x) {
!in 1..15 -> print("x is 1 or 5")
2 -> print("x is 2")
else -> {
print("x value is unknown")
}
}
}```

In this block, when conditions become false here then compilers look for simple matches here. And again it won’t find the match then by default it will execute the else part here

Now let us run the code and see on console, Output will be like below .

```x value is unknown
Process finished with exit code 0
```

#### WHEN as Expression

Suppose if I define a variable, let say var result:String, Now let us change this when statement as expression, In this case simply we assign result wich when statement using the equal sign.

```fun main(args: Array<String>) {
val x = 1
val result = when (x) {
1 -> "x is 1"
2 -> "x is 2"
else -> {
"x value is unknown"
}
}
print(result)
}```

Now here, what is happening here when the condition is actually matched then it will simply return the match value. And this value will assign to result variable.

#### Multiple statement in a block

Now suppose If I will write two statements inside one block. In this situation what will happen because we have two string expressions inside the else block. Lets us run the code and see

```fun main(args: Array<String>) {
val x = 5
val result = when (x) {
1 -> "x is 1"
2 -> "x is 2"
else -> {
"x value is unknown"
"x is a magic number"
}
}
print(result)
}```

The output is

```x is a magic number
Process finished with exit code 0```

As you see we are getting a second expression. What happened is that whatever statement we have inside the code at the end, that actually returns. We saw the last expression is actually executed.

So always remember this, whatever you write the end of the code block it actually return.

#### Conclusion

This is all about when expression, here the when statement actually acting expression and returning some value, In Kotlin, forgot about switch statement and simply adapt to when expression. Thank for reading, Have a Good Day

In this post, we’ll learn If else conditional statement in Kotlin. We’ll cover each small topic, that has related to IF-ELSE. will see If expression and if multiple conditions with the help of an example. So lets started

#### If else conditional Statement

Let’s see the below here we simply define two values. Now down the side, I want to get max value using if condition in Kotlin

```fun main(args: Array<String>) {
val a = 10
val b = 15

var result: Int
if (a > b) {
result = a
} else {
result = b
}
print("Greater value is \$result")
}```

Above expression, we simply wrote a code for get a max value. So let us run the code and see, here we find simply 15, So we had simply written code to find the maximum value of these two number .

Output is

``` Greater value is 15
Process finished with exit code 0```

#### IF as Expression

The conditional statement is very similar to java. But in Kotlin we have If expression. What mean of that? We can use if condition as expression. How to do that let’s see

```fun main(args: Array<String>) {
val a = 10
val b = 15

val result = if (a > b) {
a
} else {
b
}
print("Greater value is \$result")
}```

As you see our code is more cleaner, So here what I’m doing is, this is the if as an expression. Now simply if a is greater than b then assign a to the result variable else assign b.

Means which is a maximum value that will be assigned to the result variable. In the end, simply print the result variables. Let us run the code the output will we same. So in Kotlin we can make if condition to the return some value, that called IF as Expression

#### Multiple if statements

Suppose I have multiple lines of code inside the if condition so which value will be return? and which will store in the result variable.

```   fun main(args: Array<String>) {
val a = 10
val b = 15

val result = if (a > b) {
print("A is greater")
a
} else {
print("B is greater")
b
}
print("Greater value is \$result")
}```

Let see above code here I’m adding a block of code inside the if condition. In this condition where we have multiple lines of code with in the IF block also else block. So which of the following will be return

Now as per the rule, The last statement that is define inside the if condition will be returned.In this example last statement is a and b

#### Conclusion

So in this way we are actually using the if-else condition as an expression now this if the condition is returning some value. So that us why we calling IF expression

Read our new article switch statement in Kotlin