Lesson 9: Composite conditions with logical operators in Kotlin Language

Lesson 9: Composite conditions with logical operators in Kotlin Language

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

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

 

About The Author

Related posts

1 Comment

Leave a Reply