CONTROL FLOW
The control flow is the way which controls the flow of statements to be executed in here the control flow keywords controls the statement which is to be executed first and there are keywords which can be used to repeat the statements here are the keywords given below
if expression
The if is an expression which returns a value in kotlin as in the english language like after the if keyword a condition is written if the condition is true the statement coming after the if statement will be executed there is also a way in which we can go for a else statement to execute if the if condition become false for example
// it is the traditional way
var name = bas
if (a<b) name = nas
//like other languages there is no terenary operator as "if" works fine here
var id = 123
if (32<33){
id = 365
}
else{
id = 284
}
//if as an expression
val name = if (a<b) nas else jas
var name = bas
if (a<b) name = nas
//like other languages there is no terenary operator as "if" works fine here
var id = 123
if (32<33){
id = 365
}
else{
id = 284
}
//if as an expression
val name = if (a<b) nas else jas
if branches can be blocks and the last expression is the value of a block
var mass = if (a>b){
print("choose a")
a
}else{
print ("choose b")
b
}
print("choose a")
a
}else{
print ("choose b")
b
}
one thing to note here is that if you are using if as an expression rather than the statement (like in if statement assigns the value for "mass" in the above statement) then you should include the else branch
When Expression
The when expression works as switch statement in other c like languages here is an example how when is used as
var name =23
when (name){
1 -> print("the value of name is 1")
2 ->print ("the value of name is 2")
else -> {
print("value of name is 23")
}
}
when (name){
1 -> print("the value of name is 1")
2 ->print ("the value of name is 2")
else -> {
print("value of name is 23")
}
}
in the above sample we can see that the argument in the when expression is given as "name" and the value of "name" is 23 the arguments in the when expression which evaluated sequentially with the branches in the when block like here "name" value is evaluated with 1 and 2 and if the value is same as 1 the output will be "the value of name is 1" and if it is 2 it will print "the value of name is 2"
and if those two values are not evaluated true then the else block will be executed when can be used as a expression or as a statement if it is used as an expression then the value of the satisfied branch becomes the value of the overall expression and if the when used as a statement then the values of the individual branches are ignored as explained above for if expression each branch can be a block and its value is the value of the last expression in the block if when is used as an expression then else is mandatory
if there are many cases are to be handled in the same way we can use comma as example
and if those two values are not evaluated true then the else block will be executed when can be used as a expression or as a statement if it is used as an expression then the value of the satisfied branch becomes the value of the overall expression and if the when used as a statement then the values of the individual branches are ignored as explained above for if expression each branch can be a block and its value is the value of the last expression in the block if when is used as an expression then else is mandatory
if there are many cases are to be handled in the same way we can use comma as example
when (name){
0,1 -> print("the value is either one or zero ")
else->print("the value is 23")
}
0,1 -> print("the value is either one or zero ")
else->print("the value is 23")
}
We can use arbitrary expressions as branch conditions
when(name){
parseInt(s)->print("the value is s")
else -> print("otherwise")
}
parseInt(s)->print("the value is s")
else -> print("otherwise")
}
We can also check that if the given argument is in the given range or collection with in or !in
when (name){
in 1..10 -> print("name is in the range of 1 to 10")
!in 10..19 ->print("name is not in the range 10 to 19")
else - > print("otherwise")
}
in 1..10 -> print("name is in the range of 1 to 10")
!in 10..19 ->print("name is not in the range 10 to 19")
else - > print("otherwise")
}
we can also check if the value is certain type or is not using is and !is
when (name){
is String -> print ("the value is a string")
else -> false
}
is String -> print ("the value is a string")
else -> false
}
The when statement can used as a alternative for if else statement if no argument is supplied the branch conditions are simply boolean expressions and a branch is executed when its condition is true
example :
example :
when{
name isodd -> print ("name is odd")
name iseven -> print("name is even")
else -> print("name is nothing")
}
name isodd -> print ("name is odd")
name iseven -> print("name is even")
else -> print("name is nothing")
}
No comments:
Post a Comment