Presentation is loading. Please wait.

Presentation is loading. Please wait.

Do-while loop Syntax do statement while (loop repetition condition)

Similar presentations


Presentation on theme: "Do-while loop Syntax do statement while (loop repetition condition)"— Presentation transcript:

1 Do-while loop Syntax do statement while (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?

2 Case study: convert a decimal integer to binary
Get an

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

4 For loop syntax Syntax of for loop for (expression 1; expression 2; expression 3) statement Example for (i=1;i<=100;i++) {sum += i;} Execution execute the expression 1 Execute expression 2. If true, then execute the statement. Then go to step 3. If false, go to step 4 Execute expression 3, then go to step 2 Leave the for loop, execute the statement following the for loop

5 Variations of for loop 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++;} for (i=1,j=100;i<=j; i++,j--) k = i+j; may have more than one variables for (i=1; i<=100;i++,i++) sum +=i;  for (i=1; i<=100;i=i+2) sum +=i; for ( ; (c=getchar())!=’\n’;) printf(“%c”,c); loop forever (infinite loop) for (;;) statement  while (1) statement for (i=1; ;i++) sum +=i;  i=1; while(1){sum+=i; i++;}

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 = *r*r; if (area>100) break; printf(“%f”, area); } What does this for loop do?

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 = *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); }

8 Example: test if a given integer is a prime
Analysis input a positive integer m Output: m is prime if m is not divisible by any integer between 2 and m-1; otherwise m is not prime Algorithm Get m k = sqrt(m), i = 2 For i from 2 to k if i%m == 0; output: m is not prime, break If i > m, output m is prime Why is the algorithm correct ?

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

10 Nested loop with different combinations
4. while ( ) { … do {…} while (); } 5. for (; ;) { … while() {…} … } 6. do { … for (; ;) {…} } while (); while ( ) { … while () {…} } do { … do {…} } while (); } while (); for ( ; ; ) { … for (; ;) {…} }

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

12 Function to Compute Factorial
Factorial of an integer n is defined to be n*(n-1)*(n-2)*….*2*1 Denoted by n!

13 Test the function with a driver
n n! #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); What happens here


Download ppt "Do-while loop Syntax do statement while (loop repetition condition)"

Similar presentations


Ads by Google