Fundamental Programming Fundamental Programming More on Repetition
Fundamental Programming 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
Fundamental Programming while – a Review while ( ) { } NbrTimesTold = 0; while (NbrTimesTold < NbrTimesToTell) { cout << “ No new taxes!“ << endl; NbrTimesTold = NbrTimesTold + 1; }
Fundamental Programming 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…
Fundamental Programming while – a Review while ( ) { } NbrTimesTold = 0; while (NbrTimesTold < NbrTimesToTell) { cout << “ No new taxes!“ << endl; NbrTimesTold = NbrTimesTold + 1; }
Fundamental Programming Activity Number = 5; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1 ; } cout << “The sum is “ << Sum << endl; what will the following code output?
Fundamental Programming Activity Break
Fundamental Programming 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!
Fundamental Programming Activity Sum = 0 ; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1; } cout << “The sum is “ << Sum << endl; what will the following code output?
Fundamental Programming Activity Break
Fundamental Programming 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;
Fundamental Programming 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++ ;
Fundamental Programming Activity Sum = 0 ; Number = 5; while (Number > 0) { Sum = Sum + Number ; Number++; } cout << “The sum is “ << Sum << endl; What will the following code output?
Fundamental Programming Activity Break
Fundamental Programming 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;
Fundamental Programming 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
Fundamental Programming 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
Fundamental Programming 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; }
Fundamental Programming 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’));
Fundamental Programming 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?
Fundamental Programming Activity Break
Fundamental Programming 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; }
Fundamental Programming 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’));
Fundamental Programming 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’));
Fundamental Programming 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; }
Fundamental Programming 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 );
Fundamental Programming 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.
Fundamental Programming Activity Break
Fundamental Programming 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
Fundamental Programming Activity Number = 5 ; Sum = 0 ; do { Sum += Number ; Number-- ; } while (Number > 0) ; Code the following as a while loop:
Fundamental Programming Activity Break
Fundamental Programming 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
Fundamental Programming 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