服务承诺
资金托管
原创保证
实力保障
24小时客服
使命必达
51Due提供Essay,Paper,Report,Assignment等学科作业的代写与辅导,同时涵盖Personal Statement,转学申请等留学文书代写。
51Due将让你达成学业目标
51Due将让你达成学业目标
51Due将让你达成学业目标
51Due将让你达成学业目标私人订制你的未来职场 世界名企,高端行业岗位等 在新的起点上实现更高水平的发展
积累工作经验
多元化文化交流
专业实操技能
建立人际资源圈Selection_and_Repetition_Control_Structures
2013-11-13 来源: 类别: 更多范文
Selection and Repetition Control Structures
Selection Control Structures
When a programmer is writing a program that needs to choose between two or more actions dependent upon whether the condition is true or false, a selection control structure is used. Relational operators such as less than (<), greater than (>) and equal to (< >, =) are used to express the conditions. The addition of selection control structures to a program gives a program more structure and the programmer has more flexibility in program construction.
There are four kinds of selection control structures:
1. Simple selection
2. Simple selection with null force branch
3. Combined selection
4. Nested selection
Suppose a programmer wants to process the results of a survey on how many people in San Diego, CA own dogs, how many own cats, how many own other types of pets and how many have no pets at all. The programmer would most likely use a nested control selection which is used when the word IF occurs more than once in an IF statement. When either the true or the false branch of one IF has another IF statement imbedded within it, it is called a nested selection. The important keywords that used in this type of selection are IF, THEN, ELSEIF, and ENDIF. The following example will illustrate the use of the nested selection:
IF No = “N”
THEN
Increase No Pet-Count by one
IF Yes = “Y”
IF Pet = “D” THEN
Increase Dog-Count by one
ELSE
IF Pet = “C” THEN
Increase Cat-Count by one
ELSE
IF Pet = “E” THEN
Increase Exotic-Count by one
ENDIF
ENDIF
ENDIF
ENDIF
Repetition Control Structures
Repetition control structures are blocks of one or more statements that continually repeat until a specific condition is satisfied. They are also known as iteration control structures.
There are three repetition control structures and where the decision to repeat a statement is placed determines which one of the control structures is used:
1. Leading decision loop or DOWHILE loop is used at the beginning of the loop.
2. Trailing decision loop or REPEAT…UNTIL loop is used at the end of the loop.
3. Counted repetition loops, also called DO or FOR loops are used when the number of loop iterations to repeat is known in advance.
The following is an example of a counted repetition loop to convert miles per hour to kilometers per hour. The following conversion is based on Robertson (n.d.):
Miles-per-Hour_Kilometers-per-Hour_Conversion
1. DO mph_count = 5 to 13
2. prompt operator for mph
3. Get mph
4. compute kph = (mph * 1.6)
5. Display kph
ENDDO
6. Display ‘All speeds processed’ to the screen
The DO loop controls the repetition by:
* initializing the mph to 5
* increments the mph by 5 for each pass through the loop
* tests mph at the beginning of each pass to ensure it is within the 5 to 13 range
* automatically terminates the loop once the mph exceeds 13
References
Robertson, L. A. (n.d.). Simple program design: A step by step approach. [University of Phoenix
Custom Edition e-Text]. Mason, Ohio: Cengage Learning. Retrieved from Axia College, PRG210 - Fundamentals of Programming website.

