Unformatted text preview:

Program loopingExample – Calc FactorialExample – Calc Factorial (cont.)Calc Factorial – Data FlowFlow Chart ComponentsCalc Factorial – Flow chartfor loopCalculate factorialCalc Factorial – codeinit_expressionloop_expressionloop_conditionloop_condition – examplesNested for LoopsExampleCodeCode – cont.Print Factorial F(1) … F(10)Calculate Fibonacci NumbersSlide 20while loopfor loop vs while loopConvert for loop to while loopConvert for to while – Exampledo-while loopwhile and do-while loopSlide 27Slide 28Program looping•Why we need loop–Make code concise for repetitive processes•When to use loop–Run a block of code repetitively–Process multiple data using same procedure•How to use loop–for–while–doExample – Calc Factorial •Goal: computing factorial N (N!) F(N) = N! = 1 * 2 * 3 * …… N•FactF(1) = 1;F(2) = 1 * 2 = F(1) * 2;F(3) = 1 * 2 * 3 = F(2) * 3; …F(N) = F(N-1) * N;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 NCalc Factorial – Data FlowF(N) = F(N-1) * N;•Initial setting:M = 1; F = 1;•Main calculationF = F * M;•Stop criteriaM = N•What else increase M by 1M = M + 1;F = F * M;F = 1; M = 1;M = M +1;Flow Chart Components•Help to document program logic* Not required.Calc Factorial – Flow chartF(N) = F(N-1) * N;•Initial setting:M = 1; F = 1;•Main calculationF = F * M;•Stop criteriaM = N•What else increase M by 1M = M + 1;for loop•Format: for( init_expression; loop_condition; loop_expression ) { program statement; }•Flow:Condition satisfied?NoInitial ExpressionYesProgram statementloop expressionloop expressionCalculate factorialfor( init_expression; loop_condition; loop_expression ) { program statement; }•Correspondingly when F(N)init_expression:loop_condition:loop_expression:program statement:M = 1;M <= N;M = M + 1;F = F * M;Calc Factorial – code#include <stdio.h>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;}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++)–Using gcc on grove.ufl.edu, you CANNOT declare variables here (different from textbook)loop_expression•Change values after the program statements in the for loop–Can be multiple valid C program statements, separated by comma (,)for(i = 0; i < 10; j++,++i )–May be omitted•put nothingfor(; i<10; )•Make sure the value for i has been changed within the for loop!loop_condition•Relational expression stating when loop continuesOperator Meaning Example== Equal to Count == 10!= Not equal to Count != 10< Less than Count < 10<= Less than or equal to Count <= 10> Greater than Count > 10>= Greater than or equal to Count >= 10loop_condition – examples1. 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?1 2 3 4 5 6count1 10 10 11 1 1Nested for Loops•Insert a loop within the loop of anotherfor( i=1; i<10; i++){for(j=1; j<10; j++) {…;}…;}Example•If we want to print following pattern*******************************************************Print n stars at the nth linePrint 1 star at the 1st linePrint 2 stars at the 2nd linePrint 3 stars at the 3rd lineCode#include <stdio.h>int main(void){ int row, col; for (row = 1; row <= 10; row++) { for (col = 1; col <= row; col++) { printf("*"); } }}printf("\n");Code – cont.#include <stdio.h>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"); }}Print Factorial F(1) … F(10)#include <stdio.h>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;}Calculate Fibonacci Numbersinitial 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;#include <stdio.h> 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; }while loop•Formatwhile (loop_condition) { program statement; } •FlowCondition satisfied?Program statementYesNofor loop vs while loopCondition satisfied?NoInitial ExpressionYesProgram statementloop expressionloop expressionCondition satisfied?Program statementYesNofor loop while loopConvert for loop to while loopwhile (loop_condition) { program statement; } for( init_expression; loop_condition; loop_expression ) { program statement; } program statement; loop_expression;init_expression;while(loop_condition){}Convert for to while – ExampleF = 1;N = 10;for(M=1; M<=N; M=M +1){ F = F * M;}init_expression;while(loop_condition){program statement; loop_expression;}F = 1;N = 10;M = 1;while(M<=N){}F = F * M;M = M +1;do-while loop•Formatdo { program statement } while (loop_condition);Condition satisfied?Program statementYesNowhile and do-while loop•In while loop, program statement may never be evaluated. While in do-while loop, it is evaluated at least onceCondition satisfied?Program statementYesNowhile (loop_condition) { program statement; } do { program statement } while (loop_condition);Condition satisfied?Program statementYesNoExamplep = 7; p = 7;c = 1; p = 1;do{ p = p + c; c++;} while( c<= 3 );Code 2c = 1; p = 1;while( c<= 3 ){ p = p + c; c++;}Code 1Examplep = 1; p = 6;c = 5; p = 1;do{ p = p + c; c++;} while( c<= 3 );Code 2c = 5; p = 1;while( c<= 3 ){ p = p + c; c++;}Code


View Full Document

UF CGS 3460 - Program looping

Download Program looping
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Program looping and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Program looping 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?