Download presentation
Presentation is loading. Please wait.
1
Programming Fundamentals
Lecture 13 Ms. Farah Younas
2
Agenda of Lecture do..while Loop Deitel & Deitel C++, How to program
Chapter 5 - Control Statements: Part 2 Page 174
3
do…while loop The do…while repetition statement is similar to the while statement. In the while statement, the loop-continuation condition test occurs at the beginning of the loop before the body of the loop executes. The do…while statement tests the loop-continuation condition after the loop body executes; therefore, the loop body always executes at least once.
4
do…while loop Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop. A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
5
Syntax of do…while loop
statement(s); }while( condition ); the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop execute again. This process repeats until the given condition becomes false.
6
Syntax of do…while loop
It’s not necessary to use braces in the do…while statement if there is only one statement in the body; however, most programmers include the braces to avoid confusion between the while and do…while statements For example while ( condition ) do statement while ( condition );
7
Flow Diagram of do…while loop
8
do…while loop – how it works
execute action if condition is true then execute action again repeat this process until condition evaluates to false. action is either a single statement or a group of statements within braces.
9
Flow Diagram of do…while loop
10
Example of do…while loop
int main () { // Local variable declaration: int a = 10; // do loop execution do { cout << "value of a: " << a << endl; a = a + 1; }while( a < 20 ); return 0; }
11
Output of do…while loop
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
12
do…while loop – Activity Program to add numbers until user enters zero
13
do…while loop int main() { double number, sum = 0; // loop body is executed at least once do cout<<“Enter a number: "; cin>>number; sum += number; } while(number != 0.0); cout<<"Sum = “ << sum; return 0;
14
do…while loop – calculating factorial (N!)
int main(){ int number, factorial, counter; cout << "Enter a positive integer:"; cin >> number; factorial = 1; counter = 1; do{ factorial *= counter; counter++; } while(counter <= number); cout << "The factorial of " << number<< " is " << factorial << endl; return 0;}
15
do…while loop – calculating factorial (N!)
int main(){ int number, factorial, counter; cout << "Enter a positive integer:"; cin >> number; factorial = 1; counter = 1; do{ factorial *= counter; counter++; } while(counter <= number); cout << "The factorial of " << number<< " is " << factorial << endl; return 0;}
16
do…while loop – Activity Calculating power of 2 (2^N)
17
do…while loop – calculating power of 2 (2^N)
int main(){ int number, result, counter; cout << "Enter a positive integer:"; cin >> number; result = 1; counter = 1; do{ result *= 2; counter++; }while (counter <= number); cout << "Two raised to the " << number << " power is " << result <<endl; return 0;}
18
do…while loop – calculating power of 2 (2^N)
int main(){ int number, result, counter; cout << "Enter a positive integer:"; cin >> number; result = 1; counter = 1; do{ result *= 2; counter++; }while (counter <= number); cout << "Two raised to the " << number << " power is " << result <<endl; return 0;}
19
Questions??
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.