Lesson 9: Composite conditions with logical operators in Kotlin Language

So far we have seen operators:
 Relational (>, <,> =, <=, ==,! =)
 Mathematicians (+, -, *, /,%)

But we are missing other essential operators:

 Logical (&&, ||)

These two operators are used primarily in conditional structures to group several simple conditions.

Kotlin “AND” Operator &&

Lesson 9: Composite conditions with logical operators in Kotlin Language - Lesson 9: Composite conditions with logical operators in Kotlin Language - Lesson 9: Composite conditions with logical operators in Kotlin Language - conditions with logical operators
conditions with logical operators

Translated it reads like “Y”. If Condition 1 is true AND condition 2 is true then execute the true branch.
When we link two or more conditions with the “&&” operator, the two conditions must be true so that the result of the condition composed of True and continues through the real branch of the conditional structure.

The use of logical operators allows in many cases to propose shorter and more comprehensible algorithms.

Problem 1

Make a program that reads three numbers and shows us the major one.

Project23 – Principal.kt

fun main (parameter: Array <String>) {
//coding180.com
     print ("Enter first value:")
     val num1 = readLine () !!. toInt ()
     print ("Enter second value:")
     val num2 = readLine () !!. toInt ()
     print ("Enter third value:")
     val num3 = readLine () !!. toInt ()
     if (num1 > num2 && num1 > num3)
         print (num1)
     else
         if (num2> num3)
             print (num2)
         else
             print (num3)
// conditions with logical operators
 }

This exercise is solved without using logical operators in an earlier concept of the tutorial. The first conditional structure is a COMPOSITE CONDITIONAL STRUCTURE with a COMPOSITE CONDITION.

We can read it as follows:
If the contents of the variable num1 is greater than the contents of the variable num2 And if the contents of the variable num1 is greater than the contents of the variable num3 then the COMPONENT CONDITION is True.
If one of the simple conditions gives false the COMPONENT CONDITION gives False and continuous by the branch of the false.
That is, the contents of num1 will be displayed if and only if num1> num2 and num1> num3.
If the condition is false, we analyze the contents of num2 and num3 to see which has a greater value.
In this second conditional structure no logical operators are required as there is a simple condition.

Kotlin OR Operator ||

Translated it reads like “O”. If condition 1 is True or condition 2 is True, then run the True branch.
When we link two or more conditions with the “Or” operator, one of the two conditions is True, so that the result of the compound condition is True.

Problem 2

A date (day, month and year) is loaded by keyboard. Display a message if it corresponds to the first quarter of the year (January, February or March)
Key in the numeric value of the day, month and year. Example: day: 10 month: 2 year: 2017.

Project24 – Main.kt

fun main (parameter: Array <String>) {
    //coding180.com
    print ("Enter day:")
    val day = readLine () !!. toInt ()
    print ("Enter month:")
    val month = readLine () !!. toInt ()
    print ("Enter Year:")
    val year = readLine () !!. toInt ()
    if (month == 1 || month == 2 || month == 3)
        print ("Corresponds to the first quarter")
}

The loading of a date is done by parts, we enter the variables day, month and year.
We show the message “Corresponds to the first quarter” in case the month entered by keyboard is equal to 1, 2 or 3.

In the condition do not participate the variables day and year.

Problems proposed

  • Make a program that asks to load any date, then check if that date corresponds to Christmas.
  • You enter three values ​​by keyboard, if all are equal calculate the cube of the number and display it.
  • Three numbers are entered by keyboard, if all values ​​entered are less than 10, print the legend “All numbers are less than ten”.
  • Three numbers are entered by keyboard, if at least one of the values ​​entered is less than 10, print on the screen the legend “One of the numbers is less than ten”.
  • Write a program that asks you to enter the coordinate of a point in the plane, ie two integer values ​​x and y (other than zero).
    Then print on the screen where the quadrant is located. (1 Quadrant if x> 0 Y y> 0, 2º Quadrant: x <0 Y y> 0, etc.)
  • Write a program in which: given a list of three integer values ​​entered by keyboard is saved in two other variables the smallest and largest in that list. Use the if as an expression to get the highest and the lowest.
    Then print the two variables.

Solution

 

// Project25

 fun main (parameter: Array <String>) {
//coding180.com
     print ("Enter day:")
     val day = readLine () !!. toInt ()
     print ("Enter month:")
// conditions with logical operators
     val month= readLine () !!. toInt ()
     print ("Enter Year:")
     val year = readLine () !!. toInt ()
     if (month == 12 && day == 25)
         print ("The date entered corresponds to Christmas.")
 }



 Project26

 fun main (parameter: Array <String>) {
     print ("Enter first value:")
//coding180.com
     val value1 = readLine () !!. toInt ()
     print ("Enter second value:")
     val value2 = readLine () !!. toInt ()
     print ("Enter third value:")
     val value3 = readLine () !!. toInt ()
     if (value1 == value2 && value1 == value3) {
         val cube = value1 * value1 * value3
         print ("The cube of $value1 is $cube")
     }
 }


 Project27

 fun main (parameter: Array <String>) {
//coding180.com
     print ("Enter first value:")
     val value1 = readLine () !!. toInt ()
     print ("Enter second value:")
     val value2 = readLine () !!. toInt ()
     print ("Enter third value:")
     val value3 = readLine () !!. toInt ()
     if (value1 <10 && value2 <10 && value3 <10)
         print ("All numbers are less than ten");
 }


// Project28

 fun main (parameter: Array <String>) {
//coding180.com
     print ("Enter first value:")
     val value1 = readLine () !!. toInt ()
     print ("Enter second value:")
     val value2 = readLine () !!. toInt ()
     print ("Enter third value:")
     val value3 = readLine () !!. toInt ()
     if (value1 <10 | value2 <10 | value3 <10)
         Print ("Some of the numbers are less than ten")
 }




// Project29

 fun main (parameter: Array <String>) {
//coding180.com
     print ("Enter x: coordinate")
     val x = readLine () !!. toInt ()
     print ("Enter coordinate and:")
     val y = readLine () !!. toInt ()
     if (x> 0 && y> 0)
// conditions with logical operators
         print ("Found in the first quadrant")
     else
         if (x <0 && y> 0)
             print ("It is in the second quadrant")
         else
             if (x <0 && y <0)
                 print ("Found in the third quadrant")
             else
                 if (x> 0 && y <0)
                     print ("Found in the fourth quadrant")
                 else
                     print ("Found on an axis")
 }




// Project30

fun main (parameter: Array <String>) {
//coding180.com
// conditions with logical operators
    print ("Enter first value:")
    val value1 = readLine () !!. toInt ()
    print ("Enter second value:")
    val value2 = readLine () !!. toInt ()
    print ("Enter third value:")
    val value3 = readLine () !!. toInt ()
    val lower = if (value1 < value2 && value1 < value3) value1 else if (value2 <value3) value2 else value3
    val higher = if (value1 > value2 && value1 > value3) value1 else if (value2> value3) value2 else value3
    print ("The oldest in the list is $higher and the lowest $lower")
}


How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Leave a Reply

Your email address will not be published. Required fields are marked *

1 thought on “Lesson 9: Composite conditions with logical operators in Kotlin Language”