# Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.

## Presentation on theme: "Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it."— Presentation transcript:

do-while Loops Programming

COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it works: n execute action n if condition is true then execute action again n repeat this process until condition evaluates to false. l action is either a single statement or a group of statements within braces. action true false condition

COMP102 Prog Fundamentals I: do-while Loops /Slide 3 N! int number, factorial, counter; cout > number; factorial = 1; counter = 1; do{ factorial *= counter; counter++; }while(counter <= number); cout << "The factorial of " << number << " is " << factorial << endl;

COMP102 Prog Fundamentals I: do-while Loops /Slide 4 2N2N int number, result, counter; cout > number; result = 1; counter = 1; do{ result *= 2; counter++; }while (counter <= number); cout << "Two raised to the " << number << " power is " << result << endl;

COMP102 Prog Fundamentals I: do-while Loops /Slide 5 Maximum int value;// input value int max=0;// maximum value do{ cout << "Enter a positive number “ > value; if(value > max) max = value; }while(value!=-1); cout << "The maximum value found is " << max << endl;

COMP102 Prog Fundamentals I: do-while Loops /Slide 6 Waiting for a Reply char reply; do{ // do something cout << "Continue(y/n): "; cin >> reply; }while(reply!='n');

Download ppt "Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it."

Similar presentations