Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

C Programming for the Absolute Beginner phần 3 pptx
MIỄN PHÍ
Số trang
33
Kích thước
1.4 MB
Định dạng
PDF
Lượt xem
960

C Programming for the Absolute Beginner phần 3 pptx

Nội dung xem thử

Mô tả chi tiết

How the pseudo code is written is ultimately up to you, but you should always try to keep it

as language independent as possible.

Here’s another problem statement that requires the use of decision-making.

Allow a customer to deposit or withdraw money from a bank account, and if a user elects to withdraw

funds, ensure that sufficient monies exist.

Pseudo code for this problem statement might look like the following.

if action == deposit

Deposit funds into account

else

if balance < withdraw amount

Insufficient funds for transaction

else

Withdraw monies

end if

end if

The first point of interest in the preceding pseudo code is that I have a nested condition inside

a parent condition. This nested condition is said to belong to its parent condition, such that

the nested condition will never be evaluated unless one of the parent conditional require￾ments is met. In this case, the action must not equal the deposit for the nested condition to

be evaluated.

Also notice that for each algorithm implemented with pseudo code, I use a standard form of

indentation to improve the readability.

Take a look at the same pseudo code; this time without the use of indentation.

if action == deposit

Deposit funds into account

else

if balance < withdraw amount

Insufficient funds for transaction

else

Withdraw monies

end if

end if

52 C Programming for the Absolute Beginner, Second Edition

You probably already see the benefit of using indentation for readability as the preceding

pseudo code is difficult to read and follow. Without indentation in your pseudo code or actual

program code, it is extremely difficult to pinpoint nested conditions.

In the next section, you will learn how to implement the same algorithms, shown previously,

with flowcharts.

Flowcharts

Popular among computing analysts, flowcharts use graphical symbols to depict an algorithm

or program flow. In this section, I’ll use four common flowchart symbols to depict program

flow, as shown in Figure 3.1.

FIGURE 3.1

Common

flowchart

symbols.

To demonstrate flowchart techniques, take another look at the AC algorithm used in the

previous section.

if temperature >= 80

Turn AC on

else

Turn AC off

end if

This AC algorithm can also be easily represented using flowchart techniques, as shown in

Figure 3.2.

Chapter 3 • Conditions 53

FIGURE 3.2

Flowchart for the

AC algorithm.

The flowchart in Figure 3.2 uses a decision symbol to illustrate an expression. If the expression

evaluates to true, program flow moves to the right, processes a statement, and then termi￾nates. If the expression evaluates to false, program flow moves to the left, processes a

different statement, and then terminates.

As a general rule of thumb, your flowchart’s decision symbols should always move to the right

when an expression evaluates to true. However, there are times when you will not care if an

expression evaluates to false. For example, take a look at the following algorithm imple￾mented in pseudo code.

if target hit == true

Incrementing player’s score

end if

In the preceding pseudo code, I’m only concerned about incrementing the player’s score when

a target has been hit. I could demonstrate the same algorithm using a flowchart, as shown

in Figure 3.3.

You can still use flowcharts to depict more complicated decisions, such as nested conditions,

but you must pay closer attention to program flow. To demonstrate, take another look at the

pseudo code used earlier to depict a sample banking process.

54 C Programming for the Absolute Beginner, Second Edition

Tải ngay đi em, còn do dự, trời tối mất!