Control Structures Repetition or Iteration or Looping Part

  • Slides: 32
Download presentation
Control Structures Repetition or Iteration or Looping Part II

Control Structures Repetition or Iteration or Looping Part II

Loop Controls Counter-controlled loops repeat a specific number of times Event-controlled loops repeats until

Loop Controls Counter-controlled loops repeat a specific number of times Event-controlled loops repeats until something happens in the loop body to change the evaluation of the expression

Sentinels These are used in event-controlled loops. The loop can be executed any number

Sentinels These are used in event-controlled loops. The loop can be executed any number of times. It stops only when a specifie event occurs. *

Running Totals // count =1 while (count <=4) { cout << “n. Enter a

Running Totals // count =1 while (count <=4) { cout << “n. Enter a number: “; cin >> num; total = total + num; cout “The total is now “ << total << endl; count++; } *

Flag Example num = 0; while ( num != 999 ) { total =

Flag Example num = 0; while ( num != 999 ) { total = total + num; cout “n. The total is now “ << total; cout << “Enter a number: “; cin >> num; }

Flag Example flag = 1; while ( flag ) { total = total +

Flag Example flag = 1; while ( flag ) { total = total + num; cout “n. The total is now “ << total; cout << “Enter a number: “; cin >> num; if( num > 999) flag = 0; }

Sentinel Example cout << “n. Enter a number: “; cin >> num; } primary

Sentinel Example cout << “n. Enter a number: “; cin >> num; } primary read != <=4 999 ) while ( num count { total = total + num; cout “n. The total is now “ << total; cout << “Enter a number: “; cin >> num; count++; } ****

More Examples count = 1; total = 0; cout << "Enter a number: ";

More Examples count = 1; total = 0; cout << "Enter a number: "; cin >> num; while (count <= 4) { total = total + num; cout << "Enter a number: "; cin >> num; count++; } count--; average = total / count; cout << "n. The average is " << average << 'n';

break and continue Statements Interrupt the normal flow of control. break causes an exit

break and continue Statements Interrupt the normal flow of control. break causes an exit from innermost enclosing loop or switch statement. continue cause current iteration of a loop to stop and the next iteration to begin immediately. continue may only be used in while, for, and do loops. ***

The break Statement int j =50; 1 while (j < 80) 2 { 3

The break Statement int j =50; 1 while (j < 80) 2 { 3 j += 10; 4 if (j == 70) 5 break; 6 cout << “j is “ << j<< ‘n’; 7 } 8 cout << “We are out of the loop. n”;

The break Statement Output j is 60 We are out of the loop. Sequence

The break Statement Output j is 60 We are out of the loop. Sequence of execution: 1 2 3 4 6 7 8 1 2 3 4 5

The continue Statement int j =50; 1 while (j < 80) 2 { 3

The continue Statement int j =50; 1 while (j < 80) 2 { 3 j += 10; 4 if (j == 70) 5 continue; continue 6 cout << “j is “ << j<< ‘n’; 7 } 8 cout << “We are out of the loop. n”;

The continue Statement Output j is 60 j is 80 We are out of

The continue Statement Output j is 60 j is 80 We are out of the loop. Sequence of execution: 1 2 3 4 6 7 18 1 2 3 4 5

break and continue while ( - - - ) { statement-1; if( - -

break and continue while ( - - - ) { statement-1; if( - - - ) continue statement-2; } statement-3; while ( - - - ) { statement-1; if( - - - ) break statement-2; } statement-3; *

The do Statement Variant of the while statement Syntax do statement while (expression); next

The do Statement Variant of the while statement Syntax do statement while (expression); next statement

The do Statement statements to execute loop Test the expression 1 or True 0

The do Statement statements to execute loop Test the expression 1 or True 0 or False Exit the do

do Statments Example: do { cout << “Enter your age: “; cin >> age;

do Statments Example: do { cout << “Enter your age: “; cin >> age; if (age <=0) cout << “Invalid age. n”; else cout << "DO SOMETHINGn"; } while (age <=0);

do vs. while "; cout << "Enter your age: "; cin >> age; while

do vs. while "; cout << "Enter your age: "; cin >> age; while (age <= 0) { if (age <=0) { cout << "Invalid age. n"; cout << "Enter your age: } else cin >> age; cout << "DO SOMETHINGn";

do vs. while sum = 0; cnt = 1; do { I/O sum +=

do vs. while sum = 0; cnt = 1; do { I/O sum += cnt; cnt++; } while (cnt <=n); sum = 0; cnt = 1; I/O // priming read while (cnt <=n) { I/O sum += cnt; cnt++; }

do ¾ while ¾ for sum = 0; cnt = 1; do { sum

do ¾ while ¾ for sum = 0; cnt = 1; do { sum += cnt; cnt++; } while (cnt <=n); sum = 0; cnt = 1; while (cnt <=n) { sum += cnt; cnt++; } sum = 0; for (cnt = 1; 1 cnt <= n; cnt++) sum += cnt;

Guidelines for choosing: + If simple count-controlled, use a for. + If event-controlled and

Guidelines for choosing: + If simple count-controlled, use a for. + If event-controlled and body is executed at least once, use do. + If event-controlled and nothing is known about the first execution, use while. + When in doubt use while.

Validity Checking Validity checking or data validation is a way of notifying the user

Validity Checking Validity checking or data validation is a way of notifying the user of invalid data. You should validate every input value for which any restrictions apply. *

Common Errors Using = in place of = = Placing a ; after the

Common Errors Using = in place of = = Placing a ; after the for’s parentheses Using a , to separate items in a for expression - you need a ; Omitting the final ; in the do statement

Common Errors != versus == This changes the logic, be especially careful when used

Common Errors != versus == This changes the logic, be especially careful when used with && or || ( ) && ( ) versus ( ) || ( ) } note this

Debugging Syntax errors vs. Logic error Prevention - plan first! Valuation tables Display values

Debugging Syntax errors vs. Logic error Prevention - plan first! Valuation tables Display values C++ Debugger *

A Sample Problem Write a program that accepts a user determined number of experiments

A Sample Problem Write a program that accepts a user determined number of experiments with a user determined number of scores for each experiment. It displays the average of the scores for each experiment.

How many experiments are there? 2 How many scores are there for each? 3

How many experiments are there? 2 How many scores are there for each? 3 Enter the scores for experiment 1: Enter result of test 1 : 98 Enter result of test 1 : 56 Enter result of test 1 : 34 The average for experiment 1 is 63 How many scores are there for each? 4 Enter the scores for experiment 2: Enter result of test 2 : 21 Enter result of test 2 : 32 Enter result of test 2 : 16 Enter result of test 2 : 29 The average for experiment 2 is 25 Press any key to continue

Plan for Sample Program I/O - # experiments for I/O - # scores for

Plan for Sample Program I/O - # experiments for I/O - # scores for I/O - enter scores increment total average scores display average ***

{ int exper, test, howmany 1, howmany 2; double total, score, avg; cout <<

{ int exper, test, howmany 1, howmany 2; double total, score, avg; cout << "How many experiments are there? "; cin >> howmany 1; for (exper =1; exper<=howmany 1; exper=exper +1) { cout << "How many scores are there for each? "; cin >> howmany 2; cout << "t. Enter the scores for experiment " << exper <<": n"; total = 0; for (test = 1; test <=howmany 2; test = test +1) { cout << "Enter result of test " << test <<" : "; cin >> score; total = total + score; } avg = total/(test-1); cout << "The average for experiment " << exper << " is " << setprecision(2) << avg << "nn"; } }

Reverse_digits example Write a program to reverse the digits of a positive integer. For

Reverse_digits example Write a program to reverse the digits of a positive integer. For example is the number is 8735, the displayed number should be 5378. Hint: Use a do statement and continually strip off and display the units digit of the number (number % 10). After the units digit is displayed, dividing the number by 10 strips off the current units digit and sets up number for the next iteration. Thus, (8735 % 10) is 5 and (8735 / 10) is 873. The do statement executes until number is zero. *

Palindrome Example A palindrome is a phrase (number or text) that reads the same

Palindrome Example A palindrome is a phrase (number or text) that reads the same backwards as forwards. For example, the following are palindromes: 12321, madam i'm adam. Write a program that reads a five-digit integer and determines whether or not it is a palindrome. Ask the user if another possible palindrome is to be entered.

the end The End The End

the end The End The End