Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära The While Loop in C | While Loop
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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookThe While Loop in C

Svep för att visa menyn

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 1
some-alt