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 Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
Difference between while and do-while loop in C, C++, Java
Next article icon

Difference between for and do-while loop in C, C++, Java

Last Updated : 27 Jun, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

for loop:

for loop provides a concise way of writing the loop structure. Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping. Syntax:
  for (initialization condition; testing condition;                                 increment/decrement)  {      statement(s)  }  
Flowchart: Example: C
#include <stdio.h>  int main() {      int i = 0;      for (i = 5; i < 10; i++) {         printf("GFG\n");     }      return 0; } 
C++
#include <iostream> using namespace std;  int main() {      int i = 0;      for (i = 5; i < 10; i++) {         cout << "GFG\n";     }      return 0; } 
Java
import java.io.*;  class GFG {     public static void main(String[] args)     {          int i = 0;          for (i = 5; i < 10; i++) {             System.out.println("GfG");         }     } } 
Output:
  GFG  GFG  GFG  GFG  GFG  

do-while loop:

do while loop is similar to while loop with the only difference that it checks for the condition after executing the statements, and therefore is an example of Exit Control Loop. Syntax:
  do  {      statements..  }  while (condition);  
Flowchart: do-while Example: C
#include <stdio.h>  int main() {      int i = 5;      do {         printf("GFG\n");         i++;     } while (i < 10);      return 0; } 
C++
#include <iostream> using namespace std;  int main() {      int i = 5;      do {         i++;         cout << "GFG\n";     } while (i < 10);      return 0; } 
Java
import java.io.*;  class GFG {     public static void main(String[] args)     {          int i = 5;          do {             i++;             System.out.println("GfG");         } while (i < 10);     } } 
Output:
  GFG  GFG  GFG  GFG  GFG  
Here is the difference table:
For loop Do-While loop
Statement(s) is executed once the condition is checked. Condition is checked after the statement(s) is executed.
It might be that statement(s) gets executed zero times. Statement(s) is executed at least once.
For the single statement, bracket is not compulsory. Brackets are always compulsory.
Initialization may be outside or in condition box. Initialization may be outside or within the loop.
for loop is entry controlled loop. do-while is exit controlled loop.
for ( init ; condition ; iteration ) { statement (s); } do { statement(s); } while (condition);

Next Article
Difference between while and do-while loop in C, C++, Java

P

pp_pankaj
Improve
Article Tags :
  • Difference Between
  • Programming Language
  • School Programming
  • DSA
  • loop
  • Loops & Control Structure

Similar Reads

  • Difference between for and while loop in C, C++, Java
    In C, C++, and Java, both for loop and while loop is used to repetitively execute a set of statements a specific number of times. However, there are differences in their declaration and control flow. Let's understand the basic differences between a for loop and a while loop. for Loop A for loop prov
    5 min read
  • Difference between while and do-while loop in C, C++, Java
    while loop: A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. Syntax : while (boolean condition){ loop statements...}Flowchart: Example: [GFGTABS] C++ #include <iost
    2 min read
  • Difference between while(1) and while(0) in C language
    Prerequisite: while loop in C/C++ In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The boolean condition is either true or false. while(1) It is an infinite loop which will run till a break
    3 min read
  • What is the difference between for and Foreach loop in PHP ?
    Loops can be used to iterate over collection objects in PHP. The for and foreach loop can be used to iterate over the elements. for loopThe for loop works at the end of the given condition. It is used for the implementation of variables and works in a single way. The for loop does not work in the ca
    3 min read
  • Difference between Identifiers and Variables in C
    Perquisites: Identifiers, Variables Identifiers Identifiers are used for the naming of variables, functions, and arrays. It is a string of alphanumeric characters that begins with an alphabet, or an underscore( _ ) that are used for variables, functions, arrays, structures, unions, and so on. It is
    2 min read
  • Difference between Java and C language
    Here are some of the differences between Java and C language. C is much faster than Java Java is slower than C due to overhead. C Java C was developed by Dennis M. Ritchie between 1969 and 1973.Java was developed by James Gosling in 1995.C is a Procedural Programming Language.Java is Object-Oriented
    3 min read
  • Difference Between Constants and Variables in C
    The constants and variables in C are both used to store data. So it is essential to know the difference between the variables and constants in C so that we can decide which one to use based on the situation. In this article, we will discuss the basic difference between a constant and a variable in C
    3 min read
  • Difference between C++ and Objective C
    1. C++ :C++ or CPP is a general-purpose statically typed object-oriented programming language. In 1979, a Danish computer scientist named Bjarne Stroustrup wanted to make an extension to C that would allow it to use classes. This seed has expanded since then and had become one of the most used and w
    3 min read
  • Difference between Keyword and Identifier in C
    In C, keywords and identifiers are basically the fundamental parts of the language used. Identifiers are the names that can be given to a variable, function or other entity while keywords are the reserved words that have predefined meaning in the language. The below table illustrates the primary dif
    3 min read
  • Difference between scanf() and gets() in C
    scanf()It is used to read the input(character, string, numeric data) from the standard input(keyboard).It is used to read the input until it encounters a whitespace, newline or End Of File(EOF). For example see the following code:  [GFGTABS] C // C program to see how scanf() // stops reading input a
    3 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