# The while loop operator. General form. Examples of tasks solving

## The while loop operator. General form. Examples of tasks solving

### Contents

##### 1. The purpose of the while loop operator. General form. Features of use

The while loop operator is intended for organizing a cyclic process in which the execution of each subsequent iteration is determined based on the truth of a certain condition. The while loop operator is also called the loop operator with a precondition. The general form of the while loop operator is as follows:

`while (condition) statement;`

here

• condition – some condition according to the syntax of the language C#. The statement is executed as long as the condition = true. As soon as the value of the condition becomes false, the cyclic process is terminated and the following statements are executed after the while;
• statement – one or more operators. If in a while loop you need to execute several statements at the same time, then these statements are taken in curly brackets {}.

The body of the loop (operator) is executed as long as the condition value is true. The loop operator must be organized so that ultimately the condition value becomes false. Otherwise, the program will “hang” as an infinite loop will exit.

The general form of the while loop operator in which several (two or more) statements are executed is as follows

```while (condition)
{
statement1;
statement2;
...
statementN;
}```

### ⇑

##### 2. The concept of nested while loops

A while loop can be nested in other constructions. These constructions can be:

• loops for, while, do…while;
• conditional statement if;
• switch statement.

The number of nesting levels in which a while loop can be used is unlimited.

### ⇑

##### 3. The operator while diagram

Figure 1 shows a diagram of a while statement. As you can see from the diagram, the condition is checked first. If the condition value is True, the loop body is executed. The exit from the cyclic process occurs if the value of the condition becomes False. Figure 1. Diagram of while operator

### ⇑

##### 4. Examples of solving problems with the while loop operator
###### 4.1. Output calculation results in while loop

Task. Get a table of temperatures Celsius tc from -50 to +50 degrees and their equivalents on the Fahrenheit scale tf, using the formula Solution. The text of the program for solving this problem for an application of the Console Application type, using the while loop, is the following

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
// temperature scale Celsius => Fahrenheit
int tc; // the current value of Celsius temperature
double tf; // Fahrenheit temperature value

tc = -50;

Console.WriteLine("Temperature Scale: Celsius - Fahrenheit ");

while (tc <= 50)
{
tf = 9.0 / 5.0 * tc + 32;
tc++;
Console.WriteLine("{0} C   => {1} F", tc, tf);
}
}
}
}```

### ⇑

###### 4.2. Recurrent algorithms. Calculating the n-th term in the sequence

Task. Determine n-th member of the sequence of numbers (n> 2):

xn=xn1 + xn2; x0 = x1 = 1

Solution. The text of the program for solving this task is as follows:

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
// calculation of the n-th term of the sequence
int n; // n>2
int x0, x1, x2;
int i;

// enter the value of n
Console.Write("n = ");

// initial assignments
x0 = x1 = 1;
x2 = x0 + x1;
i = 1;

while (i <= n - 3) // calculation
{
x0 = x1;
x1 = x2;
x2 = x0 + x1;
i++;
}
Console.WriteLine("{0} ", x2);
}
}
}```

After executing the above code, the following result will be obtained.

```n = 7
13```

### ⇑

###### 4.3. Calculate the values of the number π with a given accuracy

Task. Calculate the values of the number π, using the formula: Determine a number of terms need to determine the number π to within 7 decimal places.

Solution. To solve such problems, the while loop fits perfectly. The program code for solving the problem for an application of the Console Application type is given below.

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
// calculate the value of Pi
double pi; // result
int denom; // denominator
int sign; // a variable that changes the sign of the number + to -, and vice versa
double t; // additional variable - current accuracy
const double eps = 0.0000001; // accuracy of 7 decimals
int k; // the number of terms to obtain a predetermined accuracy

// initial preparations
k = 0;
pi = 0;
denom = 1;
sign = 1;
t = 1.0 / denom * sign;

// while loop - calculation
while (t > eps)
{
pi = pi + 4 * t * sign;
sign = -sign; // change the sign
denom += 2; // change the denominator
t = 1.0 / denom;
k++; // increase by 1 the number of terms
}

// output of the result
Console.WriteLine("Pi = {0}", pi);
Console.WriteLine("k = {0}", k);
}
}
}```

As a result of executing the above code, the following result was obtained

```Pi = 3.14159245358978
k = 5000000```

### ⇑

##### 5. Examples of solving problems in which the while operator is nested
###### 5.1. The while loop is nested in the switch select statement.

In the example, the while loop is nested in the switch statement. Depending on the value of the variable a, the appropriate option of finding the amount is selected:

• if a = 1, then s = 5 + 10 + … + 100 is calculated;
• if a = 2, then s = 5 + 10 + … + 20 is calculated;
• if a = 3, then s = 5 + 10;
• otherwise, s = 100 is calculated.

The program code for an application of the Console Application type that solves this task is the following:

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int a;
int s = 0;
int i;
a = 7;

// the choice of algorithm for solving the problem depending on the value of a
switch (a)
{
case 1:
i = 0;
while (i < 100)
{
i = i + 5;
s = s + i;
}
break;
case 2:
i = 0;
while (i < 20)
{
i = i + 5;
s = s + i;
}
break;
case 3:
i = 0;
while (i < 10)
{
i = i + 5;
s = s + i;
}
break;
default:
s = 100;
break;
}
Console.WriteLine("s = {0}", s);
}
}
}```

As a result of executing the above code, the result will be displayed

`s = 100`

### ⇑

###### 5.2. The while loop is nested in a for loop statement

Task. Find the number of divisors of any of the integers from 120 to 140.

Solution. Below is the program code that solves this problem for an application of the Console Application type:

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
// the number of divisors of any of the integers from 120 to 140
int i;
int up;
int t;
int k;

for (i = 120; i <= 140; i++)
{
// determine the maximum allowable value of the divider
up = i / 2;
k = 0; // number of dividers
t = 1;

while (t <= up) // nested while loop
{
if (i % t == 0) // definition: divider or non divider
k++;
t++;
}

// Display the divider
Console.WriteLine("{0}: k = {1}", i, k);
}
}
}
}```

As a result of executing the above code, the following result will be displayed

```120: k = 15
121: k = 2
122: k = 3
123: k = 3
124: k = 5
125: k = 3
126: k = 11
127: k = 1
128: k = 7
129: k = 3
130: k = 7
131: k = 1
132: k = 11
133: k = 3
134: k = 3
135: k = 7
136: k = 7
137: k = 1
138: k = 7
139: k = 1
140: k = 11```