Почему в одном случае цикл работает, а в другом нет?
using System;
class DemoFor
{
static void Main()
{
int a, b;
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
for ( a ; a < b; a++)
Console.WriteLine(a);
}
}
using System;
class DemoFor
{
static void Main()
{
int a, b;
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
for ( a = a ; a < b; a++)
Console.WriteLine(a);
}
}
Ответы (2 шт):
Автор решения: Igor
→ Ссылка
Ничего не надо "обязательно инициализировать".
for (; a < b; a++)
All the sections of the
forstatement are optional.
Все разделы оператора for являются необязательными. (и могут быть пустыми)
Автор решения: Michael Clifford
→ Ссылка
Как вариант цикл использовать можно таким образом (не указывая инициализации в 1-м аргументе).
public static void Main(string[] args)
{
Console.Write("Input A: ");
int a = Convert.ToInt32(Console.ReadLine());
Console.Write("\nInput B: ");
int b = Convert.ToInt32(Console.ReadLine());
for (; a < b; a++)
{
Console.WriteLine(a);
}
}