Presentation is loading. Please wait.

Presentation is loading. Please wait.

Iterations Very Useful: Ability to repeat a block of code Example:

Similar presentations


Presentation on theme: "Iterations Very Useful: Ability to repeat a block of code Example:"— Presentation transcript:

1 Iterations Very Useful: Ability to repeat a block of code Example:
summing numbers entered at the keyboard Example: Could do int sum; int x1,x2,x3,x4; printf(“Enter 4 numbers: ”); scanf(“%d%d%d%d%d”,&x1,&x2,&x3,&x4); sum = x1 + x2 + x3 + x4; OK, but what if we had 100 numbers! Prefer: sum =0 No get x Use while statement sum = sum + x are we done ? Yes 142 J -1

2 While Statement int sum=0,x,count=1; printf(“Enter 4 numbers: ”);
while(count<=4) { scanf(“%d”,&x); sum = sum + x; count = count + 1; } test expression body of the loop incrementing the counter variable Or more generally: int sum=0,x,count=1; int number_of_entries; printf(“How many numbers? ”); scanf(“%d”,&number_of_entries); while (count<=number_of_entries) { printf(“Enter entry %d: ”,count); scanf(“%d”,&x); sum = sum + x; count = count + 1; } 142 J -2

3 While Syntax while (condition) { statement1; statement2; ...
} the loop body is executed as long as the condition is true Flow Chart True condition ? statement1; statement2; False Note: if the condition is FALSE, the body of the loop is NOT executed (not even once) 142 J -3

4 Examples Computing factorial n: n! = n(n-1)(n-2)(n-3)...1
e.g. 9! = 9*8*7*6*5*4*3*2*1=362880 How? i>1 get n fact_n = 1 Yes No i=n fact_n = fact_n*i i = i - 1 142 J -4

5 n! in C int n; int i; int fact_n;
printf(“Enter a positive integer: ”); scanf(“%d”,&n); i=n; fact_n=1; while(i>1) { fact_n = fact_n * i; i = i - 1; } 142 J - 5

6 Another example Goal: compute the time needed to double the
amount of money on a 5% interest account With a while loop get the amount goal = 2*amount year = 0 Yes 5% increase year = year + 1 money<goal No 142 J -6

7 In C double money; double goal; int year=0;
printf(“Enter the amount of money: ”); scanf(“%lf”,&money); goal = 2.*money; while(money<goal) { money = money *money; year = year + 1; } printf(“My money will double in %d years”, year); 142 J -7

8 Sentinel controlled loop
Motivation: e.g, a program inputs grades from a class with a loop (e.g.a scanf statement within a while loop). How to end the loop when we don’t know the number of students in the class? input a value that can’t be a grade (e.g. -1) and have the condition tests for it scanf(“%lf”,&grade); while(grade != -1) { ... } sentinel value Get data Process data Get new data No sentinel value? Yes 142 J -8

9 sentinel controlled loop
Example of a sentinel controlled loop Find the average of a list of positive numbers: printf (“Enter a positive number (or -1 to end): ”); scanf(“%d”,&n); sum = 0; count = 0; while(n!=-1) { sum = sum + n; count = count + 1; (Enter a positive number (or -1 to end): ”); } if (count!=0) printf(“average=%f”, (double)sum/(double)count); sentinel 142 I -9

10 A command interpreter Read in commands and execute them
input command (single character) if a, execute command a if b, execute command b if q, exit (quit) How? have a variable done set to FALSE while not done read the user’s input select the proper execution if quit, set done to TRUE 142 J -10

11 Command interpreter in C
#define FALSE 0 #define TRUE 1 int main(void){ char command; int done = FALSE; while(!done){ printf(“Input command: ”); scanf(“%c”,&command); switch(command){ case ‘a’: case ‘A’: command_a(); break; case ‘b’: case ‘B’: command_b(); case ‘q’: case ‘Q’: done = TRUE; default: printf(“What?\n”); } /*end of switch */ }/*end of while */ return 0; } 142 J -11


Download ppt "Iterations Very Useful: Ability to repeat a block of code Example:"

Similar presentations


Ads by Google