Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental Programming 310201 Fundamental Programming More on Repetition.

Similar presentations


Presentation on theme: "Fundamental Programming 310201 Fundamental Programming More on Repetition."— Presentation transcript:

1 Fundamental Programming 310201 Fundamental Programming More on Repetition

2 Fundamental Programming 310201 Repetition statements we have been using one C++ repetition statement – while repetition statements allow us to perform a task more than once – the number of times depends on the input data in this class, we introduce another repetition statement – the do-while statement

3 Fundamental Programming 310201 while – a Review while ( ) { } NbrTimesTold = 0; while (NbrTimesTold < NbrTimesToTell) { cout << “ No new taxes!“ << endl; NbrTimesTold = NbrTimesTold + 1; }

4 Fundamental Programming 310201 while – a Review common looping errors are:  loops that fail to stop (continue forever)  loops that stop one repetition too early  loops that perform one repetition too many  loops that fail to start normal while-loop structure is…

5 Fundamental Programming 310201 while – a Review while ( ) { } NbrTimesTold = 0; while (NbrTimesTold < NbrTimesToTell) { cout << “ No new taxes!“ << endl; NbrTimesTold = NbrTimesTold + 1; }

6 Fundamental Programming 310201 Activity Number = 5; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1 ; } cout << “The sum is “ << Sum << endl; what will the following code output?

7 Fundamental Programming 310201 Activity Break

8 Fundamental Programming 310201 Activity Feedback Number = 5; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1 ; } cout << “The sum is “ << Sum << endl; output depends on initial value of Sum if Sum is zero at start: “The sum is 15” must always initialise a sum to zero!

9 Fundamental Programming 310201 Activity Sum = 0 ; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1; } cout << “The sum is “ << Sum << endl; what will the following code output?

10 Fundamental Programming 310201 Activity Break

11 Fundamental Programming 310201 Activity Feedback output depends on initial value of Number must initialise variables in while condition! Sum = 0 ; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1; } cout << “The sum is “ << Sum << endl;

12 Fundamental Programming 310201 More C++ operators C++ has operators to increment or decrement loop control variables Number = Number - 1; NbrTimesTold = NbrTimesTold + 1; can be replaced by: Number-- ; NbrTimesTold++ ;

13 Fundamental Programming 310201 Activity Sum = 0 ; Number = 5; while (Number > 0) { Sum = Sum + Number ; Number++; } cout << “The sum is “ << Sum << endl; What will the following code output?

14 Fundamental Programming 310201 Activity Break

15 Fundamental Programming 310201 Activity Feedback loop will never end – an infinite loop this is a logic error! Always check loop conditions carefully. Sum = 0 ; Number = 5; while (Number > 0) { Sum = Sum + Number ; Number++; } cout << “The sum is “ << Sum << endl;

16 Fundamental Programming 310201 The while Statement while is the only repetition statement you will every need however, most programming languages include at least two other repetition statements we introduce one of these statements in this class, the other one will be covered later

17 Fundamental Programming 310201 while vs do-while the while statement tests a condition at the start of the loop the do-while statement tests a condition at the end of the loop

18 Fundamental Programming 310201 while vs do-while the while statement tests a condition at the start of the loop cout “; cin >> Char; while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) { cout << “Invalid direction!” << endl; cout “; cin >> Char; }

19 Fundamental Programming 310201 do-while loops if a task must be performed at least once, we can perform the test at the end of the loop using do-while do { cout “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; } while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’));

20 Fundamental Programming 310201 Activity what are advantages and disadvantages of this design (compared to using while)? do { cout “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; } while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)); can you suggest an improvement?

21 Fundamental Programming 310201 Activity Break

22 Fundamental Programming 310201 Activity Feedback one advantage of do-while is that there is only one copy of prompt and input lines cout “; cin >> Char; while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) { cout << “Invalid direction!” << endl; cout “; cin >> Char; }

23 Fundamental Programming 310201 Activity Feedback one advantage of do-while is that there is only one copy of prompt and input lines do { cout “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; } while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’));

24 Fundamental Programming 310201 Activity Feedback one disadvantage of do-while is that the loop condition appears twice do { cout “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; } while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’));

25 Fundamental Programming 310201 Activity Feedback one disadvantage of do-while is that the loop condition appears twice cout “; cin >> Char; while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) { cout << “Invalid direction!” << endl; cout “; cin >> Char; }

26 Fundamental Programming 310201 Activity Feedback repetition of complex loop conditions can be avoided using a Boolean variable… WaitingForDirection = true; do { cout “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; else WaitingForDirection = false; } while ( WaitingForDirection );

27 Fundamental Programming 310201 Activity is the following logic OK? Sum = 0 ; do { cout "; cin >> Number; Sum = Sum + Number; } while (Number != -9); cout << “Sum = “ << Sum; if not, fix it.

28 Fundamental Programming 310201 Activity Break

29 Fundamental Programming 310201 Activity Feedback the problem with the logic is that it will include –9 in the sum – it should be: Sum = 0 ; do { cout "; cin >> Number; if (Number != -9) Sum = Sum + Number; } while (Number != -9); cout << “Sum = “ << Sum; note: you will often see loop conditions repeated in do-while statements

30 Fundamental Programming 310201 Activity Number = 5 ; Sum = 0 ; do { Sum += Number ; Number-- ; } while (Number > 0) ; Code the following as a while loop:

31 Fundamental Programming 310201 Activity Break

32 Fundamental Programming 310201 Activity Feedback Number = 5 ; Sum = 0 ; while (Number > 0) { Sum += Number ; Number-- ; } note: loop condition remains the same... loop condition controls value of variable during the final trip through the loop – so, it should be the same

33 Fundamental Programming 310201 while vs do-while difference between while and do-while is that while allows for the possibility that the task in the loop is performed 0 times with do-while, the task in the loop is performed at least once hence, while is more flexible - that’s why we started with it


Download ppt "Fundamental Programming 310201 Fundamental Programming More on Repetition."

Similar presentations


Ads by Google