While and Do While Difference | Difference Between While and Do While
In this article While and Do While Difference we give the information about While and Do While Difference in detail with syntax and simple program with output.
While and Do While Difference:
What is while loop?
The while loop repeats a particular code as long as the condition is true. As soon as the condition becomes false the while loop ends or stops repeating that particular code which is written inside the code while block.
What is do while loop?
The do while loop also repeats a particular code as long as the condition is true. As soon as the condition becomes false then the do while loop also ends or stops repeating that particular code which is written inside the code do while loop. But there is one special thing in this that it executes the code once or repeats whether the condition is true or false.
while versus do while loop:-
while loop | do while loop |
1. while loop is entry control loop | 1. do while loop is exit control loop |
2. In the while loop the condition is checked first. | 2. The condition is checked later in the do while loop. |
3. While loop executes the code only when the condition is true. | 3. do while loop executes the code once. Performs whether the condition is true or false. |
4. Semicolon (;) is not used in while loop. | 4. Do while loop is used at the end of semicolon (;). goes. |
5. Syntax:-
while(test-condition) { Body of the loop } |
5. Syntax:-
do { Body of the loop }while(test-condition); |
7. Program: /* To show 1st 10 natural numbers*/
#include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while(i<=10) { printf(“\n %d”, i); i++; } } O/P:- 1 2 3 4 5 6 7 8 9 10
|
8. Program:
/* To show 1st 10 natural numbers*/ #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); do { printf(“\n %d”, i); i++; } while(i<=10); } O/P:- 1 2 3 4 5 6 7 8 9 10 |
Related Link:
- BCA Syllabus Shivaji University | BCA C Programming Syllabus
- Object Oriented Programming Using C++ |BCA Semester II |History of C++
- Data Structure using C | Data structures concepts in C
- Core Java Programming | BCA Part III | SEM-VI | What is Java
- Computer Fundamentals Notes | Computer Fundamentals Tutorial
- MCQ on Computer Languages with Answers | MCQ on Computer