Download presentation
Presentation is loading. Please wait.
Published byLynette Holmes Modified over 9 years ago
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? N Y
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? Condition-2 TRUE? Condition-3 TRUE? N N N Stmt-1Stmt-2Stmt-3Stmt-4 Y Y Y 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 Statement-1 Statement-2 Statement-k Is condition TRUE? N Y Initialize Statement-2 Statement-k Is condition TRUE? N Y Statement-1 Initialize Statement-2 Statement-k Is condition TRUE? N Y Statement-1
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.