[英]Is it correct to use a do-while loop inside a for loop? Why and why not?
//program to count words
#include<stdio.h>
int main()
{
int i;
int word = 0;
char sent[] = "How are you doing mister"; //character pointer
for (i = 0; sent[i] != '\0'; i++)
{
do
{
word++;
} while (sent[i] == ' ');
}
printf("There are %d words in the sentence\n", word + 1); //words are always 1 more than the no. of spaces.
return 0; //or word=1;
}
This is a code for counting the number of words.这是一个计算单词数量的代码。 Please tell me why cannot we use do-while loops inside a for loop.
请告诉我为什么我们不能在 for 循环中使用 do-while 循环。 Or if we can, how to do it.
或者如果可以的话,怎么做。
Nesting the various composite statements such as for
and do
/ while
is allowed in C at least up to 127 levels, as specified in 5.2.4.1 Translation limits .如5.2.4.1 Translation limits 中规定的,C 中允许嵌套各种复合语句,例如
for
和do
/ while
至少最多 127 个级别。
The problem is not a syntax issue, but rather a conceptual one:问题不是语法问题,而是概念问题:
your do
/ while
loop iterates on a constant condition as neither i
, nor sent
is modified in the body or in the condition of the loop, leading to an infinite loop if sent[i]
is a space.您的
do
/ while
循环在恒定条件下迭代,因为i
和sent
均未在正文或循环条件中进行修改,如果sent[i]
是空格,则会导致无限循环。
counting spaces is not a correct approach to counting words in a string: ""
has 0
words, not 1
per your intended code, " "
also but you would get 2
and "AB"
only has 2
words, not 3
.计算空格不是计算字符串中单词的正确方法:
""
有0
单词,不是每个你想要的代码1
个, " "
也是,但你会得到2
而"AB"
只有2
单词,而不是3
。
you should count the number of transitions from space to non space, starting with an implicit space before the beginning of the string.您应该计算从空格到非空格的转换次数,从字符串开头之前的隐式空格开始。
note also that char sent[] = "...";
还要注意
char sent[] = "...";
is not a character pointer, but a character array.不是字符指针,而是字符数组。
Here is a modified version:这是一个修改后的版本:
//program to count words
#include <stdio.h>
int main() {
int i, words, last;
char sent[] = "How are you doing mister?";
words = 0;
last = ' ';
for (i = 0; sent[i] != '\0'; i++) {
if (sent[i] != ' ' && last == ' ')
word++;
last = sent[i];
}
printf("There are %d words in the sentence '%s'\n", words, sent);
return 0;
}
In my experience proof reading code, do
/ while
loops tend to be written incorrectly, especially by beginners, missing test conditions or broken in some other way.根据我校对代码的经验,
do
/ while
循环往往会被错误地编写,尤其是初学者、缺少测试条件或以其他方式损坏。 I you think a do
/ while
loop solves a given problem, think again, a for
loop might be a safer approach.我认为
do
/ while
循环可以解决给定的问题,再想一想, for
循环可能是一种更安全的方法。 The only place do
/ while
loops are needed is in a macro expansion where you want to combine multiple statements into a single compound statement:唯一需要
do
/ while
循环的地方是在宏扩展中,您希望将多个语句组合成一个复合语句:
#define swap_ints(a, b) do { a ^= b; b ^= a; a ^= b; } while (0)
Note however that the swapping method in this macro is inefficient and macros are very error prone and should be avoided even more than do
/ while
loops :)但是请注意,此宏中的交换方法效率低下,并且宏非常容易出错,比
do
/ while
循环更应该避免:)
It is completely valid to nest a do-while
loop within a for
loop.在
for
循环中嵌套do-while
循环是完全有效的。 Syntactically, there is nothing wrong with your program.从语法上讲,您的程序没有任何问题。
However, as others have described, your nested loop will never terminate when sent[i] == ' '
.但是,正如其他人所描述的,当
sent[i] == ' '
时,您的嵌套循环永远不会终止。 Your program has bugs, but they have nothing to do with the validity of nesting loops.你的程序有错误,但它们与嵌套循环的有效性无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.