Kotlin Keywords
Keywords are used of special meaning and purpose of programming language. Generally kotlin keywords is dividing of three category. there is hard keyword, soft keywords and modifier keywords. In this post mention examples of those keywords. Like that how to use and what is the role of keywords.
Kotlin Hard Keyword
Hard keyword means that are not used to define identifies name, function name and class name. They are given in belowing list.
as | as? | break | class | continue |
do | else | for | fun | if |
in | !in | interface | is | !is |
null | object | package | return | super |
this | throw | true | try | typealias |
val | var | when | while |
val and var keyword
val keyword are used to declare constant (immutable) variable in kotlin programming. And var keyword are used to define mutable variable.
//var and val keyword example
fun main(args: Array<String>) {
//Define variables
val PI : Float = 3.1415f //constant variable
var age : Int = 21
//Not allows to modify new value when its already assigned
//PI = 3.14
age = 22 //Modified var variable value
println("PI :$PI")
println("age : $age")
}
Output
PI :3.1415
age : 22
fun and return keyword
fun keyword are to define method (function) in kotlin programming. And return keyword are used to return value by function. For example.
//define a function
fun sum(x:Int,y:Int):Int{
return x+y //return statement
}
fun main(args: Array<String>) {
println("Sum : ${sum(3,5)}")
}
Output
Sum : 8
if, else keyword
This if-else is a keyword that are used to define conditional statement. if statement expression is based on boolean value. If that expression are produced boolean value true then this block will be execute. Otherwise if exist other if-else block then it will test similar way to other statement. And (else) statement is last block it will work when (if) and (if-else) statement not work.
if(expresion1){
//logical code
}else if(expression2){
//logical code
//group of statements
}else if(expression3){
//logical code
//group of statements
}else{
//else section
}
break keyword
break is a keyword that is create a termination statement of loop. This statement are used inside of for loop, while loop and do-while loop. For example.
fun main(args: Array<String>) {
var totalCandidate:Int=100
for(candidate in 1..totalCandidate){
if(candidate*3>=30){
println("Break Time")
break;
}
println(" Candidate ($candidate) Time interval: ${candidate*3}")
}
}
Output
Candidate (1) Time interval: 3
Candidate (2) Time interval: 6
Candidate (3) Time interval: 9
Candidate (4) Time interval: 12
Candidate (5) Time interval: 15
Candidate (6) Time interval: 18
Candidate (7) Time interval: 21
Candidate (8) Time interval: 24
Candidate (9) Time interval: 27
Break Time
class keyword
class keyword is used to define a class in our program.
//define a class
class Connection{
fun info(){
println("This is info function of Connection")
}
}
fun main(args: Array<String>) {
val obj=Connection() //Create object of class
obj.info() //Execute function
}
Output
This is info function of Connection
continue keyword
This keyword are used to define loop statement, that are change the flow of loop execution.
fun main(args: Array<String>) {
var index:Int =1
while(index<10){
index++
if(index>3 && index<7){
continue;//continue statement
}
println("index : ($index)")
}
}
Output
index : (2)
index : (3)
index : (7)
index : (8)
index : (9)
index : (10)
do- while keyword
while keyword is used to define while loop in program. in above example are given this example of while loop. And do-while is used to create do-while loop in our program. The advantage of this loop are it will work at least one time.
fun main(args: Array<String>) {
var counter:Int =1
do{
//loop statements
println("$counter")
counter++
}while(counter<=3)
}
Output
1
2
3
if, else keyword
This is are used to create condition statement. if statement is based on conditional statement. And this expression are produced boolean result and work on boolean value. When expression is produce True result then executes If-block of statements. otherwise executes of else-block.
//series of if..else statement
if(expression1){
//when expression1 is true
}else if(expression2){
//when expression2 is true
}
else if(expression3){
//when expression3 is true
}
else{
// when no expression1 is true
}
For example
fun main(args: Array<String>) {
val discount:String ="40 % off"
//series of if..else statement
if(discount=="20 % off"){
println("That is 20 % off")
}else if(discount=="40 % off"){
println("That is 40 % off")
}
else if(discount=="60 % off"){
println("That is 60 % off")
}
else{
println("Else part")
}
}
Output
That is 40 % off
for,in keyword
This keyword are used to create for loop and this can be used to iterates element of array, collection element and specified range value.
fun main(args: Array<String>) {
val stringData:String ="Kotlin"
//iterating string element
for (element in stringData){
println("$element")
}
}
Output
K
o
t
l
i
n
Kotlin Soft Keyword
soft keyword is used to define statement and expression. In different context and scenario this can be used by the name of identifiers.
by | catch | constructor | delegate | dynamic |
field | file | finally | get | import |
init | param | property | receiver | set |
setparam | where |
Kotlin Modifier Keywords
actual | abstract | annotation | companion | const |
crossinline | data | enum | expect | external |
final | infix | inline | inner | internal |
lateinit | noinline | open | operator | out |
override | private | protected | public | reified |
sealed | suspend | tailrec | vararg |
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment