
Understanding Different Loop Statements in Programming
Dive into the concepts of do-while and do statements in loops, see examples of finding the sum of natural numbers and reversing digits of a number, and understand the criteria for choosing between entry-controlled and exit-controlled loops in programming. Learn how to rewrite while loops as for loops and vice versa for optimized coding practices.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
The do while statement do { body of the loop } while(test condition); Exit controlled loop. At the end of the loop, the test condition is evaluated. After do statement, program executes the body of the Loop. Then, the condition is tested, if it is true, body of the loop is executed once again & this process continues as long as the condition is true. Body of the loop is executed at least once. do-while loop can be nested. 1 6/3/2025
The do statement do statement while ( loop_expression ); program Loop with the test at the end ! Body is executed at least once ! 3 1 Statement(s) yes loop_expression 2 No 4 Next statement 6/3/2025 2
Example: Finding sum of natural numbers up to 100 #include <stdio.h> int main() { int n; int sum=0; #include <stdio.h> int main() { int n; int sum =0; n=1; while (n<=100) { sum=sum+n; n = n +1; } printf( %d ,sum); return 0; } n=1; do { sum = sum + counter; counter = counter +1;}} } while (counter < 100); printf( %d ,sum); return 0; } do do { { sum = sum + n; sum = sum + n; n = n +1; n = n +1; } while (n < =100); } while (n < =100); 6/3/2025 3
Program to reverse the digits of a number #include <stdio.h> int main() { int number, rev=0, right_digit; #include <stdio.h> int main() { int number, rev=0, right_digit; printf( Enter your number.\n ); scanf( %d ,&number); printf( Enter your number.\n ); scanf( %d ,&number); do { while ( number != 0 ) { right_digit = number % 10; rev=rev*10 + right_digit; number = number / 10; } printf( The reversed number is %d , rev); return 0; right_digit = number % 10; rev=rev*10 + right_digit; number = number / 10; } while ( number != 0 ); printf( The reversed number is %d ,rev); return 0; } } 6/3/2025 4
Which loop to choose ? Criteria: category of looping Entry-controlled loop -> for, while Exit-controlledloop -> do Criteria: Number of repetitions: Indefinite loops ->while Counting loops -> for You can actually rewrite any while as a for and vice versa ! 6/3/2025 5