14
Structured Programming The Basics

Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

Embed Size (px)

DESCRIPTION

Why do structured programming? It's easier to understand code written using structured programming Easier to test and debug code Easier to modify and maintain code Easier to work with other people to write large programs

Citation preview

Page 1: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

Structured Programming

The Basics

Page 2: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

Control structures They control the order of execution What order statements will be done in, or

whether they will be done at all (skipping) Different from data structures – which are

ways to access data, to operate on it

Page 3: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

Why do structured programming?

It's easier to understand code written using structured programming

Easier to test and debug codeEasier to modify and maintain codeEasier to work with other people to write

large programs

Page 4: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

4 Control Structures Sequence Selection Iteration Module

Page 5: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

Guarantees for All StructuresONE EntranceONE Exit

Page 6: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

SEQUENCE

Statement 1 Statement 2 Statement 3 . . .

Page 7: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

Guarantees for SequencesWill execute the steps in the order givenWill not enter or leave sequence in mid-

streamWill not skip steps

Page 8: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

SELECTION(branch)

IF Condition THEN Statement1 ELSE Statement2

Statements1 Statement

Statements2

Condition . . .

True

False

Page 9: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

Selection Guarantees Control always enters through the condition /

question One branch or the other is executed, never

both on one run MUST execute one branch or the other Processes in branches can be as large or

small as you want Do not write Dead Code!

Page 10: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

Dead Code

Page 11: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

LOOP(repetition)

Body

Condition . . .False

True

WHILE Condition DO Statement1

Page 12: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

Guarantees Will go through test / condition at top to get

into loop ALL of body will be executed before test is

done again Body will be repeated until test is answered

differently (NO) Do not write Infinite Loops!

Page 13: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

SUBPROGRAM(function)

SUBPROGRAM1 . . .

SUBPROGRAM1 a meaningful collection of SEQUENCES, SELECTIONS, LOOPS, SUBPROGRAM calls

Page 14: Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will

Module Flow of control