program looping why we need loops in our code –make code concise for repetitive processes when to...

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

Upload: pamela-goodwin

Post on 19-Jan-2016

259 views

Category:

Documents


0 download

TRANSCRIPT

Page 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

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)

Page 2: 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

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;

Page 3: 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

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

Page 4: 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

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;

Page 5: 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

Flow Chart Components

• Aid in designing/documenting program logic:

* Not required.

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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;

Page 9: 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

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;

}

Page 10: 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

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

Page 11: 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

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!

Page 12: 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

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

Page 13: 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

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

Page 14: 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

Nested for Loops

• Insert a loop within a loopfor( i=1; i<10; i++){for(j=1; j<10; j++)

{…;

}…;

}

Page 15: 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

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

Page 16: 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

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");

Page 17: 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

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

Page 18: 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

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

Page 19: 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

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;

Page 20: 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

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

Page 21: 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

while loop

• Format

while (loop_condition) { program statement; }

• Flow

Condition True?

Program statement

Yes

No

Page 22: 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

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

Page 23: 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

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

Page 24: 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

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;

Page 25: 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

do-while loop

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

Condition True?

program statement

YesNo

Page 26: 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

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