DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C BasicsTopic 31 of 64
←PreviousPrevNextNext→

C While Loop

Introduction to While Loop

The while loop in C is a fundamental control flow statement that allows code to be executed repeatedly based on a given Boolean condition. It's called a 'pre-test loop' because the condition is evaluated before the execution of the loop body.

The while loop continues to execute the code block as long as the condition evaluates to true (non-zero). When the condition becomes false (zero), the program control passes to the line immediately following the loop.

Syntax and Structure

The syntax of a while loop is:

while (condition) {

// code to be executed

}

The condition can be any expression that evaluates to a numeric value. If the result is non-zero (true), the loop body executes; if zero (false), the loop terminates.

How While Loop Works

1. The condition is evaluated first

2. If the condition is true (non-zero), the code inside the while loop is executed

3. After executing the code block, the condition is evaluated again

4. This process repeats until the condition becomes false (zero)

5. Once the condition is false, the program continues with the next statement after the loop

Basic Example

Example
#include <stdio.h>

int main(void) {
    int count = 1;
    while (count <= 5) {
        printf("%d ", count);
        count++;
    }
    printf("Loop ended.");
    return 0;
}
Output
1 2 3 4 5 Loop ended.

Infinite While Loop

A while loop becomes infinite if the condition never becomes false. This can happen intentionally or accidentally.

To create an intentional infinite loop: while (1) { /* code */ }

Always ensure there's a way to break out of potentially infinite loops, either through a break statement or a condition that will eventually become false.

Example
#include <stdio.h>

int main(void) {
    int count = 0;
    while (1) {
        printf("Count: %d", count);
        count++;
        if (count >= 5) {
            break;
        }
    }
    printf("Loop exited.");
    return 0;
}
Output
Count: 0 Count: 1 Count: 2 Count: 3 Count: 4 Loop exited.

Loop Control Statements

C provides two important statements for controlling loop execution:

• break: Immediately terminates the loop and transfers control to the statement following the loop

• continue: Skips the current iteration and proceeds to the next iteration of the loop

Example
#include <stdio.h>

int main(void) {
    int i = 0;
    while (i < 10) {
        i++;
        if (i == 3) {
            continue;
        }
        if (i == 8) {
            break;
        }
        printf("%d ", i);
    }
    printf("Loop ended at i = %d", i);
    return 0;
}
Output
1 2 4 5 6 7 Loop ended at i = 8

Nested While Loops

A while loop can contain another while loop, creating a nested loop structure. This is useful for working with multi-dimensional data or complex iterations.

Example
#include <stdio.h>

int main(void) {
    int i = 1, j;
    while (i <= 3) {
        printf("Outer loop iteration %d: ", i);
        j = 1;
        while (j <= 3) {
            printf("%d ", j);
            j++;
        }
        i++;
    }
    return 0;
}
Output
Outer loop iteration 1: 1 2 3 Outer loop iteration 2: 1 2 3 Outer loop iteration 3: 1 2 3

Common Pitfalls and Best Practices

1. Always initialize loop control variables before the loop

2. Ensure the loop condition will eventually become false to avoid infinite loops

3. Use meaningful variable names for loop counters

4. Be cautious with floating-point numbers in loop conditions due to precision issues

5. Consider using for loops when you know the exact number of iterations in advance

6. Use braces even for single-statement loops to improve readability and avoid errors

Test your knowledge: C While Loop
Quiz Configuration
8 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 10 min
C BasicsTopic 31 of 64
←PreviousPrevNextNext→