1 / 86

Incremental operators

Incremental operators. Used as a short-hand. i++ or ++i == i = i + 1 i-- or --i == i = i – 1 i += a == i = i + a i -= a == i = i - a i *= a == i = i * a i /= a == i = i / a. Loops. Used to repeat the same instruction(s) over and over again. Block of code

hsaldivar
Download Presentation

Incremental operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Incremental operators • Used as a short-hand i++ or ++i== i = i + 1 i-- or --i== i = i – 1 i += a==i = i + a i -= a== i = i - a i *= a==i = i * a i /= a== i = i / a

  2. Loops • Used to repeat the same instruction(s) over and over again. Block of code Some changing state Loop while some condition holds

  3. Loops • C provides some flexible ways of deciding how many times to loop, or when to exit a loop. • for,while,do-while loops.

  4. while loops while(condition) { statement(s); } The statements are executed as long as condition is true When the condition is no longer true, the loop is stopped.

  5. Example - factorial #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); i=1; /* this is the counter */ while (i<=n) { fact = fact*i; i++; /* equivalent to i = i+1 */ } printf("the factorial is %d\n", fact); return 0; }

  6. Example – fibonacci series fibonacci.c

  7. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 0 1 --- 5 Screen 0

  8. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 0 1 --- 5 Screen 0

  9. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 0 1 --- 5 Screen 0 1

  10. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 0 1 1 5 Screen 0 1

  11. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 1 5 Screen 0 1

  12. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 1 5 Screen 0 1

  13. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 1 5 Screen 0 1

  14. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 1 5 Screen 0 1 1

  15. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 2 5 Screen 0 1 1

  16. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 2 5 Screen 0 1 1

  17. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 2 2 5 Screen 0 1 1

  18. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 2 2 5 Screen 0 1 1

  19. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 2 2 5 Screen 0 1 1 2

  20. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 2 3 5 Screen 0 1 1 2

  21. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 2 3 5 Screen 0 1 1 2

  22. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 3 3 5 Screen 0 1 1 2

  23. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 3 3 5 Screen 0 1 1 2

  24. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 3 3 5 Screen 0 1 1 2 3

  25. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 3 5 5 Screen 0 1 1 23

  26. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 3 5 5 Screen 0 1 1 23

  27. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 5 5 5 Screen 0 1 1 23

  28. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 5 5 5 Screen 0 1 1 23

  29. Fibonacci – step by step fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 5 5 5 Screen 0 1 1 23

  30. getchar • getchar() gets a single character from the user. • Requires including stdio.h • Returns a non-positive number on failure. • Similar to scanf. char c; c = getchar(); char c; scanf(“%c”, &c); ====

  31. Putchar • putchar(char) prints out the character inside the brackets. • Requires including stdio.h • Similar to printf. char c; putchar(c); char c; printf(“%c”, c); ====

  32. Example – lower-case to upper case. low2up.c

  33. Low2up – step by step Buffer #include <stdio.h> int main() { char c; char upper_c; printf(“Enter a string: "); c = getchar(); upper_c c ‘#’ ‘@’ Screen

  34. Low2up – step by step Buffer #include <stdio.h> int main() { char c; char upper_c; printf(“Enter a string: "); c = getchar(); yeS\n upper_c c ‘#’ ‘@’ Screen

  35. Low2up – step by step Buffer #include <stdio.h> int main() { char c; char upper_c; printf (“Enter a string: "); c = getchar(); eS\n upper_c c ‘y’ ‘@’ Screen

  36. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); eS\n upper_c c ‘y’ ‘@’ Screen

  37. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); eS\n upper_c c ‘y’ ‘@’ Screen

  38. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); eS\n upper_c c ‘y’ ‘Y’ Screen

  39. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); eS\n upper_c c ‘y’ ‘Y’ Screen Y

  40. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); S\n upper_c c ‘e’ ‘Y’ Screen Y

  41. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); S\n upper_c c ‘e’ ‘Y’ Screen Y

  42. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); S\n upper_c c ‘e’ ‘Y’ Screen Y

  43. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); S\n upper_c c ‘e’ ‘E’ Screen Y

  44. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); S\n upper_c c ‘e’ ‘E’ Screen YE

  45. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); \n upper_c c ‘S’ ‘E’ Screen YE

  46. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); \n upper_c c ‘S’ ‘E’ Screen YE

  47. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); \n upper_c c ‘S’ ‘E’ Screen YE

  48. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); \n upper_c c ‘S’ ‘S’ Screen YE

  49. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); \n upper_c c ‘S’ ‘S’ Screen YES

  50. Low2up – step by step Buffer while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ c = getchar(); } putchar('\n'); upper_c c ‘\n’ ‘S’ Screen YES

More Related