Upload
bryce-harold-wilkinson
View
218
Download
0
Embed Size (px)
Citation preview
CGS 3460
Program loopingProgram 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 do
CGS 3460
Example – Calc Factorial Example – Calc Factorial Goal: computing factorial 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) = 1 * 2 * … * N = F(N-1) * N;
CGS 3460
Example – Calc Factorial (cont.)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
CGS 3460
Calc Factorial – Data FlowCalc Factorial – Data FlowF(N) = F(N-1) * N;
Initial setting:
M = 1; F = 1; Main calculation
F = F * M; Stop criteria
M = N What else
increase M by 1
M = M + 1;
F = F * M;
F = 1; M = 1;
M = M +1;
CGS 3460
Flow Chart ComponentsFlow Chart Components Help to document program logic
* Not required.
CGS 3460
Calc Factorial – Flow chartCalc Factorial – Flow chartF(N) = F(N-1) * N;
Initial setting:
M = 1; F = 1; Main calculation
F = F * M; Stop criteria
M = N What else
increase M by 1
M = M + 1;
CGS 3460
for loopfor loop Format: for( init_expression; loop_condition; loop_expression )
{ program statement; }
Flow:
Condition satisfied?
No
Initial Expression
Yes
Program statement
loop expressionloop expression
CGS 3460
Calculate factorialCalculate 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;
CGS 3460
Calc Factorial – codeCalc 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;
}
CGS 3460
init_expressioninit_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++) Declaring variables in the loop
CGS 3460
loop_expressionloop_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 nothing
for(; i<10; )• Make sure the value for i has been changed within the for loop!
CGS 3460
loop_conditionloop_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
CGS 3460
loop_condition – exampleloop_condition – exampleWhat is the value of count after the for loop?
for(count = 1;count == 5; count++) { … }
count1
Loop condition satisfied?no
CGS 3460
loop_condition – exampleloop_condition – exampleWhat is the value of count after the for loop?
for(count = 1;count != 5; count++) { … }
count12345
Loop condition satisfied?YesYesYesYesno
CGS 3460
loop_condition – exampleloop_condition – exampleWhat is the value of count after the for loop?
for(count = 1;count < 5; count++) { … }
count12345
Loop condition satisfied?YesYesYesYesno
CGS 3460
loop_condition – exampleloop_condition – exampleWhat is the value of count after the for loop?
for(count = 1;count <= 5; count++) { … }
count123456
Loop condition satisfied?YesYesYesYesYesno
CGS 3460
loop_condition – exampleloop_condition – exampleWhat is the value of count after the for loop?
for(count = 1;count > 5; count++) { … }
count1
Loop condition satisfied?no
CGS 3460
loop_condition – exampleloop_condition – exampleWhat is the value of count after the for loop?
for(count = 1;count >= 5; count++) { … }
count1
Loop condition satisfied?no
CGS 3460
loop_condition – examplesloop_condition – examples1. 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?
1 2 3 4 5 6
count 1 5 5 6 1 1
CGS 3460
Nested for LoopsNested for Loops Insert a loop within the loop of another
for( i=1; i<10; i++)
{
for(j=1; j<10; j++)
{
…;
}
…;
}
CGS 3460
ExampleExample 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
CGS 3460
CodeCode#include <stdio.h>
int main(void)
{
int row, col;
for (row = 1; row <= 5; row++)
{
for (col = 1; col <= row; col++)
{
printf("*");
}
}
}
printf("\n");
CGS 3460
Print Factorial F(1) … F(10)Print Factorial F(1) … F(10)#include <stdio.h>
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;}
CGS 3460
Calculate Fibonacci Numbers(0, 1, 1, 2, 3, 5, 8, 13, …)
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;
CGS 3460
Pseudo codePseudo code
M = 6;
n;
Fn, Fnm1=1, Fnm2=0;
for(n = 2; n <= M; n++)
{
Fn = Fnm1 + Fnm2;
Fnm2 = Fnm1;
Fnm1 = Fn
}
n n <= M Fn Fnm2 Fnm1
* * * 0 1
2 T 1 1 1
3 T 2 1 2
4 T 3 2 3
5 T 5 3 5
6 T 8 5 8
7 F
CGS 3460#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; }