Statements 11 Given N, the number of the first natural numbers greater than 0, calculate and print the total sum of these N first natural numbers... Statements 17 Given N, the number
Trang 1Chapter 5: Repetition Statements
chauvtn@hcmut.edu.vn)
Trang 3References
[1] “C: How to Program”, 7th Ed – Paul
Deitel and Harvey Deitel, Prentice Hall, 2012
– Brian W Kernighan and Dennis M Ritchie, Prentice Hall, 1988
and others, especially those on the Internet
3
Trang 6double minNumber = positiveNumber[0];
iteration = iteration + 1;
Trang 7Introduction
A repetition statement allows you to specify that an action is to be repeated while some
condition remains true
Example 1: Input validation
Ask a user to input a value
While his/her input value is invalid, ask him/her
to input a value
submit homework (i.e no attachment exists)
Trang 10while Statements
Write a program to receive a natural number from a user If an invalid value is entered, ask the user to input again until a valid one is obtained
Trang 11while Statements
11
Given N, the number of the first natural numbers
greater than 0, calculate and print the total sum of
these N first natural numbers
Trang 12while Statements
Given N, a natural number greater than 0, calculate and print the
factorial of N: N! = 1*2* *N = (N-1)!*N
Trang 13while Statements
13
Given a natural number N
greater than 0, list a series of
Fibonacci numbers smaller than N
Trang 14<Statements> are always performed at least once!
Trang 16do while Statements
Write a program to receive a natural number from a user If an invalid value is entered, ask the user to input again until a valid one is obtained
Trang 17do while Statements
17
Given N, the number of the first natural numbers
greater than 0, calculate and print the total sum of
these N first natural numbers
Trang 18do while Statements
Given N, a natural number greater than 0, calculate and print the
factorial of N: N! = 1*2* *N = (N-1)!*N
Trang 1919
Given a sequence of N numbers input sequentially
by a user, find the minimum one
Trang 21for Statements
1. <expression_1> is evaluated
2. <expression_2> is evaluated
3. If <expression_2> is true (0), <Statements> are performed,
<expression_3> is evaluated, and then go to step 2 Otherwise, i.e
if <expression_2> is false (=0), go to step 4
4. End the repetition statement and move forward 21
Trang 24for Statements
Given N, the number of the first natural numbers greater than 0,
calculate and print the total sum of these N first natural numbers
Trang 25for Statements
25
Given N, a natural number greater than 0, calculate and print the
factorial of N: N! = 1*2* *N = (N-1)!*N
Trang 26for Statements
Given a natural number N greater than 0, list all the squared numbers
smaller than the given number
Trang 27for Statements
27
Trang 28while (<expression_1>) {
…
do {
… }
while (<expression_2>);
…
for(<expression_3>;<expression_4>;<expression_5>) {
… }
… }
for (…; …; …) {
…
for (…; …; …) {
… }
Trang 29Nested Repetition Statements
29
Given a size of a window (N is an odd number and N>=5),
print a star of stars: diagonal and center lines
Trang 30Nested Repetition Statements
Given a natural number N greater than 0, print a triangle full of stars N is the height of the star triangle
Trang 31Nested Repetition Statements
31
N=5
Given a natural number greater than 0, print two squared isosceles triangles of stars
Trang 32continue Statements
remainder of the body of a repetition
statement and proceeding with the next
iteration of the loop
while statements
do while statements
Trang 33continue Statements
The second loop has been skipped!
Trang 34break Statements
from certain control statements
switch case statements
while statements
do while statements
for statements
Trang 37Infinite Loops
37
Trang 38Put them all together
Natural number e is approximated:
Write a program to compute an approximation of e with
a positive number N input by a user Print the approximated value and its difference from a commonly used value which is 2.71828 It is given that: 0! = 1! = 1
Trang 39Put them all together
39
Write a program to compute an
approximation of the power x of e
where there is no use of a true value
of e Accuracy of an approximation is dependent on how large n is
Trang 40Put them all together
Given problem: print the first NxN natural
numbers greater than 0 in a spiral-shaped matrix with a given N
Given N = 4, a printed spiral-shaped matrix is as follows:
Trang 4141
N=10
N=9
How to start the spiral at
any given position (i,j)?
Trang 42 Repeat actions in connection with conditions
Important and helpful for controlling the loops in repetition
Trang 43Chapter 5: Repetition Statements