我是C语言新手。请帮我。 内循环和外循环之间的break语句如何工作? 如果我在这种情况下使用break语句,哪个循环将被终止?
#include<stdio.h>
main(){
int arr[67778], i, j, k, min, max, n, count = 0;
scanf("%s"`enter `enter code here`code here`, &arr);
n = strlen(arr);
for(j = 0; j < n; j++){
for(i = 1; i< n; i++){
if(arr[j] == arr[j]){
count++;
}
else break;
}
count = 0;
}
if(count >= 6){
printf("Yes\n");
}
else printf("No\n");
}
通过这个程序,如果有 7 个输入旁边有相同的字符,我只想打印“YES”,否则应该打印 NO。 请帮助我
最佳答案
为了回答您的主要问题,break
函数将允许代码在循环中中断,例如 while
、for
,直到
,等等。它为程序员提供了一种在满足要求循环停止的条件时停止代码的方法。 break
仅中断当前范围内的循环。
然而在实践中,不建议使用break,因为还有其他循环结构可以以更安全、更容易理解的方式完成您寻求的任何任务。如果您需要停止循环中的while,请使用while(或until,或其他)。仅在绝对必要时才在代码中添加中断,如果您不运行汇编代码,那么这种情况的用例将非常。
~~~~~~~~~~~~~~~~
至于房间里的另一头大象,C 是一种你可以用它做任何事情的语言,而且只有当你愿意在指针和内存处理方面亲自动手时,它才能发挥作用。我强烈建议您熟悉指针、内存处理、参数和整体程序设计,因为您最初传递的代码非常微不足道。如果我们想成为更好的程序员,我们一生中都必须编写一次糟糕的代码。另外,以下代码将读取传递给它的输入(在命令行)并查找传递的输入的几个重复字符。
int main( int argc, char *argv[] )
{
// Check to see if no arguments were passed to program.
// Remember, argv[0] is the program you are calling
if (argc < 2)
{
printf("You need to specify some input for this to work.\n");
return; // End program early
}
int i = 0; // Loop control
int count = 0; // Repetitive char count. After init, it will be set to 1
char check = *(argv[1] + i); // Store the first character to check
// argument's character not null and char sequence less than 7
while (*(argv[1] + i) != 0 && count < 7)
{
// The last character is the same as the current one
if (check == *(argv[1] + i))
{
count++; // Increment count.
}
else
{
count = 1; // Reset count and try again.
check = *(argv[1] + i); // Store a character to check
}
i++;
}
char* result = !(count < 7) ? "yes" : "no"; // Boolean for yes or no
printf("%s\n", result);
return 0;
}
关于c - 内循环中的break语句如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50045874/