C the while loop

The target

In this article, you’ll learn how to use C while loop statements to repeatedly execute blocks of code based on expressions.

Introduces the C while loop statement

The C while loop statement allows you to repeatedly execute a block of code based on checking the expression at the beginning of each iteration.

Here is the syntax for the while loop:

While (expression) {statement; }Copy the code

How it works:

The while statement first evaluates the value of the expression. If the result is non-zero (true), the while statement executes within its body. This loop continues until the expression becomes 0(or false).

At the beginning of each iteration, the while statement rechecks the expression before executing the statement. Therefore, at some point, the expression needs to become 0(or false) to end the loop. Otherwise, you’ll have a cycle of uncertainty.

When a while statement is first encountered and the expression is zero (or false), the while statement does not execute the statement at all. The program passes control to the statement following the while statement.

The following flowchart illustrates how the while loop works:

C while loop example

Let’s take some examples of using a while loop

1) A simple C while loop statement

The following example uses a while loop statement to display five numbers from 0 to 4:

#include <stdio.h>

int main()
{
    int n = 0;
    while (n < 5)
    {
        printf("%d ", n);
        n++;
    }

    return 0;
}
Copy the code

Output:

0, 1, 2, 3, 4

How it is executed.

First, declare the n variable and initialize it to 0. Second, before entering the loop, check that n is less than 5. Third, display n and add 1 to each iteration. Repeat this cycle until n is not less than 5. Since n begins with 0, the while statement executes five iterations.

2) Develop a number guessing game using a C while loop

The following example demonstrates how to create a number guessing game using a while loop statement

#include <stdio.h> #include <stdlib.h> #include <time.h> int main() {// MAX_GUESS = 4; Int secret_number, // number = -1, // input number = 0; // Get a random number from 0 to 10 srand(time(NULL)); Secret_number = rand() % 10 + 1; while (number ! = secret_number && guess < MAX_GUESS) {// add guess guess++; Printf ("\n Please enter a number (0-10):"); scanf("%d", &number); If (number == secret_number) {printf(" you guessed it \n"); } else {if (number > secret_number) printf(" big \n"); Else printf(" small \n"); If (MAX_GUESS > 0) printf(" rest %d times \n", MAX_GUESS - guess); Else printf(" You lost! The secret number is %d", secret_number); } } return 0; }Copy the code

How it is executed.

First, define the maximum number of guesses:

const int MAX_GUESS = 4;
Copy the code

Step 2: Generate a random number between 0 and 10:

// Get a random number from 0 to 10 srand(time(NULL)); Secret_number = rand() % 10 + 1;Copy the code

Third, allow the user to prompt a number until the number of guesses reaches MAX_GUESS or the number equals secret_number:

while (number ! = secret_number && guess < MAX_GUESS)Copy the code

Fourth, add guesses and hints for input:

// add guess guess++; Printf ("\n Please enter a number (0-10):"); scanf("%d", &number);Copy the code

Fifth, if the input number matches the secret number, the success message will be displayed:

If (number == secret_number) {printf(" you guessed it \n"); }Copy the code

Sixth, if the input number decreases or increases, a message is displayed to the user:

If (number > secret_number) printf(" big \n"); Else printf(" small \n");Copy the code

Seventh, display the remaining guesses. If the guesses are used up, display the failure message:

If (MAX_GUESS > 0) printf(" rest %d times \n", MAX_GUESS - guess); Else printf(" You lost! The secret number is %d", secret_number);Copy the code

conclusion

  • Use a C while loop statement to repeatedly execute code based on the expression checked at the beginning of each iteration.