The do while loop is used to execute a block of statements repeatedly until the given condition becomes false. In this program, the loop starts from number 1 and prints numbers up to 10 and vice versa. The statements inside the do block are executed first, and then the condition is checked.
How It Works?
k and initialize it with 1.
do block starts execution.
k.
k by 1.
k <= 10.
#include <stdio.h> #include <conio.h> main() { int k = 1; do { printf("\n %d", k); k = k + 1; } while(k <= 10); getch(); }
#include <stdio.h> #include <conio.h> main() { int k = 10; do { printf("\n %d", k); k = k - 1; } while(k >= 1); getch(); }
do while loop to print numbers from 1 to 20.do while loop for repeating statements and printing a sequence of numbers.k = 1. Print the value of k inside the do block and increase it by 1. Continue the loop while k <= 20.#include <stdio.h> #include <conio.h> main() { int k = 1; do { printf("\n %d", k); k = k + 1; } while(k <= 20); getch(); }
do while loop to print numbers from 1 to 20. The loop executes the statements first and then checks the condition. The value of k increases by 1 after every iteration until it reaches 20.