1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

Lecture 5 repetition statements

43 4 0

Đ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

Thông tin cơ bản

Tiêu đề Repetition statements
Tác giả TS. Võ Thị Ngọc Châu
Trường học Ho Chi Minh City University of Technology
Chuyên ngành Computer Science and Engineering
Thể loại Giáo trình
Năm xuất bản 2017 – 2018
Thành phố Ho Chi Minh City
Định dạng
Số trang 43
Dung lượng 1,5 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

Chapter 5: Repetition Statements

chauvtn@hcmut.edu.vn)

Trang 3

References

[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 6

double minNumber = positiveNumber[0];

iteration = iteration + 1;

Trang 7

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

submit homework (i.e no attachment exists)

Trang 10

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 11

while 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 12

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 13

while 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 16

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

Trang 17

do 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 18

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

Trang 19

19

Given a sequence of N numbers input sequentially

by a user, find the minimum one

Trang 21

for 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 24

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

Trang 25

for 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 26

for Statements

Given a natural number N greater than 0, list all the squared numbers

smaller than the given number

Trang 27

for Statements

27

Trang 28

while (<expression_1>) {

do {

… }

while (<expression_2>);

for(<expression_3>;<expression_4>;<expression_5>) {

… }

… }

for (…; …; …) {

for (…; …; …) {

… }

Trang 29

Nested 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 30

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

Trang 31

Nested Repetition Statements

31

N=5

Given a natural number greater than 0, print two squared isosceles triangles of stars

Trang 32

continue Statements

remainder of the body of a repetition

statement and proceeding with the next

iteration of the loop

 while statements

 do while statements

Trang 33

continue Statements

The second loop has been skipped!

Trang 34

break Statements

from certain control statements

 switch case statements

 while statements

 do while statements

 for statements

Trang 37

Infinite Loops

37

Trang 38

Put 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 39

Put 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 40

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:

Trang 41

41

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 43

Chapter 5: Repetition Statements

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

w