Programs to learn iterative statements like while and do-while loops.
Difference between While and Do-While loop :
|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) |
} 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).
while (condition )
//Statements to be executed
int i = 1;
while (i <= 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.
// statements to be executed
printf("Value of i is: %d\n", i);
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