<commit_before>---
title: Loops of all kinds
---
# Loops of all kinds in C
Loops are what you use when you have code that you want to loop, meaning that after it runs, you might want it to loop around to the beginning and run again. There are a few of these in C.


They can be categorized as:

(a) Entry controlled loops- Loops in which the condition is checked before every iteration. e.g. for loop, while loop
(b) Exit controlled loop- Loops in which the execution takes place once even if the condition is false. e.g. for do-while loop


## While loops
The simplest of the bunch are while loops. While loops will run while the condition within the parenthesis is true. They should be used when you want something to happen until a certain condition takes place.

### Syntax
```
while(condition) {
   statement(s);
}
```

Here's an example:
```C
#include <stdio.h>

int main(void) {
    int my_number = 0;

    while(my_number != 10){
        ++my_number;
    }

    printf("my_number = %i", my_number);

    return 0;
}
```
While the statement within the while loop is true, the content within the brackets will be run. When the program hits the `while(my_number)`, it checks the statement within the parenthesis. If that statement is false, it won't run the while loop. Instead, it will skip over the code between the two brackets and will pick up where it left off.

If the statement is true, the code within the brackets will be run. Once the code within the brackets has run, the statement within the parenthesis will be checked again. Just like before, if the statement is true, the code will be run, if it's false, the code will be skipped.

Something that you may run into when playing with this or any other loop is the idea of an infinite loop- a loop that will run an infinite amount of times because there's nothing to stop it. Sometimes this can happen on purpose:

```C
while(1) {
    printf("This will get printed forever unless the program is stopped!");
}
```
Of course, it can also happen accidentally. Here's the same code as before, but with a subtle difference that turns it into an infinite loop:
```C
#include <stdio.h>

int main(void) {
    int my_number = 11;

    while(my_number != 10){
        ++my_number;
    }

    printf("my_number = %i", my_number);<commit_msg>Incomplete info while explaining while loop (#22265)<commit_after>---
title: Loops of all kinds
---
# Loops of all kinds in C
Loops are what you use when you have code that you want to loop, meaning that after it runs, you might want it to loop around to the beginning and run again. There are a few of these in C.


They can be categorized as:

(a) Entry controlled loops- Loops in which the condition is checked before every iteration. e.g. for loop, while loop
(b) Exit controlled loop- Loops in which the execution takes place once even if the condition is false. e.g. for do-while loop


## While loops
The simplest of the bunch are while loops. While loops will run while the condition within the parenthesis is true. They should be used when you want something to happen until a certain condition takes place.

### Syntax
```
while(condition) {
   statement(s);
}
```

Here's an example:
```C
#include <stdio.h>

int main(void) {
    int my_number = 0;

    while(my_number != 10){
        ++my_number;
    }

    printf("my_number = %i", my_number);

    return 0;
}
```
While the statement within the while loop is true, the content within the brackets will be run. When the program hits the `while(my_number!=10)`, it checks the statement within the parenthesis. If that statement is false, it won't run the while loop. Instead, it will skip over the code between the two brackets and will pick up where it left off.

If the statement is true, the code within the brackets will be run. Once the code within the brackets has run, the statement within the parenthesis will be checked again. Just like before, if the statement is true, the code will be run, if it's false, the code will be skipped.

Something that you may run into when playing with this or any other loop is the idea of an infinite loop- a loop that will run an infinite amount of times because there's nothing to stop it. Sometimes this can happen on purpose:

```C
while(1) {
    printf("This will get printed forever unless the program is stopped!");
}
```
Of course, it can also happen accidentally. Here's the same code as before, but with a subtle difference that turns it into an infinite loop:
```C
#include <stdio.h>

int main(void) {
    int my_number = 11;

    while(my_number != 10){
        ++my_number;
    }

    printf("my_number = %i", my_number);
{'fraction_non_alphanumeric': 0.071813672633168, 'fraction_numerical': 0.0053914168643519516, 'mean_word_length': 3.6706948640483383, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7295344', 'n_tokens_mistral': 1312, 'n_tokens_neox': 1223, 'n_words': 731}