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.

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”.

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:

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: