Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

While Loop

Loops

Loops facilitate the execution of a block of code until a specified condition is met.

They offer convenience by saving time, minimizing errors, and enhancing code readability.

While Loop

The while loop iterates through a block of code as long as a specified condition remains true:

Syntax

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

In the provided example, the code inside the loop will execute repeatedly as long as a variable ( i ) remains less than 5.

Example

int i = 0;

while (i < 5) {
  printf(“%d\n”, i);
  i++;
}

 

Reminder: Ensure to increment the variable utilized in the condition (i++); otherwise, the loop will continue indefinitely.