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

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.vnMobile: 0901 25 2468

Selection StatementsFor Loops

While LoopsDo-While LoopsBreaking out of Loops1

Trang 2

TALENTS FOR DEVELOPMENT - Petrovietnam University

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

Conditions Statements

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

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.

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)

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 fail”.

Problem 2: Ask use to enter two numbers Display which number is greater between two.

9

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)

If codeElse 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

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

6.6 While loopExample:

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

6.6 While loop - Your task

Task 1: Write a program to sum of digits of given integer number.

Task 2: Write a

program to calculate the sum of first 10 natural number.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

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

6.8 For loop - Solution25

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.

6.9 Nested LoopsNested while loopsyntax:

33

Trang 18

TALENTS FOR DEVELOPMENT - Petrovietnam University

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

6.9 Nested LoopsNested for loopsyntax:

6.9 Nested LoopsNested while, for loopsyntax:

35

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

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 example37

Trang 20

TALENTS FOR DEVELOPMENT - Petrovietnam University

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

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.

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

Solutions – Task 241

Trang 22

TALENTS FOR DEVELOPMENT - Petrovietnam University

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

Solutions – Task 3

Solutions – Task 443

Trang 23

TALENTS FOR DEVELOPMENT - Petrovietnam University

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

Summary

1• If else statement2• While loop

4• For loop

5• Break and continue

45

Trang 24

TALENTS FOR DEVELOPMENT - Petrovietnam University

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

47

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