Programs to learn iterative statements like while and do-while loops.
Difference between While and Do-While loop :
While | Do-While |
---|
In 'while' loop the controlling condition appears at the start of the loop. | In 'do-while' loop the controlling condition appears at the end of the loop. |
The iterations do not occur if, the condition at the first iteration, appears false. | The iteration occurs at least once even if the condition is false at the first iteration. |
Entry-controlled loop | Exit-controlled loop |
Semicolon is not used | Semicolon is used at the end of the loop |
while ( condition) { statements; } | do{ statements; } while( Condition ); |
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.
- The while loop evaluates the test expression inside the parenthesis ().
- If the test expression is true, statements inside the body of while loop are executed. Then, the test expression is evaluated again.
- The process goes on until the test expression is evaluated to false.
- If the test expression is false, the loop terminates (ends).
Syntax :
while (condition ) { //Statements to be executed } |
---|

Example :
#include<stdio.h> #include<conio.h> void main() { int i = 1; while (i <= 5) { printf("%d\n", i); ++i; } } |
---|
Output :
1
2
3
4
5
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 also Exit Control Loop.
- The body of do...while loop is executed once. Only then, the test expression is evaluated.
- If the test expression is true, the body of the loop is executed again and the test expression is evaluated.
- This process goes on until the test expression becomes false.
- If the test expression is false, the loop ends.
Syntax :
do { // statements to be executed } while (Condition); |
---|

Example :
#include<stdio.h> #include<conio.h> void main() { int i=0; do { printf("Value of i is: %d\n", i); i++; }while (i<=3); } |
---|
Output :
Value of i is: 0
Value of i is: 1
Value of i is: 2
Value of i is: 3
For more RTU/BTU II Sem Computer Programming Lab Experiments CLICK HERE