Lesson 8: Nested If conditional Statement in Kotlin language

Lesson 8: Nested If conditional Statement 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

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

Related posts

Leave a Reply