• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Difference Between do, do-while and for loop | Syntax and Coding Example

Tarshal Nimawat/15171/0
C / C++Code

Table of Contents

  • What is the loop in C?
  • Different Looping Statements in C
    • while
    • do-while
    • for loop
  • Difference Between while, do-while and for loop in C/C++.
  • Practice solving while, do-while, for loop coding questions

What is LOOPING STRUCTURE in C/C++?

Loops is a technique to repeatedly perform the same set of instructions until a specific condition is satisfied. It makes the code less complex and more efficient.

What are the Different Types of Loop Statement in C/C++?

C language has three looping statements, namely…

  • while
  • do while
  • for loop

1. while loop in C

In the while statement, firs,t a counter is initialized which undergoes a test condition. If the condition evaluates to true, then the body of the loop gets executed. Hence, while loop runs until the condition becomes false.

Syntax:

Its syntax is-

initialize counter;
while (test condition)
{
    //Body of the loop
}

Example:

Write a C Prgram to print all the first 10 multiples of three using while Loop.

#include<stdio.h>
int main()
{
int i,m;
i=1;
printf("The first ten multiples of 3 are\n");
while(i<=10)
{
m=3*i;
printf("%d\n",m)
i++;
}
return 0;
}

Output:

The first ten multiples of 3 are
3 
6 
9 
12 
15 
18 
21 
24 
27 
30

2. do-while loop in C

In the do-while statement, first the instructions in the do block are executed and then the condition in the while block is tested. So, the do while statement will at least execute the code once, even if the condition is false at the very first time.

Syntax:

Its syntax is-

do
{
    //body of the loop
} while (test condition);

Example:

Write a C program to print the cube of the number first five natural numbers using while loop.

#include<stdio.h>
int main()
{
printf("The cube of first 10 number are\n");
int i;

do
{
printf("%d\n",i*i*i);

i++;
} while(i<=5);
return 0;
}

Output:

The cube of first 10 number are
1
8 
27 
64 
125

3. for loop in C

The for loop is the most used repetition statement in almost all programming languages. The reason for its popularity is that it does three jobs at once.

It initializes the counter, tests the condition and increments/decrements the counter at the same time.

This is also the key difference which makes for loop more acceptable over the while and do while loop for most programmers.

Syntax:

Its syntax is-

for (initialize counter; test condition; increment/decrement counter;)
{
    //body of for loop
}

Example:

Write a C program to print the square of the first 10 natural number using for loop.

#include<stdio.h>
int main()
{
int i;
printf("The square of first 10 natural numbers are\n");
for(i=10; i>0; i--)
{
printf("%d\n",i*i);
}
return 0;
}

Output:

The square of first 10 natural numbers are
1 
4
9 
16
25 
36 
49 
64 
81 
100

What is the difference between while, do-while and for loop in C/C++?

  • The while and do-while loop are almost similar in C. Except, for the fact that while tests the condition first and then executes, whereas do-while loop first executes then tests the condition.
  • Block of code inside the while statement may or may not be executed depending on the condition. The block of the code inside do-while always executes the first time.
  • The for loop is very much easy as compared to do and do-while in C/C++ programming. The for loop initialize, test the condition and increment/decreament the counter at the same time. If you look at the most of the code, you will see, for loop has been used over other loops in programming.

What’s next?

Practice solving while, do-while, for loop coding questions

Hope this tutorial has helped you to understand the main difference between while, do-while and for loop in C/C++ along with syntax and C programming example.

Now practise solving coding questions using different loops.

  1. Write a program to display the list of first 20 odd numbers using while, do-while and for loop.
  2. Print the Pyramid of stars using nested for loops.

You can find more programming questions here for practice.

Happy Programming!

cpp
Tarshal Nimawat
Tarshal is a tech-head CS undergrad, who is always on the lookout for the sharpest cutting edge techs in the business, be it Blockchain, hashgraphs or AI/ML. With a knack for business development, negotiation and tech, she is often found educating those around her.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard