CP104 Introduction to Programming Repetition and loop Lecture 13 __ 1
Do-while loop
• Syntaxdo statementwhile (loop repetition condition)
– It first execute the statement, then do the check. If the condition is satisfied, then it execute the statement again; otherwise jump out the loop
– While loop first check the condition. If it is satisfied, then it execute the statement. Otherwise it jump out.
#include <stdio.h>main(void){ int sum = 0, i = 1;
do {
sum += i*i; i++;
} while(i<=100); printf(“The sum is %d.”, sum);}
What does this program do?
CP104 Introduction to Programming Repetition and loop Lecture 13 __ 2
Case study: convert a decimal integer to binary
• Get an
CP104 Introduction to Programming Repetition and loop Lecture 13 __ 3
for Loop Statement
Example
#include <stdio.h>main(void){ int sum = 0; for (i=1;i<=100;i++)
{ sum += i; }
printf(“The sum is %d.”, sum);}
CP104 Introduction to Programming Repetition and loop Lecture 13 __ 4
For loop syntax
• Syntax of for loopfor (expression 1; expression 2; expression 3)statement
• Example for (i=1;i<=100;i++)
{sum += i;}
• Execution1. execute the expression 1
2. Execute expression 2. If true, then execute the statement. Then go to step 3. If false, go to step 4
3. Execute expression 3, then go to step 2
4. Leave the for loop, execute the statement following the for loop
CP104 Introduction to Programming Repetition and loop Lecture 13 __ 5
Variations of for loop
1. for (i=1; i<=100;i++) sum +=i; i=1; for (; i<=100;i++) sum +=i; for (i=1; i<=100;) {sum +=i; i++;} i=1; for (; i<=100;) {sum +=i; i++;}
2. for (i=1,j=100;i<=j; i++,j--) k = i+j;may have more than one variables
3. for (i=1; i<=100;i++,i++) sum +=i; for (i=1; i<=100;i=i+2) sum +=i;
4. for ( ; (c=getchar())!=’\n’;) printf(“%c”,c);
5. loop forever (infinite loop) for (;;) statement while (1) statement
for (i=1; ;i++) sum +=i; i=1; while(1){sum+=i; i++;}
CP104 Introduction to Programming Repetition and loop Lecture 13 __ 6
break statement
• break can be used to jump out a loop (either while-loop, or for loop)
for (i=1;i<=10;r++) { area = 3.1415*r*r; if (area>100) break; printf(“%f”, area);}
What does this for loop do?
CP104 Introduction to Programming Repetition and loop Lecture 13 __ 7
continue statement
• break can be used to jump out a loop (either while-loop, or for loop)
for (i=1;i<=10;r++) { area = 3.1415*r*r; if (area>100) break; printf(“%f”, area);}
What does this for loop do?
• Continue can be used to skip the current iteration
Example: find out the integers between 100 and 200 which are not divisible by 3.
main(){ int n; for (n =100;n<=200;n++) { if (n%3 == 0) continue; printf(“%d ”, n); }
CP104 Introduction to Programming Repetition and loop Lecture 13 __ 8
Example: test if a given integer is a prime
• Analysis1. input a positive integer m2. Output: m is prime if m is not divisible by any integer
between 2 and m-1; otherwise m is not prime
• Algorithm1. Get m2. k = sqrt(m), i = 23. For i from 2 to k
if i%m == 0; output: m is not prime, break
4. If i > m, output m is prime
Why is the algorithm correct ?
CP104 Introduction to Programming Repetition and loop Lecture 13 __ 9
Prime tester
#include <stdio.h>#include <math.h>
main(){ int m,i = 0,k; scanf("%d", &m); k = sqrt((double) m);
for (i=2;i<=k;i++) { if (m%i == 0) {printf("%d is not a prime", m); break;} }
if (i>=k) printf("%d is a prime", m);}
CP104 Introduction to Programming Repetition and loop Lecture 13 __ 10
Nested loop with different combinations
1. while ( ){ … while () {…} }
2. do { … do {…} } while ();} while ();
3. for ( ; ; ){ … for (; ;) {…} }
4. while ( ){ … do {…} while (); }
5. for (; ;) { … while() {…} … }
6. do { … for (; ;) {…} } while ();
CP104 Introduction to Programming Repetition and loop Lecture 13 __ 11
Case Study: Find all Prime Numbers Between 100 and 200
Specification: print the results in rows, 10 prime each row.
#include <stdio.h>
#include <math.h>
main()
{ int m,i,k,n=0;
for (m = 101; m<=1000; m+=2)
{ if (n%10 == 0)
printf("\n");
k =(int) sqrt(m);
for (i=2;i<=k;i++)
{ if (m%i == 0) break; }
if (i>=k+1)
{
printf("%d ", m);
n = n+1;
}
}
}
CP104 Introduction to Programming Repetition and loop Lecture 13 __ 12
Function to Compute Factorial
Factorial of an integer n is defined to be n*(n-1)*(n-2)*….*2*1Denoted by n!
CP104 Introduction to Programming Repetition and loop Lecture 13 __ 13
Test the function with a driver
#include <stdio.h>#include <math.h>
int factorial(int);
int main(){ /* a test driver for factorial function */ int i=0; printf("n n!\n"); for (i = 0; i<20; i++) { printf("%d %d\n", i, factorial(i)); }
fflush(stdin); getchar(); return 0;}
int factorial(int n){ int i, product; /* accumulator for product computation */
product = 1; /* Computes the product n x (n-1) x (n-2) x ... x 2 x 1 */ for (i = n; i > 1; --i) { product = product * i; }
/* Returns function result */ return (product);}
n n!0 11 12 23 64 245 1206 7207 50408 403209 36288010 362880011 3991680012 47900160013 193205350414 127894528015 200431001616 200418918417 -28852224018 -89843302419 109641728
What happens here
Top Related