Kotlin Operator
Kotlin Operator
o Arithmetic operator
o Relation operator
o Assignment operator
o Unary operator
o Bitwise operation
o Logical operator
Arithmetic Operator
Arithmetic operators are used to perform basic mathematical operations such as addition
(+), subtraction (-), multiplication (*), division (/) etc.
Relation Operator
Relation operator shows the relation and compares between operands. Following are the
different relational operators:
Assignment operator
Assignment operator "=" is used to assign a value to another variable. The assignment of
value takes from right to left.
! not !a a.not()
Logical Operator
Logical operators are used to check conditions between operands. List of logical operators
are given below.
&& return true if all expression are true (a>b) && (a>c) (a>b) and (a>c)
|| return true if any expression are true (a>b) || (a>c) (a>b) or(a>c)
Bitwise Operation
In Kotlin, there is not any special bitwise operator. Bitwise operation is done using named
function.
Kotlin Output
Kotlin output operation is performed using the standard methods print() and println().
Let's see an example:
Output
Hello World!
Welcome to JavaTpoint
The methods print() and println() are internally call System.out.print() and
System.out.println() respectively.
Play Video
Example
Output:
10
Welcome to JavaTpoint
20Hello
Kotlin Input
Kotlin has standard library function readLine() which is used for reads line of string input
from standard input stream. It returns the line read or null. Let's see an example:
Output:
Enter your name
Ashutosh
Enter your age
25
Your name is Ashutosh and your age is 25
While using the readLine() function, input lines other than String are explicitly converted
into their corresponding types.
To input other data type rather than String, we need to use Scanner object of
java.util.Scanner class from Java standard library.
1. import java.util.Scanner
2. fun main(args: Array<String>) {
3. val read = Scanner(System.`in`)
4. println("Enter your age")
5. var age = read.nextInt()
6. println("Your input age is "+age)
7. }
Output:
Here nextInt() is a method which takes integer input and stores in integer variable. The
other data types Boolean, Float, Long and Double uses nextBoolean(), nextFloat(),
nextLong() and nextDouble() to get input from user.
Kotlin Comment
Comments are the statements that are used for documentation purpose. Comments are
ignored by compiler so that don't execute. We can also used it for providing information
about the line of code. There are two types of comments in Kotlin.
Kotlin if Expression
In Kotlin, if is an expression is which returns a value. It is used for control the flow of
program structure. There is various type of if expression in Kotlin.
o if-else expression
o if-else if-else ladder expression
o nested if expression
We can remove the curly braces of if-else body by writing if expression in only one
statement.
For example:
Output:
For Example
Output:
Four
Multiple Statement of when Using Braces
We can use multiple statement enclosed within block of condition.
For Example
Output:
Monday
First day of the week
Output:
It is rainy season
For Example:
Output:
Output:
80
85
60
90
70
If the body of for loop contains only one single line of statement, it is not necessary to
enclose within curly braces {}.
The elements of an array are iterated on the basis of indices (index) of array. For example:
Output:
marks[0]: 80
marks[1]: 85
marks[2]: 60
marks[3]: 90
marks[4]: 70
Iterate through range
Let's see an example of iterating the elements of range.
Output:
1. while(condition){
2. //body of loop
3. }
Output:
1
2
3
4
5
For example:
Output:
infinite loop
infinite loop
infinite loop
.
.
.
.
infinite loop
infinite loop
infinite loop
infinite loop
infinite loop
infinite loop
As a do block of do-while loop executed first before checking the condition, do-while loop
execute at least once even the condition within while is false. The while statement of do-
while loop end with ";" (semicolon).
Syntax
1. do{
2. //body of do block
3. }
4. while(condition);
Output:
1
2
3
4
5
In this example do-while loop execute at once time even the condition of while is false.
Output:
o break
o continue
o return
Break Expression
A break expression is used for terminate the nearest enclosing loop. It is almost used with
if-else condition.
For example:
1. for(..){
2. //body of for
3. if(checkCondition){
4. break;
5. }
6. }
In the above example, for loop terminates its loop when if condition execute break
expression.
372.9K
Output:
1
2
In the above example, when the value of i became equal to 3 and satisfy
the if condition(i==3) than the break expression execute and terminate for loop.
Kotlin labeled break expression is used to terminate the specific loop. This is done by using
break expression with @ sign followed by label name (break@loop).
Output:
i = 1 and j = 1
i = 1 and j = 2
i = 1 and j = 3
i = 2 and j = 1
In the above example, when the value of i became 2 and satisfy the if condition which
execute break expression followed by labeled name. The break expression followed by
labeled name terminates the body of label identifier.