C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • 1. While Loop
  • 2. Do-While Loop
  • Practical Tips

Was this helpful?

  1. Control Flow

While and Do-While Loops

In addition to the for loop, C provides the while and do-while loops for iterating through code blocks based on certain conditions. Let's explore the syntax and usage of both the while and do-while loops.

1. While Loop

#include <stdio.h>

int main() {
    int count = 1;

    // Example of a while loop
    while (count <= 5) {
        printf("Iteration %d\n", count);
        count++;
    }

    return 0;
}

Explanation of the Program:

  1. The while loop continues to execute the code block as long as the specified condition (count <= 5) is true.

  2. In each iteration, the body of the loop is executed, and the loop variable (count) is updated (count++).

  3. The loop prints "Iteration 1" to "Iteration 5" in this example.

2. Do-While Loop

#include <stdio.h>

int main() {
    int x = 5;

    // Example of a do-while loop
    do {
        printf("Value of x: %d\n", x);
        x--;
    } while (x > 0);

    return 0;
}

Explanation of the Program:

  1. The do-while loop is similar to the while loop, but it ensures that the code block is executed at least once before checking the condition.

  2. In this example, the loop prints the value of x and decrements it until x becomes 0.

  3. Even if the initial condition is false, the code block executes at least once.

Practical Tips

  • Use the while loop when the number of iterations is not known in advance.

  • The do-while loop is suitable when you want to guarantee that the code block runs at least once.

Understanding and utilizing while and do-while loops provide flexibility in handling different looping scenarios in C programming. If you have specific questions or if there are additional topics you'd like to explore, feel free to ask. Happy coding!

PreviousFor LoopNextNested Loops and Loop Control

Was this helpful?