Presentation is loading. Please wait.

Presentation is loading. Please wait.

CGS 3460 Program looping n Why we need loop lMake code concise for repetitive processes n When to use loop lRun a block of code repetitively lProcess multiple.

Similar presentations


Presentation on theme: "CGS 3460 Program looping n Why we need loop lMake code concise for repetitive processes n When to use loop lRun a block of code repetitively lProcess multiple."— Presentation transcript:

1 CGS 3460 Program looping n Why we need loop lMake code concise for repetitive processes n When to use loop lRun a block of code repetitively lProcess multiple data using same procedure n How to use loop lfor lwhile ldo

2 CGS 3460 Example – Calc Factorial n Goal: computing factorial N (N!) F(N) = N! = 1 * 2 * 3 * …… N n Fact F(1) = 1; F(2) = 1 * 2 = F(1) * 2; F(3) = 1 * 2 * 3 = F(2) * 3; … F(N) = 1 * 2 * … * N = F(N-1) * N;

3 CGS 3460 Example – Calc Factorial (cont.) F(N) = F(N-1) * N; F = F * M;F = 1;M = 1;M = M +1; When to stop? M equals to N

4 CGS 3460 Calc Factorial – Data Flow F(N) = F(N-1) * N; n Initial setting: M = 1; F = 1; n Main calculation F = F * M; n Stop criteria M = N n What else increase M by 1 M = M + 1; F = F * M; F = 1;M = 1; M = M +1;

5 CGS 3460 Flow Chart Components n Help to document program logic * Not required.

6 CGS 3460 Calc Factorial – Flow chart F(N) = F(N-1) * N; n Initial setting: M = 1; F = 1; n Main calculation F = F * M; n Stop criteria M = N n What else increase M by 1 M = M + 1;

7 CGS 3460 for loop n Format: for( init_expression; loop_condition; loop_expression ) { program statement; } n Flow: Condition satisfied? No Initial Expression Yes Program statement loop expression

8 CGS 3460 Calculate factorial for( init_expression; loop_condition; loop_expression ) { program statement; } n Correspondingly when F(N) init_expression: loop_condition: loop_expression: program statement: M = 1; M <= N; M = M + 1; F = F * M;

9 CGS 3460 Calc Factorial – code #include int main(void) { int F, N, M; F = 1; N = 10; for(M=1; M<=N; M=M +1) { F = F * M; } printf(“result is: %i \n”, F); return 0; }

10 CGS 3460 init_expression n Set initial values before the loop begins lCan be multiple valid C program statements, separated by comma (,) for( i = 0, j = 0; i < 10; ++i ) lMay be omitted if initial values have been set before Make sure to put an empty statement with only semicolon (;) for(; i<10; i++) lDeclaring variables in the loop

11 CGS 3460 loop_expression n Change values after the program statements in the for loop lCan be multiple valid C program statements, separated by comma (,) for(i = 0; i < 10; j++,++i ) lMay be omitted put nothing for(; i<10; ) Make sure the value for i has been changed within the for loop!

12 CGS 3460 loop_condition n Relational expression stating when loop continues OperatorMeaningExample ==Equal toCount == 10 !=Not equal toCount != 10 <Less thanCount < 10 <=Less than or equal toCount <= 10 >Greater thanCount > 10 >=Greater than or equal toCount >= 10

13 CGS 3460 loop_condition – example What is the value of count after the for loop? for(count = 1;count == 5; count++) { … } count 1 Loop condition satisfied? no

14 CGS 3460 loop_condition – example What is the value of count after the for loop? for(count = 1;count != 5; count++) { … } count 1 2 3 4 5 Loop condition satisfied? Yes no

15 CGS 3460 loop_condition – example What is the value of count after the for loop? for(count = 1;count < 5; count++) { … } count 1 2 3 4 5 Loop condition satisfied? Yes no

16 CGS 3460 loop_condition – example What is the value of count after the for loop? for(count = 1;count <= 5; count++) { … } count 1 2 3 4 5 6 Loop condition satisfied? Yes no

17 CGS 3460 loop_condition – example What is the value of count after the for loop? for(count = 1;count > 5; count++) { … } count 1 Loop condition satisfied? no

18 CGS 3460 loop_condition – example What is the value of count after the for loop? for(count = 1;count >= 5; count++) { … } count 1 Loop condition satisfied? no

19 CGS 3460 loop_condition – examples 1.for(count = 1; count == 5; count++) { … } 2.for(count = 1; count != 5; count++) { … } 3.for(count = 1; count <5; count++) { … } 4.for(count = 1; count <=5; count++) { … } 5.for(count = 1; count >5; count++) { … } 6.for(count = 1; count >=5; count++) { … } What is the value of count after the for loop? 123456 count 155611

20 CGS 3460 Nested for Loops n Insert a loop within the loop of another for( i=1; i<10; i++) { for(j=1; j<10; j++) { …; } …; }

21 CGS 3460 Example n If we want to print following pattern * ** *** **** ***** ****** ******* ******** ********* ********** Print n stars at the nth line Print 1 star at the 1st line Print 2 stars at the 2nd line Print 3 stars at the 3rd line

22 CGS 3460 Code #include int main(void) { int row, col; for (row = 1; row <= 5; row++) { for (col = 1; col <= row; col++) { printf("*"); } printf("\n");

23 CGS 3460 Print Factorial F(1) … F(10) #include int main(void) { int F, N, M; F = 1; //variable that holds the result N = 10; /*what to calculate the factorial of*/ printf("num \t factorial \n"); for(M=1; M<=N; M=M+1) { F = F * M; printf("%i \t %i \n", M, F); } return 0; }

24 CGS 3460 Calculate Fibonacci Numbers (0, 1, 1, 2, 3, 5, 8, 13, …) initial value: init_expression: loop_condition: loop_expression: program statement: N = 2; N <= M; N = N + 1; Fn = Fnm1 + Fnm2; Fnm1 = 1; Fnm2 = 0; What else? Fnm2 = Fnm1; Fnm1 = Fn;

25 CGS 3460 Pseudo code M = 6; n; Fn, Fnm1=1, Fnm2=0; for(n = 2; n <= M; n++) { Fn = Fnm1 + Fnm2; Fnm2 = Fnm1; Fnm1 = Fn } nn <= MFnFnm2Fnm1 ***01 2T111 3T212 4T323 5T535 6T858 7F

26 CGS 3460 #include int main(void) { int m = 0; int Fn = 0; int Fnm1 = 1; int Fnm2 = 0; /* print out the first two numbers */ printf("F(%i) = %i\n", 0, 1); printf("F(%i) = %i\n", 1, 1); /* print out the next 38 numbers */ for (n = 2; n < 40; n++) { /* calculate the next number and print it */ Fn = Fnm1 + Fnm2; printf("F(%i) = %i\n", n, Fn); /* update the old two numbers for next time through the loop */ Fnm2 = Fnm1; Fnm1 = Fn; } /* no error */ return 0; }


Download ppt "CGS 3460 Program looping n Why we need loop lMake code concise for repetitive processes n When to use loop lRun a block of code repetitively lProcess multiple."

Similar presentations


Ads by Google