Program Looping Why we need loops in our code –Make code concise for repetitive processes When to...

Post on 19-Jan-2016

247 views 0 download

Transcript of Program Looping Why we need loops in our code –Make code concise for repetitive processes When to...

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)

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;

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

Calc Factorial – Data Flow

F(N) = F(N-1) * N;

• Initial variable settings:M = 1; F = 1;

• repeated calculation:F = F * M;

• completion criteriaM equals N

• Preparation for next iteration: increment M by 1

F = F * M;

F = 1; M = 1;

M = M +1;

Flow Chart Components

• Aid in designing/documenting program logic:

* Not required.

Calc Factorial – Flow chartF(N) = F(N-1) * N;

• Initial variable settings:M = 1; F = 1;

• repeated calculation:F = F * M;

• completion criteriaM equals N

• Preparation for next iteration: increment M by 1

for loop• Syntax:

for( init_expression; loop_condition; iteration_expression )

{ program statement; }

• Flow Chart:

Condition True?

No

Initial Expression

Yes

Program statement

loop expressioniteration expression

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;

Calc Factorial – code#include <stdio.h>

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;

}

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++)

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!

loop_condition

• Relational expression stating when loop continues

Operator 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 >= 10

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?

1 2 3 4 5 6

count

Nested for Loops

• Insert a loop within a loopfor( 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 line

Print 1 star at the 1st line

Print 2 stars at the 2nd line

Print 3 stars at the 3rd line

Code

#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 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;

#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 iteration */ Fnm2 = Fnm1; Fnm1 = Fn; }

return 0; }

while loop

• Format

while (loop_condition) { program statement; }

• Flow

Condition True?

Program statement

Yes

No

for loop vs while loop

Condition true?

No

Initial Expression

Yes

Program statement

loop expressionloop expression

Condition true?

Program statement

Yes

No

for loop while loop

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;}

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;

do-while loop

• Formatdo {    program statement; } while (loop_condition);

Condition True?

program statement

YesNo

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

YesNo

while (loop_condition) { program statement; }

do {     program statement; } while (loop_condition);

Condition True?

Program statement

Yes

No