450 likes | 1.38k Views
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.
E N D
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 • 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?
Control structures • if-else statements • if-elseif-elseif-…-else statements • switch-case statements • for-loops, while-loops and do-while loops
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
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
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
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
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” ;
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
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?
Analysis of these problems • To evaluate • Can you see why = 4 x (1 – 1/3 + 1/5 – 1/7 + ……..)?
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