C#. The do-while loop. Examples of using

The do-while loop. Examples of using


Contents


1. The do-while loop statement. Features of use. General form

The do-while operator is used to organize a cyclic process. The difference between this statement and the while statement is that the body of the do-while statement will be executed at least once, regardless of the value of the loop execution condition.

The general form of statement

do
{
  // statements, operators
  // ...
}
while (condition)

here

  • condition – condition for the execution of the cyclic process. If the value of condition = True, then the statements placed between curly braces { } (with the words do and while) will be executed.

 

2. The diagram of do-while loop statement

Figure 1 shows the do-while loop.

 C#. The diagram do-while loop operator

Figure 1. The diagram do-while loop operator

 

3. Examples of solving problems with the do-while loop operator
3.1. Calculating the sum of a sequence of numbers

Given a nonempty sequence of integers that ends with zero. Calculate the sum of all numbers in a sequence.

The text of the program for an application such as Console Application is as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp8
{
  class Program
  {
    static void Main(string[] args)
    {
      int number; // input number
      int summ = 0; // sum of numbers

      // The loop of input numbers, 0 - output
      do
      {
        // Enter a number using the keyboard
        Console.Write("Input number (0 - exit): ");
        number = Int32.Parse(Console.ReadLine());

        // Calculate the sum
        summ += number;
      } while (number != 0);

      // Print the sum
      Console.WriteLine("sum = {0}", summ);
      Console.ReadKey();
    }
  }
}

The result of the program

Input number (0 - exit): 1
Input number (0 - exit): 9
Input number (0 - exit): 2
Input number (0 - exit): -3
Input number (0 - exit): 0
sum = 9


 

3.2. Search for a value in a series of numbers

The number a (1<a≤1.5) is given. Among the numbers

find the first, less than a. Program text for an application of Console Application type.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp8
{
  class Program
  {
    static void Main(string[] args)
    {
      double a, t;
      int n;

      // Input the value of a
      Console.Write("a = ");
      a = Convert.ToDouble(Console.ReadLine());

      // Checking the value for correctness.
      if ((a < 0) || (a > 1.5))
      {
        Console.WriteLine("Error. The value of a is incorrect.");
        return;
      }

      n = 1;

      // loop of search n
      do
      {
        n++;
        t = 1 + 1.0 / n;
      } while (t >= a);

      // Display t, n
      Console.WriteLine("t = {0:f6}, n = {1}", t, n);
      Console.ReadKey();
    }
  }
}

The result of the program

a = 1.22
t = 1.200000, n = 5

 

3.3. Determining the number of digits in a number

Given a natural number. Determine the number of digits 7 in it.

The text of the program for an application such of Console Application type is as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp8
{
  class Program
  {
    static void Main(string[] args)
    {
      // Determining the number of digits 7 in the number
      int number;
      int t;
      int k;

      // Input number
      Console.Write("number = ");
      number = Convert.ToInt32(Console.ReadLine());

      if (number<0)
      {
        Console.WriteLine("Error. Incorrect number.");
        Console.ReadKey();
        return;
      }

      // Calculation
      t = number; // make a copy of number
      k = 0;

      // the loop of calculation of k
      do
      {
        if (t % 10 == 7) k++;
        t = t / 10;
      } while (t > 0);

      Console.WriteLine("k = {0}", k);
      Console.ReadKey();
    }
  }
}

The result of the program

number = 45277
k = 2

 

3.4. The definition of patterns in the sequence

Given a natural number. Determine if there is a sequence of its numbers when viewed from left to right sorted in descending order. For example, for the number 9621 the answer is yes, for the number 8340 the answer is no.

Program text for an application of Console Application type

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp8
{
  class Program
  {
    static void Main(string[] args)
    {
      // Determining whether digits of a number form a descending sequence
      int number;
      int t1, t2;
      bool f;

      // Input a number
      Console.Write("number = ");
      number = Convert.ToInt32(Console.ReadLine());

      if (number<0)
      {
        Console.WriteLine("Error. Incorrect number.");
        Console.ReadKey();
        return;
      }

      // Calculation
      t1 = number % 10;
      f = true;

      // the do-while loop
      do
      {
        t2 = t1;
        number = number / 10;
        t1 = number % 10;
        if (t1 < t2)
        {
          f = false;
          break; // the sequence is not decreasing
        }
      } while (number > 10);

      if (f == true)
        Console.WriteLine("The sequence is decreasing.");
      else
        Console.WriteLine("The sequence is not decreasing.");
      Console.ReadKey();
    }
  }
}

The result of the program

number = 88765521
The sequence is decreasing.

 


Related topics