这个循环是什么意思:'while(string[i])'
What does this loop mean: 'while(string[i])'
我有这行代码我看不懂。
char string[100];
while(string[i])
{
//statements
i++;
}
您在问这是否是遍历字符串的最佳方式。我会说不,指针算法是要走的路。如果你是索引,那么你必须用两个变量来操作,而不是一个。
const char *str = "Hello World";
while (*str != '[=10=]') {
++str;
}
或
for (; *str != '[=11=]'; ++str)
迭代:
We use the term iteration, we are usually talking about loops. For, while, and do...while loops in C are loops that will execute as long as a condition is true. Iteration is one of the reasons that computer programs exist in the first place.
From the page of Recursion & Iteration in C
您可以使用 for 循环遍历数组元素。
string cars[4] = {"Volvo", "BMW", "Fiat", "Ferrari","Mazda"};
for(int i = 0; i < 5; i++) {
cout << cars[i] << "\n";
}
您也可以使用 while 循环遍历数组元素。
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int i = 0;
do
{
printf("%d\n", arr[i]);
i++;
} while (i < 10);
这个:
while (string[i])
{
//statements
i++;
}
严格等同于:
while (string[i] != 0)
{
//statements
i++;
}
在 C 中,如果表达式的值为 0 则表达式为假,如果其值不为零则为真。
你也可以这样写
if (Foo)
...
而不是:
if (Foo != 0)
...
在语句 while (string[i])
中,我们检查 string[i]
的值是否等于 [=13=]
(ASCII 为 0)。如果 string[i]
等于某个字符,则进入循环。如果string[i] = [=15=]
(字符串结尾),则退出循环。
#include <stdio.h>
int main()
{
char string[100] = "Krishna";
int i=0;
while (string[i]) // same as while(string[i] != '[=10=]')
{
printf("%c",string[i]);
i++;
}
return 0;
}
输出为:Krishna
C 没有与整数分开的布尔值类型 (true/false)。在 C 语言中,如果整数非零则为真,如果为零则为假。所以 while (string[i])
是一个循环,当 string[i]
为非零时运行,即当 string
中索引 i
处的字符为非零时。
写成 while (string[i] != 0)
或 while (string[i] != '[=15=]')
是等价的,而且可以说更清楚。 (请注意,0
和 '[=17=]'
都是 整数 零,这也是值为零的字符,因为 C 也没有字符和整数的不同类型。这不是数字 0 的字符,它应该是 '0'
。)
C 中的字符串表示为空终止:字符串的结尾由值为 0 的字符指示。(因此,C 字符串不能包含字符 0。可以表示字符串不同,但它们不会是标准库函数(如 strlen
、strcpy
等)意义上的字符串)
因此while (string[i])
循环直到到达字符串的末尾。这是一个很常见的成语。
我有这行代码我看不懂。
char string[100];
while(string[i])
{
//statements
i++;
}
您在问这是否是遍历字符串的最佳方式。我会说不,指针算法是要走的路。如果你是索引,那么你必须用两个变量来操作,而不是一个。
const char *str = "Hello World";
while (*str != '[=10=]') {
++str;
}
或
for (; *str != '[=11=]'; ++str)
迭代:
We use the term iteration, we are usually talking about loops. For, while, and do...while loops in C are loops that will execute as long as a condition is true. Iteration is one of the reasons that computer programs exist in the first place.
From the page of Recursion & Iteration in C
您可以使用 for 循环遍历数组元素。
string cars[4] = {"Volvo", "BMW", "Fiat", "Ferrari","Mazda"};
for(int i = 0; i < 5; i++) {
cout << cars[i] << "\n";
}
您也可以使用 while 循环遍历数组元素。
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int i = 0;
do
{
printf("%d\n", arr[i]);
i++;
} while (i < 10);
这个:
while (string[i])
{
//statements
i++;
}
严格等同于:
while (string[i] != 0)
{
//statements
i++;
}
在 C 中,如果表达式的值为 0 则表达式为假,如果其值不为零则为真。
你也可以这样写
if (Foo)
...
而不是:
if (Foo != 0)
...
在语句 while (string[i])
中,我们检查 string[i]
的值是否等于 [=13=]
(ASCII 为 0)。如果 string[i]
等于某个字符,则进入循环。如果string[i] = [=15=]
(字符串结尾),则退出循环。
#include <stdio.h>
int main()
{
char string[100] = "Krishna";
int i=0;
while (string[i]) // same as while(string[i] != '[=10=]')
{
printf("%c",string[i]);
i++;
}
return 0;
}
输出为:Krishna
C 没有与整数分开的布尔值类型 (true/false)。在 C 语言中,如果整数非零则为真,如果为零则为假。所以 while (string[i])
是一个循环,当 string[i]
为非零时运行,即当 string
中索引 i
处的字符为非零时。
写成 while (string[i] != 0)
或 while (string[i] != '[=15=]')
是等价的,而且可以说更清楚。 (请注意,0
和 '[=17=]'
都是 整数 零,这也是值为零的字符,因为 C 也没有字符和整数的不同类型。这不是数字 0 的字符,它应该是 '0'
。)
C 中的字符串表示为空终止:字符串的结尾由值为 0 的字符指示。(因此,C 字符串不能包含字符 0。可以表示字符串不同,但它们不会是标准库函数(如 strlen
、strcpy
等)意义上的字符串)
因此while (string[i])
循环直到到达字符串的末尾。这是一个很常见的成语。