Professional Documents
Culture Documents
sg/~cs1010/
Week5 - 2
Week5 - 3
Week5 - 4
Week5 - 5
Sample run:
CS1010 (AY2013/4 Semester 1)
if-else, switch
Week5 - 8
2. LOOPS! (1/2)
A program without a loop and a structure variable isnt worth writing.
Alan J.Perlis Yale University The first recipient of ACM Turing Award
Week5 - 9
2. LOOPS! (2/2)
Loop condition
cond?
true
loop body
Some statement(s)
Week5 - 10
Keep prompting the user to input a non-negative integer, and output that integer. Halt the loop when the input is negative.
Key observations:
Enter a number: 12 You entered: 12 Enter a number: 0 You entered: 0 Enter a number: 26 You entered: 26 Enter a number: 5 You entered: 5 Enter a number: -1
You keep repeating a task while certain condition is met, or alternatively, you repeat until the condition is not met. You do not know beforehand how many iterations there will be.
Week5 - 11
if (num >= 0) { print the value entered read num } else end input request ....
Enter a number: 12 You entered: 12 Enter a number: 0 You entered: 0 Enter a number: 26 You entered: 26 Enter a number: 5 You entered: 5 Enter a number: -1
Week5 - 12
false
printf("Enter a number: "); scanf("%d", &num); while (num >= 0) { printf("You entered: %d\n", num); printf("Enter a number: "); scanf("%d", &num); } return 0; }
Week5 - 13
Week5 - 14
Keep prompting the user to enter a nonnegative integer. Halt the loop when the input is negative, and output the maximum integer entered so far.
Enter a number: 12 Enter a number: 0 Enter a number: 26 Enter a number: 5 Enter a number: -1 The maximum number is 26
Week5 - 15
maxi = 0; read num; while (num >= 0) { if (maxi < num) maxi = num; read num; } print maxi;
printf("Enter a number: "); scanf("%d", &num); while (num >= 0) { if (maxi < num) { maxi = num; } printf("Enter a number: "); scanf("%d", &num); } prinf("The maximum number is %d\n", maxi); return 0;
}
CS1010 (AY2013/4 Semester 1) Week5 - 17
Output: ?
a = a + 2;
}
Week5 - 18
Output: ?
a = a + 2;
}
Week5 - 19
(a)
Trace the following codes manually and write out their outputs (assume all variables are of type int)
a = 1; while (a*a < 100) { printf("%d ", a); a *= 2; } printf("\n"); b = 0; c = 9; while (b < c) { printf("b=%d, c=%d\n", b, c); b++; c--; } printf("outside: b=%d, c=%d\n", b, c);
Week5 - 20
(b)
while (n > 0) { digit = n%10; printf("%d\n", digit); n /= 10; } } n initially n @ point digit @ point
What are the values of n and digit after exiting the loop?
true
cond?
false
Week5 - 24
Week5_Count_digits.c
Week5 - 27
// Precond: n > 0 void print_odd_integers(int n) int i; for (i=1; i<=n; i+=2) printf("%d ", i); printf("\n"); }
CS1010 (AY2013/4 Semester 1)
Week5 - 28
Week5 - 29
Call this program Week5_SumMultiples3.c Sample run: Enter a positive integer: 50 Sum = 408
Week5 - 30
Week5 - 31
Enter n: -2 Done!
Week5 - 32
Off-by-one error; make sure the loop repeats exactly the correct number of iterations. Make sure the loop body contains a statement that will eventually cause the loop to terminate. Using = where it should be == Putting ; where it should not be (just like for the if statement)
Week5 - 35
double one_seventh = 1.0/7.0; double f = 0.0; while (f != 1.0) { printf("%f\n", f); f += one_seventh; }
Week5_Caution1.c
Week5 - 36
Week5 - 37
Week5 - 38
Week5 - 39
// with break int n, i = 1, sum = 0; while (i <= 5) { scanf("%d", &n); if (n < 0) break; sum += n; i++; }
CS1010 (AY2013/4 Semester 1)
Week5 - 41
Write a program Week5_PrimeTest.c. You should include a function is_prime(int). (What does it return?) Sample runs:
Enter a positive integer: 131 131 is a prime. Enter a positive integer: 713 713 is not a prime.
Week5 - 42
Week5 - 43
Week5 - 44
With these, you are able to solve just any computing problem! However, writing good programs is more than just learning the syntax:
Week5 - 45
Announcements/Things-to-do
Week5 - 46
End of File