Download presentation
Presentation is loading. Please wait.
Published byAbel Goodman Modified over 9 years ago
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 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"); 0 Screen 5 lim 0 fib1 1 fib2 --- fib_next
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"); 0 Screen 5 lim 0 fib1 1 fib2 --- fib_next
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"); 0 1 Screen 5 lim 0 fib1 1 fib2 --- fib_next
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"); 0 1 Screen 5 lim 0 fib1 1 fib2 1 fib_next
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"); 0 1 Screen 5 lim 1 fib1 1 fib2 1 fib_next
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"); 0 1 Screen 5 lim 1 fib1 1 fib2 1 fib_next
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"); 0 1 Screen 5 lim 1 fib1 1 fib2 1 fib_next
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"); 0 1 1 Screen 5 lim 1 fib1 1 fib2 1 fib_next
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"); 0 1 1 Screen 5 lim 1 fib1 1 fib2 2 fib_next
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"); 0 1 1 Screen 5 lim 1 fib1 1 fib2 2 fib_next
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"); 0 1 1 Screen 5 lim 1 fib1 2 fib2 2 fib_next
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"); 0 1 1 Screen 5 lim 1 fib1 2 fib2 2 fib_next
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"); 0 1 1 2 Screen 5 lim 1 fib1 2 fib2 2 fib_next
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"); 0 1 1 2 Screen 5 lim 1 fib1 2 fib2 3 fib_next
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"); 0 1 1 2 Screen 5 lim 2 fib1 2 fib2 3 fib_next
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"); 0 1 1 2 Screen 5 lim 2 fib1 3 fib2 3 fib_next
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"); 0 1 1 2 Screen 5 lim 2 fib1 3 fib2 3 fib_next
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"); 0 1 1 2 3 Screen 5 lim 2 fib1 3 fib2 3 fib_next
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"); 0 1 1 2 3 Screen 5 lim 2 fib1 3 fib2 5 fib_next
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"); 0 1 1 2 3 Screen 5 lim 3 fib1 3 fib2 5 fib_next
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"); 0 1 1 2 3 Screen 5 lim 3 fib1 5 fib2 5 fib_next
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"); 0 1 1 2 3 Screen 5 lim 3 fib1 5 fib2 5 fib_next
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"); 0 1 1 2 3 Screen 5 lim 3 fib1 5 fib2 5 fib_next
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 #include int main() { char c; char upper_c; printf(“Enter a string: "); c = getchar(); Buffer ‘#’‘@’ c upper_c Screen
34
Low2up – step by step #include int main() { char c; char upper_c; printf(“Enter a string: "); c = getchar(); yeS\n Buffer ‘#’‘@’ c upper_c Screen
35
Low2up – step by step #include int main() { char c; char upper_c; printf (“Enter a string: "); c = getchar(); eS\n Buffer ‘y’‘@’ c upper_c Screen
36
Low2up – step by step 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 Buffer ‘y’‘@’ c upper_c Screen
37
Low2up – step by step 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 Buffer ‘y’‘@’ c upper_c Screen
38
Low2up – step by step 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 Buffer ‘y’‘Y’ c upper_c Screen
39
Low2up – step by step eS\n Buffer ‘y’‘Y’ c upper_c Y Screen 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');
40
Low2up – step by step S\n Buffer ‘e’‘Y’ c upper_c Y Screen 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');
41
Low2up – step by step S\n Buffer ‘e’‘Y’ c upper_c Y Screen 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');
42
Low2up – step by step S\n Buffer ‘e’‘Y’ c upper_c Y Screen 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');
43
Low2up – step by step S\n Buffer ‘e’‘E’ c upper_c Y Screen 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');
44
Low2up – step by step S\n Buffer ‘e’‘E’ c upper_c YE Screen 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');
45
Low2up – step by step \n Buffer ‘S’‘E’ c upper_c YE Screen 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');
46
Low2up – step by step \n Buffer ‘S’‘E’ c upper_c YE Screen 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');
47
Low2up – step by step \n Buffer ‘S’‘E’ c upper_c YE Screen 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');
48
Low2up – step by step \n Buffer ‘S’ c upper_c YE Screen 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');
49
Low2up – step by step \n Buffer ‘S’ c upper_c YES Screen 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');
50
Low2up – step by step Buffer ‘\n’‘S’ c upper_c YES Screen 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');
51
Low2up – step by step Buffer ‘\n’‘S’ c upper_c YES Screen 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');
52
Low2up – step by step Buffer ‘\n’‘S’ c upper_c YES Screen 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');
53
Exercise Input: Two integers – A and B Output: How many times A contains B This is the result of the integer division A/B Note: Do not use the division operator!
54
Solution #include int main() { int a, b, res; printf("Please enter two numbers.\n"); scanf("%d%d", &a, &b); res = 0; while ( (res+1) * b <= a) res = res + 1; printf("%d / %d = %d", a, b, res); return 0; }
55
break in a loop When break is encountered, the loop is stopped regardless of whether the condition is still true. The program then continues to run from the first line after the while loop. If called within a nested loop, break breaks out of the inner loop only.
56
Example – counting letters break.c
57
continue When continue is encountered, the rest of the loop body is ignored. The program then continues to run from the beginning of the loop.
58
for loops for loops are controlled by a counter variable. for (c = begin; c <= end; c += inc) { loop body } initializationconditionincrement
59
The factorial example again, this time using for #include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i=1; i<=n; ++i) { fact *= i; } printf("the factorial is %d\n", fact); return 0; }
60
Factorial with for – step by step --- i 3 n 1 fact #include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i=1; i<=n; ++i) { fact *= i; } printf("the factorial is %d\n", fact); return 0; }
61
Factorial with for – step by step 1 i 3 n 1 fact #include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i=1; i<=n; ++i) { fact *= i; } printf("the factorial is %d\n", fact); return 0; }
62
Factorial with for – step by step 1 i 3 n 1 fact #include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i=1; i<=n; ++i) { fact *= i; } printf("the factorial is %d\n", fact); return 0; }
63
Factorial with for – step by step 2 i 3 n 1 fact #include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i=1; i<=n; ++i) { fact *= i; } printf("the factorial is %d\n", fact); return 0; }
64
Factorial with for – step by step 2 i 3 n 2 fact #include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i=1; i<=n; ++i) { fact *= i; } printf("the factorial is %d\n", fact); return 0; }
65
Factorial with for – step by step 3 i 3 n 2 fact #include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i=1; i<=n; ++i) { fact *= i; } printf("the factorial is %d\n", fact); return 0; }
66
3 i 3 n 6 fact #include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i=1; i<=n; ++i) { fact *= i; } printf("the factorial is %d\n", fact); return 0; } Factorial with for – step by step
67
4 i 3 n 6 fact #include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i=1; i<=n; ++i) { fact *= i; } printf("the factorial is %d\n", fact); return 0; } Factorial with for – step by step
68
4 i 3 n 6 fact #include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i=1; i<=n; ++i) { fact *= i; } printf("the factorial is %d\n", fact); return 0; }
69
for loops (cont.) Equivalent to while. Any for loop can be converted to while loop and vice versa Some applications are more natural to for, and others to while. If we want to perform something for a predefined number of times, better use for. If we just wait for something to happen (not after a certain number or iterations), better use while.
70
Example – fahrenheit-celsius conversion table /* Print a Fahrenheit-to-Celsius conversion table */ #include int main ( ) { int fahr; double celsius; int lower = 0, upper = 300; int step = 20; for (fahr=lower; fahr<=upper; fahr += step) { celsius = 5.0*(fahr -32.0)/9.0; printf("%d\t%g\n", fahr, celsius); } return 0; }
71
Nested for loop – rectangle example /* Print a rectangle of *. The height and width are defined by the user */ #include int main( ) { int i, j; int height, width; printf("Please enter the two box dimensions: \n"); scanf("%d%d", &height, &width); for (i = 1; i <= height; i++) { for (j = 1; j <= width; j++) printf("*"); printf("\n"); }
72
Exercise Write a program that prints an upside-down half triangle of *. The height of the pyramid is the input. ***** *** ** **** *
73
Solution #include int main() { int i, j, size; printf(“Please enter a size:\n”); scanf(“%d”,&size); for (i = 1; i <= size; i++) { for (j = i; j <= size; j++) printf("*"); printf("\n"); } return 0; }
74
Exercise Write a program accepts a number from the user, and prints out all of the prime numbers up to that number.
75
Solution #include int main() { int i, j, last; printf("enter a number\n"); scanf("%d", &last); for (i = 2; i <= last; i++) { for (j = 2 ; j < i; j++) { if (i % j == 0) break; } if (j == i) printf("the number %d is prime\n", i); } return 0; }
76
Exercise Change the former program, so that is displays only the largest prime number which is smaller than or equal to the user’s input.
77
Solution 1 #include int main() { int i, j, last; int found = 0; /* This indicates if we found the largest prime */ printf("enter a number\n"); scanf("%d", &last); i = last; while (!found) /* Loop until we find our guy */ { for (j = 2 ; j < i; j++) if (i % j == 0) break; if (j == i) /* If this is true then i is prime */ found = 1; else i--; } printf("The largest prime not larger than %d is %d.\n", last, i); return 0; }
78
Solution 2 (with break) #include int main() { int i, j, last; printf("enter a number\n"); scanf("%d", &last); for (i=last; i>1; i--) { for (j = 2 ; j < i; j++) if (i % j == 0) break; if (j == i) /* i is prime. We found our guy */ break; } printf("The largest prime not larger than %d is %d.\n", last, i); return 0; }
79
do while loops do { statement(s) } while ( expression ); Similar to while loops Except the condition is evaluated after the loop body The loop body is always executed at least once, even if the expression is never true
80
Example – waiting for legal input #include int main() { int i; printf("Please enter a positive number.\n"); do { scanf("%d", &i); if (i <= 0) printf("Try again.\n"); } while (i<=0); /* The program continues.... */ return 0; }
81
Debugging It is virtually impossible to program without errors Syntax errors are detected by the compiler However, often a program has no syntax errors and compiles, but still doesn’t perform as desired
82
Debugging Debuggers are software tools designed to help find software bugs Both Visual C and the lcc compiler include a debugger
83
Debugging The debugger allows us to – Execute the program one line at a time At each step see the values of all variables and expressions Run the program up to a pre-specified point And more…
84
The debugger’s common features Setting breakpoints (a point where the execution stops): bring the cursor to desired line and press the palm icon or F9. A dark red dot appears near the line. Executing a debugged run: Build->start debug->go or F5. The program will run and stop at the first breakpoint.
85
The debugger’s common features (cont.) Stopping at a specific line: Bringing the cursor to the line and press ctrl+F10, or Build->start debug->go to cursor. The program will stop at that point. Stepping to the next line – F10. Entering a function – F11. Seeing variable values – quickwatch and/or debug window at the bottom. The yellow arrow indicates our whereabouts at any given moment.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.