Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
While loop Syntax
Next article icon

Do While loop Syntax

Last Updated : 14 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Do while loop is a control flow statement found in many programming languages. It is similar to the while loop, but with one key difference: the condition is evaluated after the execution of the loop’s body, ensuring that the loop’s body is executed at least once.

do-while-Loop-GeeksforGeeks2

Table of Content

  • Do While loop Syntax in C/C++
  • Java Do While loop Syntax
  • Do While loop Syntax in Python
  • Do While loop Syntax in C#
  • Do While loop Syntax in JavaScript

The syntax for a do while loop varies slightly depending on the programming language you're using, but the general structure is similar across many languages.

do {
// Code block to be executed at least once
// This block will execute at least once
} while (condition);

Here's a basic outline:

  1. Initialization: Initialize any loop control variables or other necessary variables before entering the loop.
  2. Do While Loop Structure: The loop consists of two main parts: the "do" block and the "while" condition.
    • The "do" block contains the code that will be executed at least once, regardless of the condition.
    • The "while" condition specifies the condition that must be true for the loop to continue iterating. If the condition evaluates to true, the loop will execute again. If it evaluates to false, the loop will terminate.
  3. Loop Execution: The loop executes the code inside the "do" block, then evaluates the "while" condition.
    • If the condition is true, the loop repeats, executing the code block again.
    • If the condition is false, the loop terminates, and control passes to the code following the loop.

Do While loop Syntax in C/C++:

Syntax:

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

Explanation of the Syntax:

  • In C and C++, the do while loop executes a block of code once before checking the condition.
  • The code block enclosed within the curly braces {} is executed at least once, regardless of the condition.
  • After executing the code block, the loop checks the specified condition inside the parentheses ().
  • If the condition evaluates to true, the loop repeats; otherwise, it terminates.

Implementation of Do While Loop Syntax in C/C++:

C++
#include <iostream> using namespace std;  int main() {     int i = 0;     do {         cout << i << " ";         i++;     } while (i < 10);     return 0; } 
C
#include <stdio.h>  int main() {     int i = 0;     do {         printf("%d ", i);         i++;     } while (i < 10);      return 0; } 

Output
0 1 2 3 4 5 6 7 8 9 

Java Do While loop Syntax:

Syntax:

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

Explanation of the Syntax:

  • In Java, the do while loop works similarly to C and C++.
  • The code block within the curly braces {} is executed at least once.
  • After executing the code block, the loop evaluates the condition specified in the parentheses ().
  • If the condition is true, the loop repeats; otherwise, it exits.

Implementation of Do While Loop Syntax in Java:

Java
import java.io.*;  class GFG {     public static void main(String[] args)     {         int i = 0;         do {             System.out.print(i + " ");             i++;         } while (i < 10);     } } 

Output
0 1 2 3 4 5 6 7 8 9 

Do While loop Syntax in Python:

Python doesn't have a built-in do while loop, but a similar behavior can be achieved using a while loop with a condition check after the code block.

Syntax:

while True:
# Code block to be executed at least once
if not condition:
break

Explanation of the Syntax:

  • Python doesn't have a built-in do while loop construct.
  • However, a similar behavior can be achieved using a while loop with a conditional break statement.
  • The loop is initiated with a condition that is always true (while True:).
  • Inside the loop, the code block is executed at least once.
  • After executing the code block, an if statement checks the condition.
  • If the condition is not met (not condition), the loop is terminated using the break statement.

Implementation of Do While Loop Syntax in Python:

Python3
i = 0 while i < 10:     print(i, end=" ")     i += 1 

Output
0 1 2 3 4 5 6 7 8 9 

Do While loop Syntax in C#:

Syntax:

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

Explanation of the Syntax:

  • In C#, the do while loop operates similarly to C, C++, and Java.
  • The code block enclosed within the curly braces {} is executed at least once.
  • After executing the code block, the loop evaluates the condition specified in the parentheses ().
  • If the condition is true, the loop continues; otherwise, it terminates.

Implementation of Do While Loop Syntax in C#:

C#
using System;  public class GFG {      static public void Main()     {         int i = 0;         do {             Console.Write(i + " ");             i++;         } while (i < 10);     } } 

Output
0 1 2 3 4 5 6 7 8 9 

Do While loop Syntax in JavaScript:

Syntax:

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

Explanation of the Syntax:

  • In JavaScript, the do while loop functions similarly to other languages.
  • The code block within the curly braces {} is executed at least once.
  • After executing the code block, the loop evaluates the condition specified in the parentheses ().
  • If the condition is true, the loop repeats; otherwise, it exits.

Implementation of Do While Loop Syntax in JavaScript:

JavaScript
let i = 0; do {     console.log(i + " ");     i++; } while (i < 10); 

Output
0  1  2  3  4  5  6  7  8  9  

Next Article
While loop Syntax

C

code_r
Improve
Article Tags :
  • Programming

Similar Reads

  • While loop Syntax
    While loop is a fundamental control flow structure (or loop statement) in programming, enabling the execution of a block of code repeatedly as long as a specified condition remains true. Unlike the for loop, which is tailored for iterating a fixed number of times, the while loop excels in scenarios
    5 min read
  • Solidity Do While Loop
    In solidity do while loop is similar to the other programming languages firstly it won't check the condition it executes the program at least once which is written in the do block and later it checks the condition in the while block. Syntax: do{ // Block of code to be written for execution }while(co
    2 min read
  • For loop Syntax
    For loop is a control flow statement in programming that allows you to execute a block of code repeatedly based on a specified condition. It is commonly used when you know how many times you want to execute a block of code. Table of Content For loop Syntax in C/C++For loop Syntax in JavaFor loop Syn
    5 min read
  • Rust - While Loop
    Loops in Rust come into use when we need to repeatedly execute a block of statements. Loops are also useful when we need to iterate over a collection of items. In Rust, we have various kinds of loops, including loops, while loops, and for loops. The while loop is the most common loop in Rust. The lo
    3 min read
  • SAP ABAP | While Loop
    A fundamental control structure in SAP ABAP is the while loop. The while loop in SAP ABAP can be used to iterate a part of code while the given condition remains true. This construct the while loop facilitates iterative running a block of code continues to execute until the specified condition withi
    3 min read
  • Swift - While Loop
    Just like other programming languages, the working of the while loop in Swift is also the same. It is used to execute a target statement repeatedly as long as a given condition is true. And when the condition becomes false, the loop will immediately break and the line after the loop will execute. Wh
    2 min read
  • R - while loop
    While loop in R programming language is used when the exact number of iterations of a loop is not known beforehand. It executes the same code again and again until a stop condition is met. While loop checks for the condition to be true or false n+1 times rather than n times. This is because the whil
    5 min read
  • Solidity While Loop
    In Solidity, a while loop is a type of loop statement that allows you to execute a block of code repeatedly until a certain condition is met.  Syntax: while (condition) {    statement or block of code to be executed if the condition is True } The condition is an expression that is evaluated before e
    1 min read
  • PL/SQL While Loop
    Oracle PL/SQL provides various loop structures that help developers execute a block of code multiple times based on certain conditions. The main loop structures include LOOP ... END LOOP, WHILE ... END LOOP, and FOR ... END LOOP. In this article, we will explore the WHILE loop in detail, including i
    5 min read
  • Do-While loop in Programming
    Do-while loop is a control flow statement found in many programming languages. It is similar to the while loop, but with one key difference: the condition is evaluated after the execution of the loop's body, ensuring that the loop's body is executed at least once. In this article, we will learn abou
    10 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences