Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookThe While Loop in C

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1
some-alt