C#. Nested conditional jump statements

Nested conditional branch operators. The if condition statement as part of other statements. Examples

This topic is a continuation of the topic:


Contents


Search other resources:

1. The concept of nested if statements

The conditional branch operator (any of its forms) can be nested in any other operator that controls the execution of the computational process. These can be other language constructs such as loop statements, when or conditional jump statement, and the like. Any number of levels of operator nesting is allowed.

If the conditional branch operator is nested in another conditional branch operator, then the program code can be something like this

if (condition1)
{
  ...

  if (condition2)
  {
    ...
  }

  ...
}

If the conditional jump operator is nested in the body of the for loop operator, then in the simplest case the program code of such nesting is as follows:

for (...)
{
  ...

  if (...)
  {
    ...
  }
  else
  {
    ...
  }
  ...
}

Similarly, you can implement nesting for any other control statement (while, do-while, when).

 

2. Conditional jump operator as part of other operators. Examples
2.1. Conditional jump statement in the body of other conditional jump statements

Task. The year and number of the month are given for this year. Determine how many days this month.

Solution.

fun main(args:Array<String>)
{
  // Nested if operators.
  // Determine the number of days in the specified month of the specified year
  // 1. Declaring variables
  val year : Int   // year
  val month : Int // month
  val days : Int   // result - number of days

  // 2. Data input
  print("year = ")
  year = readLine().toString().toInt()
  print("month = ")
  month = readLine().toString().toInt()

  // 3. Checking if the data is entered correctly
  if ((year < 0) || (month<1) || (month>12))
  {
    println("Incorrect input.")
    return
  }

  // 4. Evaluation - nested if statements
  if ((month==4)||(month==6)||(month==9)||(month==11))
    days = 30
  else
  if (month==2) // check if february
  {
    // Leap year definition - nested if
    if (year % 400 == 0)
      days = 29
    else
    if (year % 100 == 0)
      days = 28
    else
    if (year % 4 == 0)
      days = 29
    else
      days = 28
  }
  else
    days = 31

  print("days = " + days)
}

Test example

year = 2020
month = 2
days = 29

 

2.2. The conditional jump operator in the body of the for loop. Example

Task. Display all integers in the range from a to b that are multiples of c.

Solution.

 

fun main(args:Array<String>)
{
  // Nested if operator in the body of the loop for

  // 1. Declaring variables
  val a : Int
  val b : Int
  val c : Int

  // 2. Input numbers
  print("a = ")
  a = readLine().toString().toInt()
  print("b = ")
  b = readLine().toString().toInt()
  print("c = ")
  c = readLine().toString().toInt()

  // Checking, is the input correct
  if ((a>b) || (c<=0))
  {
    println("Incorrect input.")
    return
  }

  // 3. Input numbers
  print("[" + a + "; " + b + "] % " + c + " => ")
  for (i:Int in a .. b) // The for loop
    if (i%c == 0) // The if statement in the body of the for loop
      print(" " + i)
  println()
}

Test example

a = -22
b = 34
c = 3
[-22; 34] % 3 => -21 -18 -15 -12 -9 -6 -3 0 3 6 9 12 15 18 21 24 27 30 33

 

2.3. The conditional branch operator in the while statement. Example

Task.

A natural number n and real numbers a1, a2, …, an are given. Determine the sum of those real numbers that are greater than the number x.

Solution.

fun main(args:Array<String>)
{
  // Processing data during keyboard input
  // 1. Declaring variables
  val n : Int
  var a : Double
  val x : Double
  var i : Int
  var summ = 0.0   // The required sum - result

  // 2. Input data
  print("n = ")
  n = readLine().toString().toInt()

  print("x = ")
  x = readLine().toString().toDouble()

  // 2. The while loop for entering numbers and calculating the  sum   
  println("Enter numbers:")

  i = 0
  while (i<n)
  {
    // Input number a
    print("=> ")
    a = readLine().toString().toDouble()

    // Calculate the sum
    if (a>x)
      summ += a

    // increase counter
    i++;
  }

  // 4. Display the result
  println("summ = " + summ)
}

Test example

n = 6
x = 2.5
Enter numbers:
=> 3.1
=> 2.8
=> 1.4
=> 2.5
=> 2.2
=> 4.3
summ = 10.2

 

2.4. The conditional branch operator in the do-while statement. Example

Task.

You are given a sequence of integers that ends in zero. Find the minimum and maximum values in this sequence.

Solution.

A do-while loop is best for entering data before getting zero.

 

fun main(args:Array<String>)
{
  // Processing data during keyboard input
  // 1. Declaring variables
  var number : Int // entered number
  var max = 0
  var min = 0
  var f_first = true // signals the first iteration of the loop

  // 2. Cycle for entering numbers
  print("Enter numbers:")

  do {
    print("=> ")
    number = readLine().toString().toInt()        

    if (number==0) break

    // If the first iteration
    if (f_first) {
      max = number
      min = number
      f_first = false
    }

    // Calculate maximum and minimum
    if (max<number) max = number
    if (min>number) min = number
  } while (number!=0)

  // 3. Display the result
  println("max = " + max)
  println("min = " + min)
}

Test example

Enter numbers:
=> 5
=> 1
=> 3
=> 4
=> 0
max = 5
min = 1

 


Related topics