1. Trang chủ
  2. » Luận Văn - Báo Cáo

chapter 6 control structures

24 0 0
Tài liệu đã được kiểm tra trùng lặp

Đ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 đề Control Structures
Người hướng dẫn Trần Ngọc Hiệp, Msc.
Trường học Petrovietnam University
Chuyên ngành Foundations of Engineering II
Thể loại lecture
Năm xuất bản 2024
Định dạng
Số trang 24
Dung lượng 1,82 MB

Nội dung

TALENTS FOR DEVELOPMENT - Petrovietnam UniversityFoundations of Engineering II- PVUTrần Ngọc Hiệp, MSc.Conditions StatementsSingle selection ifDouble Selection if-elseMultiple Selection

Trang 1

TALENTS FOR DEVELOPMENT - Petrovietnam University

Chapter 6: Control Structures

Lecturer: Trần Ngọc Hiệp, MSc.

Email: hieptn@pvu.edu.vn Mobile: 0901 25 2468

Contents

Selection Statements For Loops

While Loops Do-While Loops Breaking out of Loops 1

Trang 2

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

Conditions Statements

3

Single selection (if) Double Selection (if-else) Multiple Selection (else-if) Multiple Selection (switch)

6.1 If statement

 If statement is used in Python for selection

purposes

3

Trang 3

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

6.1 If statement - Example

5

6.1 If Statement - Problems

Problem 1: Write a

program which gets the

value of two integers from

user and display whether

first number is lower than,

equal to or greater than

the second number.

Problem 2: Prompt user to enter a value Check if the number is even then display a message that number is even If number

is odd then display number is odd.

5

Trang 4

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

6.1 If Statement - Solutions

7

6.1 If Statement - Task

 Write a nested if statement to print the appropriate

activity depending on the value of a variable

temperature and humidity as in the table below:

7

Trang 5

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

6.2 Double selection statement (if…else)

9

6.2 Double selection statement - Problems

Problem 1: Write a program

which inputs the marks obtained

by student If the marks are

equal to or greater than 50 then

display the message that “You

are pass” else “Sorry you are

Trang 6

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

6.2 Double selection statement - Solutions

Trang 7

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

6.3 Multiple Selection (else if)

13

Condition1

If code Else if

codeCondition2

6.3 Multiple Selection - Problem

Problem 1: Write a program which prompt the user to enter a digit between 1 to 10 and display the digit entered by the user.

Problem 2: Write a program to calculate the monthly telephone bills as per by the following rules:

• Minimum 200.000 vnd for up to 100 calls

• Plus 600 vnd per call for next 50 calls.

• Plus 500 vnd per call for next 50 call.

• Plus 400 vnd per call for any call beyond 200 calls.

13

Trang 8

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

6.3 Multiple Selection - Problem

15

6.5 Repetition (Loops)

 Repetition structures are used when a program needs

to repeatedly process one or more instructions until

some condition is met, at which time the loop ends.

 Repetition allows the programmer to efficiently use

variables.

Parameters

of loops:

Starting Point

Ending Point

Sequence

of Moving

15

Trang 9

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

6.6 While loop

17

6.6 While loop Example:

17

Trang 10

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

Trang 11

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

6.6 While loop - Solutions

21

Trang 12

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

6.8 For loop

 The structure of for loop is as

follows:

23

6.8 For loop - Problems

Problem 1: Write the table of 5 using for loop.

Problem 2: Write a program to calculate the factorial of a number using for loop.

23

Trang 13

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

6.8 For loop - Solution

25

6.8 For loop - Solution 25

Trang 14

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

Trang 15

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

Trang 16

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

Trang 17

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

6.9 Nested Loops

The placing of one

loop inside the body

of another loop is

called nesting

We can us loops inside loop body.

First inner loop complete its iteration and then control shift to outer one, this process continues till end.

33

6.9 Nested Loops

 Nested while loop syntax:

33

Trang 18

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

Trang 19

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

6.10 Controlling Loops/ Jumping out of loop

37

We can use break and continue statement to control the

interaction of a loop:

Break statement: Stops the loops

and start executing program from

line after loop.

Continue statement: Breaks only the current integration.

Sometime, while executing a loop It becomes necessary to skip

a part of the loop or to leave the loop as soon as certain

condition becomes true, that is jump out of loop.

Break-continue statement example 37

Trang 20

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

Tasks

39

Task 1: Two numbers are

entered through the keyboard

Write a program to find the

value of one number raised to

the power of another.

Task 2: Write a program to sum

of digits of given integer number.

Tasks

Task 3: Write a program to check

given number is prime or not.

Task 4: Compute the natural logarithm of 2 by adding up to n terms in the series:

1-1/2+1/3-1/4+1/5-…1/n Where n is a positive integer and input by user

39

Trang 21

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

Solutions – Task 1

41

Solutions – Task 2 41

Trang 22

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

Solutions – Task 3

43

Solutions – Task 4 43

Trang 23

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

Trang 24

TALENTS FOR DEVELOPMENT - Petrovietnam University

Foundations of Engineering II- PVUTrần Ngọc Hiệp, MSc

Discussion

47

47

Ngày đăng: 17/06/2024, 15:00