The basic structure of a do-while loop includes:
Here is the syntax for a do-while loop in C#
do
{
// Code to be executed
} while (condition);
using System;
class Program
{
static void Main()
{
int number;
do
{
Console.Write("Enter a positive number: ");
number = Convert.ToInt32(Console.ReadLine());
if (number <= 0)
{
Console.WriteLine("That is not a positive number. Please try again.");
}
} while (number <= 0);
Console.WriteLine($"You entered: {number}");
}
}
using System;
class Program
{
static void Main()
{
int rows = 2;
int columns = 2;
int[,] matrix = new int[rows, columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
do
{
Console.Write($"Enter a positive number for matrix[{i},{j}]: ");
matrix[i, j] = Convert.ToInt32(Console.ReadLine());
if (matrix[i, j] <= 0)
{
Console.WriteLine("That is not a positive number. Please try again.");
}
} while (matrix[i, j] <= 0);
}
}
Console.WriteLine("Matrix entered:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
}