1. Trang chủ
  2. » Công Nghệ Thông Tin

Programming Concepts (Part A) ENGR 10 Introduction to Engineering pot

33 697 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 đề Programming Concepts (Part A)
Trường học University of Engineering and Technology
Chuyên ngành Introduction to Engineering
Thể loại lecture notes
Năm xuất bản 2023
Thành phố Sample City
Định dạng
Số trang 33
Dung lượng 592 KB

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

Nội dung

A computer program is an ordered list of instructions.. Consider the sequential execution of the above expressions to find the value of y:... An Empty EasyC programC programFlow chart Y

Trang 1

Programming Concepts

(Part A)

ENGR 10 Introduction to Engineering

Trang 2

What is a program?

WHITE CAKE RECIPE

1 Preheat oven to 350 degrees F (175 degrees C)

2 Grease and flour a 9x9 inch pan.

3. In a medium bowl, cream together the sugar and butter.

4 Beat in the eggs.

5 Stir in the vanilla

6 Combine flour and baking powder, add to the creamed mixture and mix well

7 Stir in the milk until batter is smooth

8 Pour or spoon batter into the prepared pan

9 Bake for 30 to 40 minutes in the preheated oven

A computer program is an ordered

list of instructions.

It’s like a recipe!

In Summary…

Trang 3

(3a 2 + 21) / b + 4

Sequential Solving

following Math conventions for correct answer.

Trang 4

Consider the sequential execution of the above expressions to find the value of y:

Trang 5

An Empty EasyC program

C programFlow chart

Your program goes

Trang 6

C program

Logic flow

An instruction

in C

Trang 7

• A “variable” is a place where we keep a value.

A = 10 ; // The value 10 is stored in A.

• Any statement after the sign “//” is NOT part of the program It is a comment made by the programmer.

WSalary = 800 ; // Any statement can go here.

• A semicolon is required at the end of each C

instruction

Trang 8

Variables, cont.

Every variable in the program needs to be

declared in the beginning of the program

Trang 9

Commonly Used Variable Types

Variable

extended range EX: 56, 6000, -4,234,128

-2,147,483,648 to +2,147,483,647

float Stores values with decimal

point EX: 1.245, -4.2341

[-10^+38, -10^-38] 0

[10^-38, 10^+38]

Trang 10

• Global Variable:

This variable is accessible from

anywhere within your program

• Local Variable:

This variable is only accessible from a

“local area” within your program (“functions” will be discussed later)

Trang 11

Global Variable Declaration

To declare a global variable, click on the tab

“Global” in the Main program

Trang 12

Global Variable Declaration

(A close-up view)

Trang 13

Assignment Operator

• In C language “ = ” is an assignment operator, not an “ is equal to ” statement.

• A = 10; // means assign 10 to A.

• A = B; // means assign the value of B to A

• A = A+1; //means assign the value A+1

// back to A, i.e., increase A by 1.

Trang 14

Assignment Operator: Examples:

int A; // a variable named A of type integer

int B; // a variable named B of type integer

A = 10; // value 10 is assigned to variable A

Trang 15

Q1: What is the value of B at the

end of this program?

Trang 16

Decision Making

• The ability to make decision is the most basic form of

intelligence.

• A linear (sequential) execution of instructions can only

perform a simple task that does not involve decision

making.

• The IF instruction gives a C program decision making ability

Trang 17

IF Statement: Simple Example

This == symbol is used for

checking ‘equal’ condition,

not for value assignment.

Only if this condition is true, this instruction is executed.

Trang 18

IF-ELSE StatementConsider the following program:

If (score <60)

{ PrintToScreen(“You failed the test”);}

If (score == 60) // ‘==‘ means ‘if equal to’{ PrintToScreen(“You passed the test”);}

If (score > 60)

{ PrintToScreen(“You passed the test”);}

Trang 20

Q2: What is the value of A and B

at end of this program?

Trang 21

WHILE Statement

• In C, the WHILE statement is useful for

repeating a set of instructions

Suppose we have to add the first 50 positive integers

1+2+3+4+………+48+49+50

Trang 22

First Approach:

Use a single statement:

int SUM ; // integer variable for the resultSUM = 1+2+3+……… +48+49+50;

Trang 23

Second Approach:

int SUM ; //variable SUM is declared as integer SUM = 0 ; //SUM is assigned the value 0

SUM = SUM + 1 ; // Now SUM = (0 + 1) = 1

SUM = SUM + 2 ; // SUM = (1 + 2) = 3

SUM = SUM + 3 ; // SUM = (3 + 3) = 6

SUM = SUM + 49 ; //SUM = (1176 + 49) = 1225 SUM = SUM + 50; //SUM = (1225 + 50) = 1275

Trang 24

Third approach:

Use “while” statement

This condition is checked first If it is true, the block

Trang 25

While Statement - Example

• To find factorial of a number N

Trang 27

Q3: What is the final value of A

Trang 28

Fourth iteration : condition 3<3 is false

So the WHILE loop stops.

Final Value of A is 3.

Trang 30

Infinite Loop

• In the previous examples we have employed

condition checking in order to control the flow of

• The status of a bumper switch is continuously

monitored and depending on its value the

movement of the robot is decided.

Trang 31

while (1==1)

{ status = GetDigitalInput(5); // read bumper

if (status ==1 ) // bumper hits something

{ SetMotor(2,127);} // Stop the robot

else

{ SetMotor(2,0); // Move forward

} }

Infinite Loop

The status of a bumper switch (via Digital input port #5) is repeatedly read needlessly When a hit (status==1) is detected, the robot is stopped

Trang 32

status = GetDigitalInput(5); // read bumper

while (1==1)

{

if (status ==1 ) // bumper hits something

{ SetMotor(2,127);} // Stop the robot

else

{ SetMotor(2,0); // Move forward

} }

Infinite Loop

A common mistake – reading sensor OUTSIDE of the loop

Trang 33

IMPORTANT NOTE

Review this example carefully

Ngày đăng: 05/03/2014, 18:20

TỪ KHÓA LIÊN QUAN