Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu

Content

 Introduction

 while. Statements

 do.while. Statements

 for. Statements

 Nested Repetition Statements

 continue Statements

 break Statements

 Summary

Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu trang 1

Trang 1

Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu trang 2

Trang 2

Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu trang 3

Trang 3

Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu trang 4

Trang 4

Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu trang 5

Trang 5

Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu trang 6

Trang 6

Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu trang 7

Trang 7

Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu trang 8

Trang 8

Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu trang 9

Trang 9

Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu trang 10

Trang 10

Tải về để xem bản đầy đủ

pdf 43 trang xuanhieu 9180
Bạn đang xem 10 trang mẫu của tài liệu "Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

Tóm tắt nội dung tài liệu: Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu

Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition Statements - Võ Thị Ngọc Châu
 Ho Chi Minh City University of Technology 
 Faculty of Computer Science and Engineering 
Chapter 5: Repetition Statements 
 Introduction to Computer Programming 
 (C language) 
 TS. Võ Thị Ngọc Châu 
 (chauvtn@cse.hcmut.edu.vn, 
 chauvtn@hcmut.edu.vn) 
 2017 – 2018, Semester 2 
Course Content 
 C.1. Introduction to Computers and 
 Programming 
 C.2. C Program Structure and its 
 Components 
 C.3. Variables and Basic Data Types 
 C.4. Selection Statements 
 C.5. Repetition Statements 
 C.6. Functions 
 C.7. Arrays 
 C.8. Pointers 
 C.9. File Processing 2 
 References 
 [1] “C: How to Program”, 7th Ed. – Paul 
 Deitel and Harvey Deitel, Prentice Hall, 2012. 
 [2] “The C Programming Language”, 2nd Ed. 
 – Brian W. Kernighan and Dennis M. Ritchie, 
 Prentice Hall, 1988 
 and others, especially those on the Internet 
 3 
Content 
 Introduction 
 while.. Statements 
 do..while.. Statements 
 for.. Statements 
 Nested Repetition Statements 
 continue Statements 
 break Statements 
 Summary 
 4 
 Introduction 
 Control statements in C 
  Sequence 
 Assignment 
 Function calling 
  Selection 
 if 
 if..else.. 
 switch..case.. 
  Repetition 
 for.. 
 while.. 
 do..while.. 
 5 
 Introduction 
 Given a void main() { 
 double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12}; 
 set of n 
 int n = 10; 
 positive double minNumber = positiveNumber[0]; 
 int iteration = 1; 
 numbers, 
 while (iteration < n) { 
 find the if (minNumber <= positiveNumber[iteration]) 
 iteration = iteration + 1; 
 smallest 
 else { 
 one. minNumber = positiveNumber[iteration]; 
 iteration = iteration + 1; 
(Chapter 1 – 
 } 
Real code in C) } Control Statements for Repetition 
 } 
 6 
 Introduction 
 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. 
 Example 2: Search for any of you who didn’t 
 submit homework (i.e. no attachment exists) 
 Example 3: Count the number of occurrences 
 of an important keyword in a text 7 
 while.. Statements 
while () ; 
 false (= 0) 
while () { 
 ; true ( 0) 
 ; 
} 
 1. is evaluated. 
 2. If is true ( 0), are performed 
 and then go to step 3. Otherwise, i.e. if is false 
 (=0), go to step 4. 
 3. Go back to step 1. 
 4. End the repetition statement and move forward. 8 
while.. Statements 
 false (= 0) 
 i<=3 
 true ( 0) 
 printf(); 
 i++; 
 false (= 0) 
 i>=3 
 true ( 0) 
 printf(); 
 i++; 
 9 
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. 
 10 
while.. 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. 
 11 
while.. Statements 
 Given N, a natural number greater than 0, calculate and print the 
 factorial of N: N! = 1*2*..*N = (N-1)!*N 
 12 
 Given a natural number N 
while.. Statementsgreater than 0, list a series of 
 Fibonacci numbers smaller than N. 
 13 
 do..while.. Statements 
 do { 
 ; 
 ; true( 0) 
 } 
 while (); false(=0) 
 are always 
 1. are performed. 
 performed at least once! 
 2. is evaluated. 
 3. If is true ( 0), go to step 1. Otherwise, i.e. if 
 is false (=0), go to step 4. 
 4. End the repetition statement and move forward. 
 14 
do..while.. Statements 
 printf(); 
 i++; 
 true( 0) 
 i<=3 
 false(=0) 
 printf(); 
 i++; 
 true( 0) 
 i>=3 
 false(=0) 
do..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. 
 16 
do..while.. 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. 
 17 
do..while.. Statements 
 Given N, a natural number greater than 0, calculate and print the 
 factorial of N: N! = 1*2*..*N = (N-1)!*N 
 18 
Given a sequence of N numbers input sequentially 
by a user, find the minimum one. 
 19 
 for.. Statements 
 false (=0) 
 true ( 0) 
for (; ; ) ; 
for (; ; ) { 
 ; 
 ; 
} 20 
for.. Statements 
 false (=0) 
 true ( 0) 
1. is evaluated. 
2. is evaluated. 
3. If is true ( 0), are performed, 
 is evaluated, and then go to step 2. Otherwise, i.e. 
 if is false (=0), go to step 4. 
4. End the repetition statement and move forward. 21 
for.. Statements 
for (; ; ) ; 
is regarded as: 
 ; 
 while () { 
 ; 
 ; 
 } 
for (; ; ) { 
 ; 
 ; 
} 
is regarded as: 
 ; 
 while () { 
 ; 
 ; 
 ; 
 } 22 
 for.. Statements 
 i=1; 
 false (=0) 
 i<=3 
 true ( 0) 
 printf(); i++; 
No printing result exists due to a false value of the expression “i>=3”. 23 
for.. 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. 
 24 
for.. Statements 
 Given N, a natural number greater than 0, calculate and print the 
 factorial of N: N! = 1*2*..*N = (N-1)!*N 
 25 
for.. Statements 
 Given a natural number N greater than 0, list all the squared numbers 
 smaller than the given number. 
 26 
for.. Statements 
 27 
while () { Nested Repetition 
 do { Statements 
 } 
 while (); 
 for(;;) { 
 } 
} 
for (; ; ) { A repetition statement 
 for (; ; ) { 
  can contain other 
 } 
 while () { repetition statements in 
 } many various 
} 
 combination ways. 
 28 
Nested Repetition Statements 
 Given a size of a window (N is an odd number and N>=5), 
 print a star of stars: diagonal and center lines 
 29 
Nested Repetition Statements 
 Given a natural number N greater than 0, print a 
 triangle full of stars. N is the height of the star triangle. 
 N = 4 
 30 
 Given a natural number greater than 0, 
Nested Repetitionprint two Statements squared isosceles triangles of 
 stars. 
 N=5 
 31 
continue Statements 
 the continue statement for skipping the 
 remainder of the body of a repetition 
 statement and proceeding with the next 
 iteration of the loop 
  while.. statements 
  do.. while.. statements 
  for.. statements 
 32 
continue Statements 
 The second loop has been skipped! 
 33 
break Statements 
 the break statement for exiting immediately 
 from certain control statements 
  switch..case statements 
  while.. statements 
  do.. while.. statements 
  for.. statements 
 34 
break Statements 
 All the loops from the second loop 
 have been skipped! 
 A corresponding repetition statement 
 is then ended. 
 35 
 Infinite Loops 
while (1) ; do { 
 ; 
while (1) { 
 ; 
 ; 
 } 
 ; 
 while (1); 
} 
 for (; 1;) 
for (; ;) ; 
 ; 
for (; ;) { 
 ; for (; 1;) 
 { ; 
 ;  
} ; 
 } 36 
Infinite Loops 
 37 
Put them all together 
 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. 
 Natural number e is approximated: 
 38 
Put them all together 
 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. 
 39 
Put 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: 
 1 2 3 4 0 
 12 13 14 5 12 
 11 16 15 6 
 10 9 8 7 
 Given N = 5, a printed spiral-shaped matrix is as follows: 
 1 2 3 4 5 0 
 16 17 18 19 6 16 
 15 24 25 20 7 24 
 14 23 22 21 8 
 13 12 11 10 9 40 
 N=10 
 N=9 
How to start the spiral at 
any given position (i,j)? 41 
 Summary 
 Three control statement types of repetition 
  while.. 
  do.. while.. 
  for... 
 Repeat actions in connection with conditions 
 break and continue statements 
  Important and helpful for controlling the loops in 
 repetition 
 Infinite loops 
  Check the conditions for repetition carefully 
 42 
Chapter 5: Repetition Statements 
 43 

File đính kèm:

  • pdfbai_giang_introduction_to_computer_programming_c_language_ch.pdf