330 likes | 353 Views
Delve into essential programming concepts like repetition statements in C++. Learn to avoid common looping errors and practice with examples to understand the nuances of while and do-while loops. Enhance your coding skills and prepare for efficient algorithm development.
E N D
Fundamental Programming More on Repetition
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
while – a Review • while ( <condition> ) • { • < while statements > • } • NbrTimesTold = 0; • while (NbrTimesTold < NbrTimesToTell) • { cout << “No new taxes!“ << endl; NbrTimesTold = NbrTimesTold + 1; }
while – a Review commonlooping 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…
while – a Review • < initialise variables in while-condition > • while ( <condition> ) • { • < while statements > • < updatevariables in while-condition > • } • NbrTimesTold = 0; • while (NbrTimesTold < NbrTimesToTell) • { cout << “No new taxes!“ << endl; NbrTimesTold = NbrTimesTold + 1; }
Activity what will the following code output? Number = 5; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1 ; } cout <<“The sum is “<<Sum <<endl;
Activity Feedback output depends on initial value of Sum if Sum is zero at start: “The sum is 15” must always initialise a sum to zero! Number = 5; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1 ; } cout <<“The sum is “<<Sum <<endl;
Activity what will the following code output? Sum = 0 ; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1; } cout <<“The sum is “<<Sum <<endl;
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;
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++ ;
Activity What will the following code output? Sum = 0 ; Number = 5; while (Number > 0) { Sum = Sum + Number ; Number++; } cout <<“The sum is “<<Sum <<endl;
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;
The whileStatement 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
while vs do-while the while statement tests a condition atthe start of the loop the do-while statement tests a condition atthe endof the loop
while vs do-while the while statement tests a condition atthe startof the loop cout << “Select a direction – N,S,E or W ==> “; cin >> Char; while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) { cout << “Invalid direction!” << endl; cout << “Select a direction – N,S,E or W ==> “; cin >> Char; }
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 << “Select a direction – N,S,E or W ==> “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; }while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’));
Activity what are advantages and disadvantages of this design (compared to using while)? do { cout << “Select a direction – N,S,E or W ==> “; 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?
Activity Feedback one advantage of do-while is that there is only one copy of prompt and input lines cout << “Select a direction – N,S,E or W ==> “; cin >> Char; while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) { cout << “Invalid direction!” << endl; cout << “Select a direction – N,S,E or W ==> “; cin >> Char; }
Activity Feedback one advantage of do-while is that there is only one copy of prompt and input lines do { cout << “Select a direction – N,S,E or W ==> “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; }while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’));
Activity Feedback one disadvantage of do-while is that the loop condition appears twice do { cout << “Select a direction – N,S,E or W ==> “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; }while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’));
Activity Feedback one disadvantage of do-while is that the loop condition appears twice cout << “Select a direction – N,S,E or W ==> “; cin >> Char; while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) { cout << “Invalid direction!” << endl; cout << “Select a direction – N,S,E or W ==> “; cin >> Char; }
Activity Feedback repetition of complex loop conditions can be avoided using a Boolean variable… WaitingForDirection = true; do { cout << “Select a direction – N,S,E or W ==> “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; else WaitingForDirection = false; }while ( WaitingForDirection );
Activity • is the following logic OK? • Sum = 0 ; • do • { • cout << “Enter a number (-9 to quit) ==> "; • cin >> Number; • Sum = Sum + Number; • } while (Number != -9); • cout << “Sum = “ << Sum; • if not, fix it.
Activity Feedback • the problem with the logic is that it will include –9 in the sum – it should be: • Sum = 0 ; • do • { • cout << “Enter a number (-9 to quit) ==> "; • 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
Activity Code the following as a while loop: Number = 5 ; Sum = 0 ; do { Sum += Number ; Number-- ; }while (Number > 0) ;
Activity Feedback note: loop condition remains the same... loop condition controls value of variable during the finaltrip through the loop – so, it should be the same Number = 5 ; Sum = 0 ; while (Number > 0) { Sum += Number ; Number-- ; }
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