Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre The While Loop in C | While Loop
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C Loops for Beginners

bookThe While Loop in C

The while loop is a fundamental control structure in C programming that lets you repeat a block of code as long as a specified condition remains true.

The loop uses the while keyword, followed by a condition in parentheses. Before each iteration, the condition is checked. If it evaluates to true, the loop body executes; if false, the loop stops and the program continues with the next statement after the loop.

main.c

main.c

copy
12345678910111213
#include <stdio.h> int main() { int number = 1; // Loop variable initialized to 1 // The while loop will run as long as number is less than or equal to 5 while (number <= 5) { printf("%d\n", number); // Print the current value of number number++; // Increment the loop variable to avoid infinite loop } return 0; }

Let's break down how this while loop works step by step:

  1. The loop variable number is set to 1 before the loop starts;
  2. The condition number <= 5 is checked before each iteration;
  3. If the condition is true, the loop body executes and prints the current value of number;
  4. After printing, number is incremented by 1 using number++;
  5. The loop repeats this process, checking the condition each time, until number becomes 6;
  6. When number is 6, the condition number <= 5 is false, so the loop ends and the program continues after the loop.
Note
Note

An infinite loop happens when the loop's condition never becomes false. This often occurs if you forget to update the loop variable inside the loop. To avoid infinite loops, always ensure that something in the loop body will eventually make the condition false.

question mark

Which of the following is the correct syntax for a while loop in C?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 1

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you show me an example of a while loop in C?

What happens if the condition in the while loop is always true?

Can you explain the difference between a while loop and a for loop?

bookThe While Loop in C

Glissez pour afficher le menu

The while loop is a fundamental control structure in C programming that lets you repeat a block of code as long as a specified condition remains true.

The loop uses the while keyword, followed by a condition in parentheses. Before each iteration, the condition is checked. If it evaluates to true, the loop body executes; if false, the loop stops and the program continues with the next statement after the loop.

main.c

main.c

copy
12345678910111213
#include <stdio.h> int main() { int number = 1; // Loop variable initialized to 1 // The while loop will run as long as number is less than or equal to 5 while (number <= 5) { printf("%d\n", number); // Print the current value of number number++; // Increment the loop variable to avoid infinite loop } return 0; }

Let's break down how this while loop works step by step:

  1. The loop variable number is set to 1 before the loop starts;
  2. The condition number <= 5 is checked before each iteration;
  3. If the condition is true, the loop body executes and prints the current value of number;
  4. After printing, number is incremented by 1 using number++;
  5. The loop repeats this process, checking the condition each time, until number becomes 6;
  6. When number is 6, the condition number <= 5 is false, so the loop ends and the program continues after the loop.
Note
Note

An infinite loop happens when the loop's condition never becomes false. This often occurs if you forget to update the loop variable inside the loop. To avoid infinite loops, always ensure that something in the loop body will eventually make the condition false.

question mark

Which of the following is the correct syntax for a while loop in C?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 1
some-alt