Download presentation
Presentation is loading. Please wait.
Published byPamela Goodwin Modified over 9 years ago
1
Program Looping Why we need loops in our code –Make code concise for repetitive processes When to use loops –Run a block of code repetitively –Process multiple data sets with same code C loop facilities –for –while –do (you are not responsible for “do” stmts)
2
Example – Calculate Factorial Goal: computing factorial of N (N!) F(N) = N! = 1 * 2 * 3 * …… N Fact F(1) = 1; F(2) = 1 * 2 = F(1) * 2; F(3) = 1 * 2 * 3 = F(2) * 3; … F(N) = F(N-1) * N;
3
Example – Calc Factorial (cont.) F(N) = F(N-1) * N; F = F * M;F = 1;M = 1;M = M +1; When to stop? When M equals N
4
Calc Factorial – Data Flow F(N) = F(N-1) * N; Initial variable settings: M = 1; F = 1; repeated calculation: F = F * M; completion criteria M equals N Preparation for next iteration: increment M by 1 F = F * M; F = 1;M = 1; M = M +1;
5
Flow Chart Components Aid in designing/documenting program logic: * Not required.
6
Calc Factorial – Flow chart F(N) = F(N-1) * N; Initial variable settings: M = 1; F = 1; repeated calculation: F = F * M; completion criteria M equals N Preparation for next iteration: increment M by 1
7
for loop Syntax: for( init_expression; loop_condition; iteration_expression ) { program statement; } Flow Chart: Condition True? No Initial Expression Yes Program statement loop expression iteration expression
8
Calculate factorial for( init_expression; loop_condition; iteration_expression ) { program statement; } For factorial(N) init_expression: loop_condition: loop_expression: program statement: M = 1; M <= N; M = M + 1; F = F * M;
9
Calc Factorial – code #include int main() { 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
init_expression Set initial values before the loop begins –Can be multiple valid C program statements, separated by comma (,) for( i = 0, j = 0; i < 10; ++i ) –May be omitted if initial values have been set before Make sure to put an empty statement with only semicolon (;) for(; i<10; i++)
11
iteration_expression Change values after the program statements execute in the body of the for loop –Can be multiple valid C program statements, separated by comma (,) for(i = 0; i < 10; j++,++i ) –May be omitted put nothing for(; i<10; ) Make sure the value for i has been changed within the for loop!
12
loop_condition 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
loop_condition – examples 1.for(count = 1;count == 10; count++) { … } 2.for(count = 1; count != 10; count++) { … } 3.for(count = 1; count <10; count++) { … } 4.for(count = 1; count <=10; count++) { … } 5.for(count = 1; count >10; count++) { … } 6.for(count = 1; count >=10; count++) { … } What is the value of count after the for loop? 123456 count
14
Nested for Loops Insert a loop within a loop for( i=1; i<10; i++) { for(j=1; j<10; j++) { …; } …; }
15
Example 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
16
Code #include int main(void) { int row, col; for (row = 1; row <= 10; row++) { for (col = 1; col <= row; col++) { printf("*"); } printf("\n");
17
Code – cont. #include int main(void) { int row, col, max_rows; printf("How many rows do you want to print out? \n"); scanf("%i", &max_rows); for (row = 1; row <= max_rows; row++) { for (col = 1; col <= row; col++) { printf("*"); } printf("\n"); }
18
Print Factorial F(1) … F(10) #include int main(void) { int F, N, M; F = 1; N = 10; 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; }
19
Calculate Fibonacci Numbers 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;
20
#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 iteration */ Fnm2 = Fnm1; Fnm1 = Fn; } return 0; }
21
while loop Format while (loop_condition) { program statement; } Flow Condition True? Program statement Yes No
22
for loop vs while loop Condition true? No Initial Expression Yes Program statement loop expression Condition true? Program statement Yes No for loopwhile loop
23
for loop versus while loop while (loop_condition) { program statement; } for(init_expression;loop_condition;loop_expression) { program statement; } init_expression; while(loop_condition){ program statement; loop_expression; }
24
Convert for to while – Example F = 1; N = 10; for(M=1; M<=N; M=M+1) { F = F * M; } init_expression; while(loop_condition) { program statements; loop_expression; } F = 1; N = 10; M = 1; while(M<=N) { } F = F * M; M = M +1;
25
do-while loop Format do { program statement; } while (loop_condition); Condition True? program statement Yes No
26
while and do-while loop In while loop, program statement may never be executed. With a do-while loop, it is executed at least once Condition True? Program statement Yes No while (loop_condition) { program statement; } do { program statement; } while (loop_condition); Condition True? Program statement Yes No
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.