C while loop Complete Tutorial

In this section, we will learn what the while loop statement is and how to use it in C.

What is while Loop in C?

The keyword `while` means: during the time that something is happening.

For example:

  • They arrived while we were having dinner.
  • While she was asleep, thieves broke in and stole her handbag.
  • She met Andy while working on a production of Carmen.

In each of the three examples above, something is repeated for a while.

In the first example, it’s eating.

In the second example, it’s sleep.

In the second example, it’s work.

In the world of computing, there are times that we want to repeat a process for a while and as long as a condition is true.

This is where the `while` loop can help.

Using the `while` loop we can develop a program that can repeat the execution of a block of code as long as a condition is true and it’ll break the loop the moment that condition is no-longer true.

C while loop syntax

Here’s the structure of `while` loop:

while (condition) {
  // the code block to be executed
}

`while`: To create a while loop, we start with the keyword, while.

`()`: within the parentheses that come after the `while` keyword, we define the condition. Here we put an expression that result a boolean value (either true or false). Now the result of this condition must be true so the CPU can run the instructions within the body of the loop.

`{}`: the body of the `while` loop starts from the open curly brace `{` and ends with closing curly brace`}` and of course within the body of this block, we set the instructions of the loop to be executed.

Example: creating a while loop in C

#include <stdio.h>

int main() {

    int age = 15;

    while (age <21){
        printf("She is: %d so she can't go to a bar!\n",age);
        age++;
    }
    printf("Now she is: %d so she can go to a bar. \n", age);

    return 0;
}

Output:

She is: 15 so she can't go to a bar!

She is: 16 so she can't go to a bar!

She is: 17 so she can't go to a bar!

She is: 18 so she can't go to a bar!

She is: 19 so she can't go to a bar!

She is: 20 so she can't go to a bar!

Now she is: 21 so she can go to a bar.

How does `while` loop work?

In the example above, the condition within the `while` loop is `age<21` which is true because the age is 15 the first time that the condition is being checked. So the body of the `while` loop will be executed.

Note: when it comes to the while loop, the CPU will first check the condition of the loop and then, if the result was true, it will move on to the body of that loop to run its instructions. So, if the loop’s condition was false in the first place, the loop’s body won’t be executed at all.

Within the body of the `while` loop, there’s the `printf` function, which displays a message on the screen.

Then, at the next line, we’re incrementing the value of the `age` variable by 1.

Note: you should work on the condition set in the `while` loop, within the body of that loop, so at some point, the condition of the loop becomes false. If you don’t do this, there’s a great chance that your program will stuck in a loop that never ends (At least not until the OS stops the program).

We know that the while loop will repeat until the condition of the `while` loop becomes false.

In our example, every time the loop repeats itself, it increases the value of the `age` variable by 1 in order to finally bring the value to 21. In this case, the condition of the loop will no-longer result true and so the loop will break after the value of the age variable reached to 21.

How to exit while loop in C?

By default, the moment the result of a condition becomes false, that loop will break and the CPU will continue to run the statements after the body of the target while loop. But if we want to interrupt a loop sooner than its arranged lifetime, then we can use statements like the `break` or `return` in the body of the loop itself.

If we use the `return` statement within the body of a while loop, it will terminate the work of not just the while loop but also the method that this loop is in it.

Note: please check the `break` section to learn about this statement.

C Infinite while loop

If the condition within a while loop is always true, an infinite loop will be created!

This means the body will run for as long as the program is not stopped.

The simplest way of creating an infinite loop is by using a non-zero value as the condition of a loop.

Example: creating Infinite while loop in C

#include <stdio.h>

int main() {
    int n = 1; 
    while(n){
        n++; 
        if (n == 100){
            break;
        }
    }
    printf("The final value of the n variable is: %d",n);
    return 0;
}

Output:

The final value of the n variable is: 100

Here, the condition of the while loop is a non-zero value. If we didn’t call the `break` statement within the body of this loop, the loop would’ve run forever!

Note: please check the `break` section to learn more about this statement.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies