Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 如何设置while循环计数器?什么时候应该是1,什么时候应该是0?_Algorithm - Fatal编程技术网

Algorithm 如何设置while循环计数器?什么时候应该是1,什么时候应该是0?

Algorithm 如何设置while循环计数器?什么时候应该是1,什么时候应该是0?,algorithm,Algorithm,如何设置while循环计数器?什么时候应该是1,什么时候应该是0 通常,我应该如何从while循环问题开始?a根据语言的不同,通常使用布尔值,而不是计数器 while (condition) { // Do something until condition == false } 对于“计数器”样式的循环,通常需要(同样,在大多数语言中)a来代替。a根据语言的不同,通常使用布尔值,而不是计数器 while (condition) { // Do something until

如何设置while循环计数器?什么时候应该是1,什么时候应该是0

通常,我应该如何从while循环问题开始?

a根据语言的不同,通常使用布尔值,而不是计数器

while (condition)
{
    // Do something until condition == false
}
对于“计数器”样式的循环,通常需要(同样,在大多数语言中)a来代替。

a根据语言的不同,通常使用布尔值,而不是计数器

while (condition)
{
    // Do something until condition == false
}

对于“计数器”样式的循环,您通常需要(在大多数语言中也是如此)一个替换。

这取决于您正在做什么以及您想要完成什么

如果在数组中进行迭代,则可能需要以
0
开始计数器,因为数组是
0
索引的(数组的第一个元素位于位置
0
)。例如:

int integerArray[] = {1, 2, 3}
int counter = 0;
while ( counter < 3 )
{
  System.out.println(integerArray[counter]);
  ++counter;
}
int counter = 0;
while ( counter < 100 )
{
  //prints the numbers 0-99
  System.out.println(counter);
  ++counter;
}

int counter = 1;
while ( counter < 101 )
{
  //prints the numbers 1-100
  System.out.println(counter);
  ++counter;
}
实际上,对于这两种情况,
for
循环可能会更好地为您服务,但同样的概念适用于:

for (int i = 0; i < 100; ++i)
{
  //prints the numbers 0-99
  System.out.println(i);
}
for(int i=0;i<100;++i)
{
//打印数字0-99
系统输出打印LN(i);
}

这取决于你在做什么和你想完成什么

如果在数组中进行迭代,则可能需要以
0
开始计数器,因为数组是
0
索引的(数组的第一个元素位于位置
0
)。例如:

int integerArray[] = {1, 2, 3}
int counter = 0;
while ( counter < 3 )
{
  System.out.println(integerArray[counter]);
  ++counter;
}
int counter = 0;
while ( counter < 100 )
{
  //prints the numbers 0-99
  System.out.println(counter);
  ++counter;
}

int counter = 1;
while ( counter < 101 )
{
  //prints the numbers 1-100
  System.out.println(counter);
  ++counter;
}
实际上,对于这两种情况,
for
循环可能会更好地为您服务,但同样的概念适用于:

for (int i = 0; i < 100; ++i)
{
  //prints the numbers 0-99
  System.out.println(i);
}
for(int i=0;i<100;++i)
{
//打印数字0-99
系统输出打印LN(i);
}

是的,当然我有一个循环,我只需要计数器来知道我到目前为止所做的操作数或循环数,它们就像几千个一样,它们确实需要一个计数器。是的,当然我有一个循环,我只需要计数器来知道我到目前为止所做的操作数或循环数,它们就像几千个一样,它们确实需要一个计数器。事实上,使用while循环很容易发生灾难。如果您在while循环中有任何条件语句,那么您会突然有几个代码分支需要遵循,所有这些都需要您更新条件语句,否则将面临无限循环的可能性!我遇到过初级程序员在网站代码中编写无限循环的问题,因为他们使用
while
而不是
for
,这对于系统管理员来说是一个头痛的问题,他们会发现所有CPU核在大型网站上运行一段时间后都会旋转。事实上,使用while循环很容易发生灾难。如果您在while循环中有任何条件语句,那么您会突然有几个代码分支需要遵循,所有这些都需要您更新条件语句,否则将面临无限循环的可能性!我遇到过初级程序员在网站代码中编写无限循环的问题,因为他们使用
while
而不是
for
,这对于系统管理员来说是一个头痛的问题,他们会发现所有的CPU核在一个大网站上运行了一段时间后都在旋转。