Lecture 5 repetition statements

43 3 0
Lecture 5   repetition statements

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệ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 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  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 Content  Introduction  while Statements  while  for Statements Statements  Nested Repetition Statements  continue  break Statements Statements  Summary Introduction  Control statements in C  Sequence     Selection     Assignment Function calling … if if else switch case Repetition    for while while Introduction  Given a void main() { set of n double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12}; positive double minNumber = positiveNumber[0]; numbers, int iteration = 1; int n = 10; while (iteration < n) { find the if (minNumber =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, print two squared isosceles triangles of Nested Repetition Statements 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  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  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) ; { ; while (1) { … ; … ; } for (; ;) ; for (; ;) { ; … ; } ; } while (1); for (; 1;) ; 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! = 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 in a spiral-shaped matrix with a given N Given N = 4, a printed spiral-shaped matrix is as follows: 12 11 10 13 16 14 15 12 Given N = 5, a printed spiral-shaped matrix is as follows: 16 17 18 19 15 24 25 20 14 23 22 21 13 12 11 10 16 24 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  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

Ngày đăng: 11/04/2023, 18:55

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan