📜  C 中的 Sentinel 和计数器控制循环之间的区别

📅  最后修改于: 2021-09-12 10:46:39             🧑  作者: Mango

哨兵控制回路

哨兵控制循环也称为无限重复循环,因为在循环开始执行之前迭代次数是未知的。在哨兵控制的循环中,一个叫做哨兵值的特殊值用于将循环控制表达式从真变为假,以确定是否执行循环体。当我们事先不知道循环将执行多少次时,哨兵控制循环很有用。哨兵控制循环的一个例子是处理来自未知大小的文本文件的数据。

下面是用 C 语言说明哨兵控制循环的程序:

C
// The following program does the work
// of finding a length of a string using
// sentinel controlled loop
#include 
  
// Function to find the
// length of the string
void lengthOfString(char* string)
{
    int count = 0, i = 0;
    char temp;
  
    // Pointer to string
    temp = string[0];
  
    // Iterate till temp points to NULL
    while (temp != '\0') {
        count++;
        i++;
        temp = string[i];
    }
  
    // Print the length of the string
    printf("The length of string is %d",
           count);
}
  
// Driver Code
int main()
{
    // Given String
    char string[] = "GeeksForGeeks";
  
    // Function Call
    lengthOfString(string);
    return 0;
}


C
// Program to print first K
// natural numbers
#include 
  
// Function to print the Natural Number
void printNaturalNumbers(int K)
{
    int i = 1;
    while (i <= K) {
  
        // Print the number i
        printf("%d ", i);
        i++;
    }
    return;
}
  
// Driver Code
int main()
{
    // Given Number N
    int N = 5;
  
    // Function Call
    printNaturalNumbers(N);
    return 0;
}


输出:
The length of string is 13

计数器控制回路

计数器控制循环也称为确定重复循环,因为在循环开始执行之前迭代次数是已知的。反控回路具有以下组件:

  1. 一个控制变量。
  2. 在循环的每次迭代中修改控制变量的增量(或减量)值。
  3. 检查循环是否应该继续的循环终止条件。

由于计数器控制循环是由一个计数器值控制的,每次迭代计数器值都会以一个确定的值增加或减少,并检查条件,因此循环执行的次数变得确定。任何涉及确定迭代的任务都可以使用计数器控制循环来解决,例如打印前 10 个自然数。

C

// Program to print first K
// natural numbers
#include 
  
// Function to print the Natural Number
void printNaturalNumbers(int K)
{
    int i = 1;
    while (i <= K) {
  
        // Print the number i
        printf("%d ", i);
        i++;
    }
    return;
}
  
// Driver Code
int main()
{
    // Given Number N
    int N = 5;
  
    // Function Call
    printNaturalNumbers(N);
    return 0;
}
输出:
1 2 3 4 5

C 中的Sentinel 和 Counter Controlled Loop之间的主要区别在于,在 Sentinel Controlled Loop 中,具体执行多少次循环体是未知的,而在 Counter Controlled Loop 中,循环体将执行多少次是已知的。

C 中的 Sentinel 和 Counter Controlled Loop 之间的区别如下:

S.NO. BASIS OF COMPARISON SENTINEL CONTROLLED LOOP COUNTER CONTROLLED LOOP
1. Definition A sentinel controlled loop is the indefinite repetition loop as the number of repetitions is not known before the loop begins executing A counter controlled loop is the definite repetition loop as the number of repetitions is known before the loop begins executing
2. Controlling variable Controlled variable variable used is know as sentinel variable. Controlled variable used is know as counter.
3. Number of iteration Unknown number of iteration Known number of iteration.
4. Value of variable The value of the variable is not strict and it varies. The value of the variable is strict.
5. Limitation of variable The Limitation of the variable is strict. The Limitation of the variable is strict also.
6. Example A do….while loop is an example of sentinel controlled loop. A while loop is an example of counter controlled loop.
想要从精选的视频和练习题中学习,请查看 C 基础到高级C 基础课程。