C语言学习第二章(英文版)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

School of Software, Nanchang HangKong University
5
Chapter 2 Algorithm and Flowchart
2.1 What is algorithm
A step-by-step problem-solving
procedure, especially an established,
If student’s grade is greater than or equal to 60
Print “Passed”
If condition true
Print statement executed and program goes on to next statement If false, print statement is ignored and the program goes onto the next statement Indenting makes programs easier to read C ignores whitespace characters
School of Software, Nanchang HangKong University
12
Chapter 2 Algorithm and Flowchart
2.5 Control Structures
Sequential execution Transfer of control
School of Software, Nanchang HangKong University
2
Chapter 2 Algorithm and Flowchart
Outline
2.1 What is algorithm
2.2 Features of algorithms
2.3 Pseudocode
School of Software, Nanchang HangKong University
16
Chapter 2 Algorithm and Flowchart
2.6 Selection Structure (if)
if structure is a single-entry/single-exit structure
Easy to convert into a corresponding C program Consists only of executable statements
School of Software, Nanchang HangKong University
8
Chapter 2 Algorithm and Flowchart
Key points
What is algorithm
Features of algorithms
Flowchart
School of Software, Nanchang HangKong University
4
Chapter 2 Algorithm and Flowchart
2.1 What is algorithm
printf( "Passed\n" );
C code corresponds closely to the pseudocode
ቤተ መጻሕፍቲ ባይዱ
Diamond symbol (decision symbol)
Indicates decision is to be made Contains an expression that can be true or false Test the condition, follow appropriate path
7
School of Software, Nanchang HangKong University
Chapter 2 Algorithm and Flowchart
2.3 Pseudocode
Pseudocode
Artificial, informal language that helps us develop algorithms Similar to everyday English Not actually executed on computers Helps us “think out” a program before writing it
Statements executed one after the other in the order written When the next statement executed is not the next one in sequence Overuse of goto statements led to many problems
School of Software, Nanchang HangKong University
15
Chapter 2 Algorithm and Flowchart
2.6 Selection Structure (if)
Pseudocode statement in C:
if ( grade >= 60 )
2.3 Pseudocode
Example of pseudocode
If student’s grade is greater than or equal to 60 Print “Passed” If student’s grade is greater than or equal to 60 Print “Passed” else Print “Failed”
Single-entry/single-exit control structures
10
Chapter 2 Algorithm and Flowchart
School of Software, Nanchang HangKong University
11
Chapter 2 Algorithm and Flowchart
Connect exit point of one control structure to entry point of the next (control-structure stacking) Makes programs easy to build
School of Software, Nanchang HangKong University
School of Software, Nanchang HangKong University
9
Chapter 2 Algorithm and Flowchart
2.4 Flowchart
Flowchart
Graphical representation of an algorithm Drawn using certain special-purpose symbols connected by arrows called flowlines Rectangle symbol (action symbol): Indicates any type of action Oval symbol: Indicates the beginning or end of a program or a section of code
Before writing a program:
Have a thorough understanding of the problem Carefully plan an approach for solving it
While writing a program:
Know what ”building blocks” are available Use good programming principles
2.4 Flowchart
2.5 Control Structures
2.6 Selection Structure
2.7 Repetition Structure
School of Software, Nanchang HangKong University
3
Chapter 2 Algorithm and Flowchart
Chapter 2 Algorithm and Flowchart
The number of the group:77162023
School of Software, Nanchang HangKong University
1
Chapter 2 Algorithm and Flowchart
Chapter 2 Algorithm and Flowchart
2.2 Features of algorithm
Determinism
An algorithm is deterministic, if at any stage of the algorithm execution, the next action step is clearly defined.
Finiteness
The finiteness restricts the definition of the algorithm to a finite long-term operation.
Effectiveness
The effect of each statement of an algorithm must be clearly defined.
recursive computational procedure for
solving a problem in a finite number of
steps.
School of Software, Nanchang HangKong University
6
Chapter 2 Algorithm and Flowchart
School of Software, Nanchang HangKong University
14
Chapter 2 Algorithm and Flowchart
2.6 Selection Structure (if)
Selection structure:
Used to choose among alternative courses of action Pseudocode:
School of Software, Nanchang HangKong University
13
Chapter 2 Algorithm and Flowchart
2.5 Control Structures
Bohm and Jacopini
All programs written in terms of 3 control structures Sequence structures: Built into C. Programs executed sequentially by default Selection structures: C has three types: if, if/else, and switch(book chapter 4) Repetition structures: C has three types: while, do/while and for Single-entry/single-exit control structures Connect exit point of one control structure to entry point of the next (control-structure stacking) Makes programs easy to build
A decision can be made on any expression. zero - no
grade >= 60 yes print “Passed”
相关文档
最新文档