c# while 中的两个语句
c# two statements in while
我正在编写一个程序,该程序从键盘获取输入并以以下螺旋方式打印方阵
1 2 3
8 9 4
7 6 5
我写了程序,但是遇到了一个奇怪的错误。
在第 26 行,它给了我一个索引超出范围的异常
while (matrix[row, col] == 0 && col < matrix.GetLength(0) )
但是,如果我在循环内切换两个语句的顺序,异常就消失了吗?这是否意味着 while 循环中两个语句的顺序很重要?如果是,为什么?不应该是如果两个语句都为真就执行循环,如果其中一个为假,不管哪一个,都停止执行。
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpiralMatrixN
{
class Program
{
static void Main(string[] args)
{
//prompt the user to enter n
Console.WriteLine("Enter the value of n");
int n = int.Parse(Console.ReadLine());
int[,] matrix = new int[n,n];
Console.Clear();
System.Console.SetWindowSize(100, 30);
int value = 1;
int col = 0;
int row = 0;
if (n>0 && n<21)
{
while(value <= n*n)
{
while (matrix[row, col] == 0 && col < matrix.GetLength(0) )
{
matrix[row, col++] = value;
value++;
}
col--;
row++;
while (row < matrix.GetLength(1) && matrix[row, col] == 0)
{
matrix[row++, col] = value;
value++;
}
row--;
col--;
while (col >= 0 && matrix[row, col] == 0 )
{
matrix[row, col--] = value;
value++;
}
col++;
row--;
while (matrix[row, col] == 0 && row >= 0)
{
matrix[row--, col] = value;
value++;
}
col++;
row++;
}
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.SetCursorPosition(j * 5, i * 2);
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
}
是的,顺序很重要。 && 子句中的条件按优先顺序执行,如果一个失败则不执行另一个。目前你失败了,因为 matrix[row, col] == 0
首先执行, col
超出范围。所以你应该首先检查 col
(顺便说一句,这是绝对正确的):
while (col < matrix.GetLength(0) && matrix[row, col] == 0)
如果失败,则不会执行第二条语句,您也不会出现错误。这叫做"short-circuit evaluation".
是的,这叫做短路评估。由于您使用的是“&&”,因此只有在第一个条件的计算结果为真后,第二个条件才会被计算。
&&
和 ||
运算符被称为“shortcut 短路运算符”;下半场仅在必要时进行评估。这很有用,原因有二:
- 它在执行过程中效率更高,因为更少的代码是 运行(通常不会 多 ,但如果你正在做 IO 或其他可能的事情),并且
编程方便,前半部分可以作为后半部分的前提条件,甚至可以进行求值。比如,
if (myObj != null && myObj.Name == "something")
如果上面对两半都进行了评估,则只要 myObj 为空,您就会从 myObj.Name
收到错误。
如果无论如何都需要计算表达式的两半,可以使用 &
或 |
运算符。我发现我很少这样做。
&& 中的顺序很重要 && 使用短路 vs & 而不是。在从左到右的 && 表达式中,如果条件为假,则不评估其他条件。
Conditional logical operators
The && and || operators are called the conditional logical operators.
They are also called the "short-circuiting" logical operators.
conditional-and-expression:
inclusive-or-expression
conditional-and-expression && inclusive-or-expression
conditional-or-expression:
conditional-and-expression
conditional-or-expression || conditional-and-expression*
- The && and || operators are conditional versions of the & and |
operators:
- The operation x && y corresponds to the operation x & y,
except that y is evaluated only if x is true.
The operation x || y corresponds to the operation x | y, except that y
is evaluated only if x is false.
An operation of the form x && y or x || y is processed by applying
overload resolution (Section 7.2.4) as if the operation was written x
& y or x | y. Then,
If overload resolution fails to find a single best
operator, or if overload resolution selects one of the predefined
integer logical operators, a compile-time error occurs.
Otherwise, if the selected operator is one of the predefined Boolean
logical operators (Section 7.10.2), the operation is processed as
described in Section 7.11.1.
Otherwise, the selected operator is a user-defined operator, and the
operation is processed as described in Section 7.11.2.
It is not possible to directly overload the conditional logical operators.
However, because the conditional logical operators are evaluated in
terms of the regular logical operators, overloads of the regular
logical operators are, with certain restrictions, also considered
overloads of the conditional logical operators. This is described
further in Section 7.11.2.
我正在编写一个程序,该程序从键盘获取输入并以以下螺旋方式打印方阵
1 2 3
8 9 4
7 6 5
我写了程序,但是遇到了一个奇怪的错误。 在第 26 行,它给了我一个索引超出范围的异常
while (matrix[row, col] == 0 && col < matrix.GetLength(0) )
但是,如果我在循环内切换两个语句的顺序,异常就消失了吗?这是否意味着 while 循环中两个语句的顺序很重要?如果是,为什么?不应该是如果两个语句都为真就执行循环,如果其中一个为假,不管哪一个,都停止执行。
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpiralMatrixN
{
class Program
{
static void Main(string[] args)
{
//prompt the user to enter n
Console.WriteLine("Enter the value of n");
int n = int.Parse(Console.ReadLine());
int[,] matrix = new int[n,n];
Console.Clear();
System.Console.SetWindowSize(100, 30);
int value = 1;
int col = 0;
int row = 0;
if (n>0 && n<21)
{
while(value <= n*n)
{
while (matrix[row, col] == 0 && col < matrix.GetLength(0) )
{
matrix[row, col++] = value;
value++;
}
col--;
row++;
while (row < matrix.GetLength(1) && matrix[row, col] == 0)
{
matrix[row++, col] = value;
value++;
}
row--;
col--;
while (col >= 0 && matrix[row, col] == 0 )
{
matrix[row, col--] = value;
value++;
}
col++;
row--;
while (matrix[row, col] == 0 && row >= 0)
{
matrix[row--, col] = value;
value++;
}
col++;
row++;
}
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.SetCursorPosition(j * 5, i * 2);
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
}
是的,顺序很重要。 && 子句中的条件按优先顺序执行,如果一个失败则不执行另一个。目前你失败了,因为 matrix[row, col] == 0
首先执行, col
超出范围。所以你应该首先检查 col
(顺便说一句,这是绝对正确的):
while (col < matrix.GetLength(0) && matrix[row, col] == 0)
如果失败,则不会执行第二条语句,您也不会出现错误。这叫做"short-circuit evaluation".
是的,这叫做短路评估。由于您使用的是“&&”,因此只有在第一个条件的计算结果为真后,第二个条件才会被计算。
&&
和 ||
运算符被称为“shortcut 短路运算符”;下半场仅在必要时进行评估。这很有用,原因有二:
- 它在执行过程中效率更高,因为更少的代码是 运行(通常不会 多 ,但如果你正在做 IO 或其他可能的事情),并且
编程方便,前半部分可以作为后半部分的前提条件,甚至可以进行求值。比如,
if (myObj != null && myObj.Name == "something")
如果上面对两半都进行了评估,则只要 myObj 为空,您就会从
myObj.Name
收到错误。
如果无论如何都需要计算表达式的两半,可以使用 &
或 |
运算符。我发现我很少这样做。
&& 中的顺序很重要 && 使用短路 vs & 而不是。在从左到右的 && 表达式中,如果条件为假,则不评估其他条件。
Conditional logical operators
The && and || operators are called the conditional logical operators. They are also called the "short-circuiting" logical operators.
conditional-and-expression:
inclusive-or-expression conditional-and-expression && inclusive-or-expression
conditional-or-expression:
conditional-and-expression conditional-or-expression || conditional-and-expression*
- The && and || operators are conditional versions of the & and | operators:
- The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is true.
The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is false.
An operation of the form x && y or x || y is processed by applying overload resolution (Section 7.2.4) as if the operation was written x & y or x | y. Then,
If overload resolution fails to find a single best operator, or if overload resolution selects one of the predefined integer logical operators, a compile-time error occurs.
Otherwise, if the selected operator is one of the predefined Boolean logical operators (Section 7.10.2), the operation is processed as described in Section 7.11.1.
Otherwise, the selected operator is a user-defined operator, and the operation is processed as described in Section 7.11.2.
It is not possible to directly overload the conditional logical operators. However, because the conditional logical operators are evaluated in terms of the regular logical operators, overloads of the regular logical operators are, with certain restrictions, also considered overloads of the conditional logical operators. This is described further in Section 7.11.2.