Lesson 9 Programming in Kotlin language Logical operations
Lesson 9 Programming in Kotlin language Logical operations
First, what is the Boolean variable: it is a variable that accepts only two values true, false
Example 3> 2 The result is true
7 <3 The result is false
Logical operations: are( and - or – not)
For( and) we use &&
For (or) we use ||
For (not) we use !
This table demonstrates logical operations
the process( and) be true if and only if the value of both x and y is true
If either or both is false then the result is false
the process (or) be false if and only if the value of each false is either one or both true, the result is true
the process(not) is the opposite
if the value of x is true--> the ! x is false
if the value of x is false --> the !x is true
for example:
fun main (args:Array <String>)
{
print("enter a number: ")
var number1 = readLine()!!
print(number1>0 && number1<10)
print("enter other number: ")
var number2 = readLine()!!
print(number2 > 2 || number2 == 0)
}
In this example we asked the user to enter the number we stored in the variable (number1) if the number entered is greater than 0 and smaller than 10 then the result is true - if the two conditions are true – then otherwise the result will be false
Then we dialed another number and stored it in(number2) if the input number is greater than 2 or equal to 0. If either of the two conditions is true, the result is true. If both are false, the result is false.
Example:
fun main (args:Array <String>)
{
var x = 10
print((x > 1 && x <= 10) || (x > 50 && x < 100))
}
In this example, we defined an x variable of 10 The result that will be printed is true
because if we replace each x with 10 of the condition we will be
10> 1 --> true
10 <= 10 --> true
10> 50 --> false
10 <100 -->true
the condition has become ((true && true) || (false && true))
We know that ( true && true) is true
And ( false && true) is false
the result of The condition (true) || (false)) is true
I hope things are clear
We will understand the logical processes further in the next lesson, "Conditional Phrases"
Q: What will be the result in the previous example if it is
var y = 20
var z = 55
Instead of x