Lesson 8: Nested If conditional Statement in Kotlin language

Nested If conditional Statement in Kotlin language. We say that a conditional structure is nested when by the real or false branch of a conditional structure there is another conditional structure.

Lesson 8: Nested If conditional Statement in Kotlin language - Lesson 8: Nested If conditional Statement in Kotlin language - Lesson 8: Nested If conditional Statement in Kotlin language - Nested If conditional Statement in Kotlin language
Nested If conditional Statement in Kotlin language

The flow diagram that appears contains two conditional structures. The main one is a compound conditional structure and the second is a simple conditional structure and is contained by the false branch of the first structure.
It is common for even more complex nested conditional structures to occur.

Problem 1

Make a program that asks for three notes of a student by keyboard, calculate the average and print some of these messages:
If the average is > = 7 show “Promoted”.
If the average is > = 4 and <7 show “Regular”.
If the average is < 4 show “Failed”.

Project18 – coding180project.kt

fun main (Parameters: Array <String>) {
//coding180.com
    print ("Enter first note:")
    val nota1 = readLine () !!. toInt ()
    print ("Enter second note:")
    val nota2 = readLine () !!. toInt ()
    print ("Enter third note:")
    val nota3 = readLine () !!. toInt ()
    val average  = (nota1 + nota2 + nota3) / 3
    if (average >= 7)
    print ("Promoted")
    else
    if (average >= 4)
    print ("Regular")
    else
    print ("Free")
}
Lesson 8: Nested If conditional Statement in Kotlin language - Lesson 8: Nested If conditional Statement in Kotlin language - Lesson 8: Nested If conditional Statement in Kotlin language - Nested If conditional Statement in Kotlin language
Nested If conditional Statement in Kotlin language

First we ask if the average is greater than or equal to 7, if so it goes by the branch of the true one of the if showing a message that indicates “Promoted” (with quotes we indicate a text that must be printed on screen).
In case the condition is false, by the branch of false another conditional structure appears if, because we still have to find out if the student’s average is greater than or equal to four or less than four.

We are in the presence of two compound conditional structures.

In none of the true and false blocks of the two if have we set open and closed keys because there is only one instruction in it.

If we use if as expressions we can encode the same program more concisely with the following code:

Project18 – coding180project.kt

fun main (Parameters: Array <String>) {
//coding180.com
    print ("Enter first note:")
    val nota1 = readLine () !!. toInt ()
    print ("Enter second note:")
    val nota2 = readLine () !!. toInt ()
    print ("Enter third note:")
    val nota3 = readLine () !!. toInt ()
    val average = (nota1 + nota2 + nota3) / 3
    val state = if (average >= 7) "Promoted" else if (average >= 4) "Regular" else "Free"
    print ("Status of student $state")
}

Problems proposed

  • Three different numbers are loaded on the keyboard. Display the largest of them.
  • Enter an integer value by keyboard, display a legend that indicates whether the number is positive, null, or negative.
  • Make a program that allows loading a positive integer up to three digits and displays a message indicating if it has 1, 2, or 3 digits. Show an error message if the number of digits is greater.
  • A job applicant, conducted a training test, obtained the following information: total number of questions that were asked and the number of questions that answered correctly. It is asked to create a program that enters the two data by keyboard and report the level of the same according to the percentage of correct answers that has obtained, and knowing that:
    	 Maximum level: Percentage> = 90%.
    	 Average level: Percentage> = 75% and <90%.
    	 Regular level: Percentage> = 50% and <75%.
    	 Out of level: Percent <50%.
    
    

Solution


// Project19

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 > value2)
         if (value1> value3)
             print (value1)
         else
             print (value3)
      else
         if (value2> value3)
             print (value2)
         else
             print (value3)
 }
  Project20
fun main (parameter: Array <String>) {
     print ("Enter an integer value:")
     val value = readLine () !!. toInt ()
     if (value == 0)
         println ("Zero entered")
     else
         if (value> 0)
             println ("A positive value was entered")
         else
             println ("A negative value was entered")
 }
 Project21
fun main (parameter: Array <String>) {
     print ("Enter an integer value with 1,2 or 3 digits:");
//coding180.com 
     val value = readLine () !!. ToInt ()
     if (value <10)
         println ("Has a digit")
     else
         if (value <100)
             println ("It has two digits")
         else
             if (value <1000)
                 println ("It has three digits")
             else
                 println ("Error in data entry.")
 }
  Project22
fun main (parameter: Array <String>) {
    print ("Enter the total number of test questions:")
//coding180.com 
   val totalQuestions = readLine () !!. toInt ()
    print ("Enter the total number of correctly answered questions:")
    val totalCorrect = readLine () !!. toInt ()
    val percentage = totalCorrect * 100 / totalQuestions
    if (percentage >= 90)
    println ("Maximum Level")
    else
    if (percentage >= 75)
        println ("Middle level")
    else
    if (percentage >= 50)
    println ("Regular level")
    else
    println ("Out of level")
}

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!

No ratings yet.

Please rate this

Leave a Reply

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