1 / 14

The do - while Loop

The do - while Loop. Venkatesh Ramamoorthy 02-March-2005. Basics. do { statement-1 ; statement-2 ; …….. statement-k ; } while (condition) ;. Diagrammatic view. Initialize. Statement-1. Statement-2. Statement-k. Is condition TRUE?. Y. N. Questions.

roger
Télécharger la présentation

The do - while Loop

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. The do-while Loop Venkatesh Ramamoorthy 02-March-2005

  2. Basics do { statement-1 ; statement-2 ; …….. statement-k ; } while (condition) ;

  3. Diagrammatic view Initialize Statement-1 Statement-2 Statement-k Is condition TRUE? Y N

  4. Questions • What does this structure do? • Executes repeatedly • When does the condition get examined? • First, or last? • Therefore, will the body of the loop always execute for the first time? • How does this differ from the for and while loops?

  5. Control structures • if-else statements • if-elseif-elseif-…-else statements • switch-case statements • for-loops, while-loops and do-while loops

  6. What structure would this diagram represent? Initialize Condition-1 TRUE? N Y Condition-2 TRUE? N Y Condition-3 TRUE? N Y Stmt-1 Stmt-2 Stmt-3 Stmt-4 Next-stmt

  7. Exercise • Write a C++ program that computes the sum of an unspecified number of integers • Keep on reading in integers, and accumulating these into the total • Stop accumulating into the total only if you enter the integer –9999

  8. Counter-controlled and Sentinel-controlled loops • Counter-controlled loops • You know the number of data items in the beginning • You use an index to serve to count the number of data items, as the current data item is being processed • Sentinel-controlled loops • You do not know the number of data items in the beginning • You therefore signal an “end-of-data” by sending as input a special data item, that will cause loop termination

  9. Control Structures • In the figures below, what kind of loops are best used to determine the following executions? Initialize Initialize Initialize Is condition TRUE? Is condition TRUE? Statement-1 N N Statement-2 Y Y Statement-2 Statement-2 Statement-k Statement-k Statement-k Is condition TRUE? Y Statement-1 Statement-1 N

  10. A one-line version of if • The conditional operator (the ? Operator) • y = (x >= 0) ? 25 : 50 ; • cout << (x % 2 != 0) ? “x is an odd number” : “x is an even number” ;

  11. Unary Binary and Ternary Operators • Unary operators • Operates on only one operand • Example : !(x == y) • Binary operators • Take two operands at a time • Example: y = a + b ; • Ternary operators • Takes three operands • Example: The ? operator

  12. More idioms • How do you compute a number correct to a specified number of decimal places? • Example: How do you evaluate  correct to 4 decimal places? • How do you generalize this to evaluate  correct to d decimal places, where d is a user input? • How do you iteratively compute the solution to a numerical problem? • How do you compute the square root, or even the cube root of a number correct to d decimal places?

  13. Analysis of these problems • To evaluate  • Can you see why  = 4 x (1 – 1/3 + 1/5 – 1/7 + ……..)?

  14. More on Control Structures • A one-line version of the if-else statement • The conditional operator (?) • Abnormal termination / repetition of loops • The break statement • The continue statement • A more symmetric form of if-elseif-elseif-…-else • The switch-case statement along with the break statement

More Related