Download presentation
Presentation is loading. Please wait.
1
Repetition statements
for statement
2
1. Compound assignment operators
We have seen before this statement: total_paper = total_paper + paper_order; C provides a more concise notation for such statements: total_paper += paper_order; In general: variable1 = variable1 op expression Can be written as: variable1 op= expression where op can be +, -, *, / and % Dr. Soha S. Zaghloul 2
3
2. Compound assignment operators - example
Statement with Simple Assignment Operator Equivalent statement with Compound Assignment Operator count_emp = count_emp + 1; count_emp += 1; time = time – 1; time -= 1; total_time = total_time + time; total_time += time; product = product * time; product *= time; n = n * (x + 1); n *= x + 1; x = x / y; x /= y; a = a % b; a %= b; Dr. Soha S. Zaghloul 3
4
3. The Increment operator
This is another concise notation for writing C statements. The statement: counter = counter + 1; may be written as: counter += 1; or just simply as counter++; // postfix increment (after the operand) or as ++counter; // prefix increment (before the operand) Dr. Soha S. Zaghloul 4
5
4. The decrement operator
This is another concise notation for writing C statements. The statement: counter = counter - 1; may be written as: counter -= 1; or just simply as counter--; // postfix decrement (after the operand) or as --counter; // prefix decrement (before the operand) Dr. Soha S. Zaghloul 5
6
5. The postfix and prefix operators
When the ++ is placed immediately in front of its operand (prefix increment), then increment the operand first, then use it. When the ++ is placed immediately after its operand (postfix increment), then use the operand first, then increment it. The same applies for the prefix and postfix decrement operators. Dr. Soha S. Zaghloul 6
7
6. The postfix and prefix operators – example 1
Assume the initial value of i is 2. Consider the following expressions: Expression Explanation Equivalent to Values after j = ++i; increment i then use (assign) it i = i + 1; j = i; i = 3 j = 3 j = i++; use (assign) i then increment it j = 2 Dr. Soha S. Zaghloul 7
8
7. The postfix and prefix operators – example 2
Assume the initial value of n is 4. Consider the following code fragments: Assume that the statement: printf (“n = %3d”, n); Comes next to each of the above statements. What will be the output? Expression Explanation Equivalent to Output printf (“%3d”, --n); decrement n then use (print) it n = n – 1; printf (“%3d”, n); ~~3 printf (“%3d”, n--); use (print) n then decrement it ~~4 In both cases, the output is n = ~~3 Dr. Soha S. Zaghloul 8
9
Statements to be repeated
8. The for statement The for statement executes a number of statements many times using a loop control variable (counter). The counter defines three main parts: The initial value The condition The step For example: for (i = 0; i < 5; i++) { printf (“i = %3d\n”, i); } The counter used in the previous loop is the variable i. Condition Initial value Step Statements to be repeated Dr. Soha S. Zaghloul 9
10
9. The for statement – cont’d
The initial value of the counter determines the first value to be used when executing the statements. The loop repeats as long as the condition is true. The step is the change (or update) in the value of the counter. The loop counter should be always of type integer. Dr. Soha S. Zaghloul 10
11
10. The for statement – example 1
Consider the following code fragment and let us trace it: The trace: for (i = 0; i < 5; i++) { printf (“i = %3d \n”, i); } Iteration # i++ i < 5? Output 1 0: initial value 0 < 5? True ~~0 2 i= 1 1 < 5? True ~~1 3 i= 2 2 < 5? True ~~2 4 i= 3 3 < 5? True ~~3 5 i= 4 4 < 5? True ~~4 6 i= 5 5 < 5? False Exit from loop Dr. Soha S. Zaghloul 11
12
11. The for statement – example 2
Consider the following code fragment and let us trace it: The trace: for (i = 5; i >= 0; i--) { printf (“i = %3d \n”, i); } Iteration # i-- i >= 0? Output 1 5: initial value 5 >= 0? True ~~5 2 i= 4 4 >= 0? True ~~4 3 i= 3 3 >= 0? True ~~3 4 i= 2 2 >= 0? True ~~2 5 i= 1 1 >= 0? True ~~1 6 i= 0 0 >= 0? True ~~0 7 i= -1 -1 >= 0? False Exit from loop Dr. Soha S. Zaghloul 12
13
12. The for statement – example 3
Consider the following code fragment and let us trace it: The trace: for (i = 0; i <= 5; i++) { printf (“i doubled = %3d \n”, 2*i); } Iteration # i++ i <= 5? 2*i Output 1 0: initial value 0 <= 5? True ~~0 2 i= 1 1 <= 5? True ~~2 3 i= 2 2 <= 5? True 4 ~~4 i= 3 3 <= 5? True 6 ~~6 5 i= 4 4 <= 5? True 8 ~~8 i= 5 5 <= 5? True 10 ~10 7 i= 6 6 <= 5? False Exit from the loop Dr. Soha S. Zaghloul 13
14
13. The for statement – example 4
Write a complete program that calculates the factorial of an integer number n such that n is greater than zero. Input? n Type? integer Value? Through scanf Restriction? n > 0 Output? n! 1! = 1 2! = 2 x 1 = 2 x 1! 3! = 3 x 2 x 1 = 3 x 2! 4! = 4 x 3 x 2 x 1 = 4 x 3! 5! = 5 x 4 x 3 x 2 x 1 = 5 x 4! n! = n * (n – 1) * (n – 2) * (n – 3) * … 1 = n * (n-1)! Dr. Soha S. Zaghloul 14
15
13. The for statement – example 4 (cont’d)
#include <stdio.h> int main (void) { int n; printf (“enter an integer number: \n”); scanf (“%d”, &n); return (0); } n! = n * (n – 1) * (n – 2) * (n – 3) * … 1 = n * (n – 1)! Initial value: n Step: -1 Condition: n >= 1 for (i = n; i >= 1; i--) { } Dr. Soha S. Zaghloul 15
16
13. The for statement – example 4 (cont’d)
#include <stdio.h> int main (void) { int n, i; printf (“enter an integer number: \n”); scanf (“%d”, &n); for (i = n; i >= 1; i--) } return (0); for (i = n; i >= 1; i--) { product = product * i; } Dr. Soha S. Zaghloul 16
17
13. The for statement – example 4 (cont’d)
#include <stdio.h> int main (void) { int n, i; int product; printf (“enter an integer number: \n”); scanf (“%d”, &n); product = 1; for (i = n; i >= 1; i--) product = product * i; // or product *= i; } printf (“factorial of %3d = %6d\n”, n, product); return (0); What if the user entered a number less than 1? Dr. Soha S. Zaghloul 17
18
13. The for statement – example 4 (cont’d)
#include <stdio.h> int main (void) { int n, i; int product; printf (“enter an integer number: \n”); scanf (“%d”, &n); if (n >= 1) product = 1; for (i = n; i >= 1; i--) product = product * i; // or product *= i; } //end of for loop printf (“factorial of %3d = %6d\n”, n, product); } // if (n >= 1) else printf (“invalid input\n”); return (0); } //end of main function Dr. Soha S. Zaghloul 18
19
13. The for statement – example 4 - trace
i-- i >= 1? product -3 -3 >= 1? false 1: initial value output: invalid input n n >= 1? i-- i >= 1? product 1: initial value 4 4 >= 1? true 4: initial value 4 * 1 = 4 i= 3 3 >= 1? true 3 * 4 = 12 i= 2 2 >= 1? true 2 * 12 = 24 i= 1 1 >= 1? true 1 * 24 = 24 i= 0 0 > =1? false output: factorial of ~~4 = ~~~~24 n n >= 1? i-- i >= 1? product 1 1 >= 1? True 1: initial value 0 >= 1? False output: factorial of ~~1 = ~~~~~1 Dr. Soha S. Zaghloul 19
20
Nested loops consist of an outer loop with one or more inner loops.
Each time the outer loop is repeated, the inner loop restarts from the initial value of its counter. Dr. Soha S. Zaghloul 20
21
15. Nested loops – simple example
for (i= 0; i < 3; i++) for (j = 0; j < 5; j++) printf (“i = %3d /t j = %3d /n”, i, j); i++ i < 3? j++ j < 5? output 0: initial value 0 < 3? true 0 < 5? true i = ~~0 j = ~~0 i= 0 j= 1 1 < 5? true i = ~~0 j = ~~1 j= 2 2 < 5? true i = ~~0 j = ~~2 j= 3 3 < 5? true i = ~~0 j = ~~3 j= 4 4 < 5? true i = ~~0 j = ~~4 j= 5 5 < 5? false exit from inner loop i= 1 1 < 3? true i = ~~1 j = ~~0 i = ~~1 j = ~~1 i = ~~1 j = ~~2 i = ~~1 j = ~~3 i = ~~1 j = ~~4 i= 2 2 < 3? true i = ~~2 j = ~~0 Dr. Soha S. Zaghloul 21
22
15. Nested loops – simple example (cont’d)
for (i= 0; i < 3; i++) for (j = 0; j < 5; j++) printf (“i = %3d /t j = %3d /n”, i, j); i++ i < 3? j++ j < 5? output 2 2 < 3? true j= 1 1 < 5? true i = ~~2 j = ~~1 j= 2 2 < 5? true i = ~~2 j = ~~2 j= 3 3 < 5? true i = ~~2 j = ~~3 j= 4 4 < 5? true i = ~~2 j = ~~4 j= 5 5 < 5? false exit from inner loop 3 3 < 3 false exit from outer loop Dr. Soha S. Zaghloul 22
23
Write a complete program that produces the following output:
16. Nested for – example 1 Write a complete program that produces the following output: Analysis: We need two counters:- The first counter line counts the lines from 1 to 5 The second counter asterisks counts the number of asterisks in each line * ** *** **** ***** Dr. Soha S. Zaghloul 23
24
16. Nested for – example 1 (cont’d)
* ** *** **** ***** Line Asterisks 1 2 3 4 5 Dr. Soha S. Zaghloul 24
25
16. Nested for – example 1 (cont’d)
Line Asterisks 1 2 3 4 5 line is the counter of the outer loop, and asterisks is the counter of the inner loop. line: Initial Value: 1 Final Value: 5 condition is (line <= 5) Step: +1 asterisks: Initial Value: 1 Final Value: line condition is (asterisks <= line) Step: +1 for (line = 1; line <= 5; line++) { for (asterisks = 1; asterisks <= line; asterisks++) printf (“*”); printf (“\n”); } Dr. Soha S. Zaghloul 25
26
16. Nested for – complete program
#include <stdio.h> int main (void) { int line, asterisks; for (line = 1; line <= 5; line++) for (asterisks = 1; asterisks <= line; asterisks++) printf (“*”); printf (“\n”); } // end (line = 1; line <= 5; line++) return (0); } // end of main Dr. Soha S. Zaghloul 26
27
17. Example (1) There are 9,870 people in a town whose population increases by 10 percent each year. Write a loop that displays the annual population for ten consecutive years. The program should also write a message “over population” if the population exceeds 30,000. Dr. Soha S. Zaghloul 27
28
17. Example (1) – solution (cnt’d)
There are 9,870 people in a town whose population increases by 10 percent each year. Write a loop that displays the annual population for ten consecutive years. The program should also write a message “over population” if the population exceeds 30,000. Dr. Soha S. Zaghloul 28
29
17. Example (1) – solution (cnt’d)
There are 9,870 people in a town whose population increases by 10 percent each year. Write a loop that displays the annual population for ten consecutive years. Counter = years Initial value = 0 Final value = 9 condition is (years <= 9) Step = +1 for (years = 0; years <= 9; years++) Dr. Soha S. Zaghloul 29
30
17. Example (1) – solution (cnt’d)
There are 9,870 people in a town whose population increases by 10 percent each year. Write a loop that displays the annual population for ten consecutive years. Initial value of people is 9870 Ten percent of people is (people * 0.01) people increases by 0.01 each year people = people + (people * 0.01); Display the annual population people = 9870; for (years = 0; years <= 9; years++) { people = people + (0.01 * people); // or people += (0.01 * people) printf (“Annual Population = %7.1f”, people); } Dr. Soha S. Zaghloul 30
31
17. Example (1) – solution (cnt’d)
The program should also write a message “over population” if the population exceeds 30,000. Condition: (people > 30,000) people = 9870; for (years = 0; years <= 9; years++) { people = people + (0.01 * people); printf (“annual population = %7.1f”, people); } if (people > 30000) printf (“over population \n”); Dr. Soha S. Zaghloul 31
32
18. Example (2) – Self-check exercise
The table below shows the normal boiling points of several substances. Write a program that prompts the user to enter 1000 observed boiling points of a substance in ºC. The program then identifies the substance if the observed boiling point is within 5% (more or less) of the expected boiling point. If the data input is more than 5% higher or lower than any of the boiling points in the table, the program should output the message “Substance unknown”. Substance Expected Boiling Point (ºC) Water 100 Mercury 357 Copper 1187 Silver 2193 Gold 2660 #include <stdio.h> int main (void) { int years; double people; people = 9870; for (years = 0; years <= 9; years++) people = people + (0.01 * people); printf (“annual population = %7.1f”, people); } //end for if (people > 30000) printf (“over population \n”); return (0); } // end main Dr. Soha S. Zaghloul 32
33
19. Example (3) – self-check exercise
Write a program that calculates and prints the bill for Riyadh’s power consumption for 100 customers. The rates vary depending on whether the user is residential, commercial, or industrial. A code of R corresponds to a Residential, C corresponds to a Commercial, and I to Industrial. Any other code should be treated as an error. The program should read the power consumption rate in KWH (Kilowatt per Hour); then it calculates the due amount according to the following: The rate is SAR 5 per KWH for Residential, SAR 10 per KWH for Commercial and SAR 20 per KWH for Industrial. Dr. Soha S. Zaghloul 33
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.