General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Control structures
  3. Iteration (Loops)

Do-While loops

Do-While Loop Statement in Computer Programming

The do-while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given Boolean condition at the end of each iteration. Unlike the while loop, which tests the loop condition before the block of code runs, the do-while loop checks its condition after the code has executed. This ensures that the block of code inside the loop executes at least one time.

Syntax:

do {
    // Code block to be executed
} while (condition);

Example in C#:

using System;

class Program {
    static void Main() {
        int counter = 0;
        do {
            Console.WriteLine(counter);
            counter++;
        } while (counter < 5);
    }
}

Note: Python does not have a built-in do-while loop construct. Instead, a similar effect can be achieved using a while loop. Here is an equivalent approach in Python:

Example in Python:

counter = 0
while True:
    print(counter)
    counter += 1
    if counter >= 5:
        break
PreviousWhile loopsNextFor-Each loops

Last updated 1 year ago

Was this helpful?