c - 内循环中的break语句如何工作?

标签 c arrays loops nested break

我是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 函数将允许代码在循环中中断,例如 whilefor直到,等等。它为程序员提供了一种在满足要求循环停止的条件时停止代码的方法。 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/

相关文章:

c - 什么是 C 中链表的忠实替代品?

java - 递归地找到java中数组中最长的递增序列

c# - 子列表项未在 C# 中显示

arrays - 使用 bash/expect 中的变量索引到数组中

c - MPI 程序未按预期工作

c - 为什么我必须按 CTRL + Z 3 次才能发送 EOF?

c - 什么会导致 "regular"类型的文件在 Linux 中不可读以及如何识别这个

Java 将 Int 转换为 Hex 并将其粘贴到字节数组元素中

php - Ajax 查询 sql 并发送单行响应

创建游戏 Rush Hour,但错误检查器出现问题。 (C)