l04_6up - : do while loops : CSci 1113 C/C+ for Scientists...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
: CSci 1113 C/C++ for Scientists and Engineers Section 010 - evening class More Flow of Control Constructs Prof. Eric Van Wyk and Ted Kaminski Spring 2010 c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 1 / 42 do while loops : Additional types of loops I Sometimes a while loop works but is not a perfect fit. I Two other forms of loops: I a do-while loop that has the condition at the end. I a for loop that is useful for loops that count or iterate over a sequence of values such as integers 1 . . 10. c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 2 / 42 do while loops : do while loops do { statements } while ( boolean expression ) ; What is the control flow diagram of this? c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 3 / 42 do while loops : For example: int x = 0 ; do { cout << "Enter a value between 1 and 10: " ; cin >> x ; } while (x < 1 || x > 10) ; cout << "Your value is: " << x << endl ; c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 4 / 42 do while loops : I What if we want to print an error message if the input is invalid? I We may then want a while loop, but we duplicate the code for entering the data. c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 5 / 42 do while loops : Exercise 4.1 I What is the minimum number of times that a while-loop executes its loop body? I What about a do-while loop? c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 5 / 42
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
For loops : For Loops for ( init ; test ; step ) { body } I init - an expression to initialize variables I test - a boolean expression to test for exiting the loop I step - an expression to modify variables after each iteration Let’s diagram the flow of control. c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 6 / 42 For loops : Exercise 4.2 Write a program in which a for loop that computes the sum of the numbers from 1 to n , where n is entered by the user. Assume that n has already be declared and provided with a value. This solves the same problem as a previous exercise that used a while loop. c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 6 / 42 For loops : Previous examples I What previous examples would be better as a for loop? I The first while loop that printed “Hello” multiple times. I Others? Go back and see which ones you might want to rewrite. c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 7 / 42 For loops : Exercise 4.3 Compute the double value of 1 / 1 + 1 / 2 + 1 / 3 + 1 / 4 + ... + 1 / n for an input int value of n . c
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

l04_6up - : do while loops : CSci 1113 C/C+ for Scientists...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online