860 likes | 884 Views
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
E N D
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 Some changing state Loop while some condition holds
Loops • C provides some flexible ways of deciding how many times to loop, or when to exit a loop. • for,while,do-while loops.
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.
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; }
Example – fibonacci series fibonacci.c
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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); ====
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); ====
Example – lower-case to upper case. low2up.c
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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