1 / 6

Programming

Programming. do-while Loops. The do-while Statement. Syntax do action while ( condition ) How it works: execute action if condition is true then execute action again repeat this process until condition evaluates to false.

adie
Télécharger la présentation

Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming do-while Loops

  2. The do-while Statement • Syntax doaction while(condition) • 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. action condition true false

  3. N! 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;

  4. 2N 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;

  5. Maximum int value; // input value int max=0; // maximum value do{ cout << "Enter a positive number “ << "(-1 to stop):"; cin >> value; if(value > max) max = value; }while(value!=-1); cout << "The maximum value found is " << max << endl;

  6. Waiting for a Reply char reply; do{ // do something cout << "Continue(y/n): "; cin >> reply; }while(reply!='n');

More Related