60-140 lecture 2b dr. robert d. kent. structured program development program control

32
60-140 Lecture 2b Dr. Robert D. Kent

Upload: martha-george

Post on 17-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

60-140 Lecture 2bDr. Robert D. Kent

Page 2: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Structured program development Program control

Page 3: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control
Page 4: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Structured program development◦ Sequential nature of instruction logic◦ Non-sequential instruction logic◦ Control structures

Repetition Decision Selection

Page 5: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Previously, we discussed program design from the perspectives of ◦ Top-Down Bottom-Up Stepwise-

Refinement

In practice, all of these techniques are used◦ However, the Top-Down approach emphasizes the

recognition of processing modules, or units of logic, that must always be processed as complete units Sometimes these units may be single statements Usually they are expressed as compound statements { }

◦ The goal of structured programming is to apply formal language control structures that determine the execution of processing units.

Page 6: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

At the level of the machine (CPU hardware)◦ The RAM address of the next instruction to be

executed is stored in a CPU register (storage unit) called the Program Counter (PC)

◦ Machine language instructions (encoded bit strings) are loaded from RAM, using the address stored in the PC, to an Instruction Register (IR) in the CPU

◦ While the IR is being decoded, the PC is incremented by the length (in bytes) of the current instruction. This last step assumes that the next instruction (in

sequence) automatically follows the current instruction in RAM.

The DEFAULT mode of process execution is SEQUENTIAL.

Statement_1 ;Statement_2 ;

.....Statement_N-1 ;Statement_N ;

Page 7: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Hardware designers have long understood the need to perform logical steps “out of order”◦ This is called non-sequential logic◦ Must be able to change the value stored in the

CPU Program Counter (PC) register Requires hardware instructions for this purpose This is called branching logic

Page 8: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Forward Branch◦ if

process

condFALSE

TRUE TO DOor

NOT TO DO ?

That is the question!

Page 9: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Forward Branch◦ if else

cond

Tprocess

FALSE

TRUE

Fprocess

EITHER

OR

Page 10: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Backward Branch (Code repetition)◦ Pre-Form: while / for

cond

process

FALSE

TRUEDoONCE

or REPEATEDLY

Page 11: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Backward Branch (Code repetition)◦ Post-Form: do while

cond

process

FALSE

TRUE

Page 12: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

In the C language, several formal control structures have been designed◦ Repetition

while do-while for◦ Decision

if if-else ◦ Selection

if-else if-else switch

◦ Each of these structures guide how the compiler generates proper machine language codes that preserve the semantics of the control logic

Page 13: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Multiple selection and For control structures

Page 14: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Program control◦ Multiple selection

If-else if-else switch

◦ Repetition do-while for

◦ Break and Continue

Page 15: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Multiple selection logic arises when a choice between more than two possible outcomes may happen

C provides two control structures to deal with these situations◦ if-else if-else◦ switch

Page 16: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Problem:◦ Part of a calculator program requires the user to

input a value from 1 to 4 indicating his/her choice of the operation to perform on two values A and B (assume A, B already entered) RESULT: C = A operation B ;

◦ The interpretation of the inputs is defined as 1 - Add 2 - Subtract 3 - Multiply 4 - Divide

Page 17: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Solution using if-else if-else :

◦ printf ( “Enter operation code >” ) ; scanf ( “%d”, &Code ) ; if ( Code == 1 ) C = A + B ; else if ( Code == 2 ) C = A – B ; else if ( Code == 3 ) C = A * B ; else C = A / B ;

A bit difficult to understand.

Page 18: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Solution using if-else if-else :

◦ printf ( “Enter operation code >” ) ; scanf ( “%d”, &Code ) ; if ( Code == 1 ) C = A + B ; else if ( Code == 2 ) C = A – B ; else if ( Code == 3 ) C = A * B ; else C = A / B ;

Much easier.

REMEMBER !Indentation is only for

programmers, compilers do not

understand indents.

Page 19: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Solution using switch :◦ printf ( “Enter operation code >” ) ;

scanf ( “%d”, &Code ) ; switch ( Code ) { case 1 : C = A + B ; break ; case 2 : C = A – B ; break ; case 3 : C = A * B ; break ; case 4 : C = A / B ; break ; default : printf ( “Error in input\n” ) ; break ; }

Page 20: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Solution using switch :◦ printf ( “Enter operation code >” ) ;

scanf ( “%d”, &Code ) ; switch ( Code ) { case 1 : C = A + B ; break ; case 2 : C = A – B ; break ; case 3 : C = A * B ; break ; case 4 : C = A / B ; break ; default : printf ( “Error in input\n” ) ; break ; }

Page 21: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Repetition logic may be of two forms◦ Pre-condition testing : enter, or re-enter, the loop

body if the condition is true.◦ Post-condition testing : enter the loop body in all

cases (performing the body a minimum of once), then repeat the loop body only if the condition is true.

C supports three forms of repetition control structures ◦ while◦ do-while ◦ for

Page 22: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

while ( condition_expression ) statement ;

while ( condition_expression ) { statement1 ; ...... statementN ; }

Page 23: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

do statement ; while ( condition_expression ) ;

do { statement1 ; ...... statementN ; } while ( condition_expression ) ;

Page 24: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

for ( init_stmt ; cond_expr ; update_stmt ) statement ;

for ( init_stmt ; cond_expr ; update_stmt ) { statement1 ; ...... statementN ; }

Page 25: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Example: Find the sum of all integers from 1 to 10.

int Sum = 0, k ;

for ( k = 1 ; k <= 10 ; k++ ) Sum = Sum + k ;

Page 26: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Example: Find the sum of all integers from 1 to 10.

int Sum, k ;

for ( k = 1, Sum = 0 ; k <= 10 ; k++ ) Sum = Sum + k ;

Page 27: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

C defines two instruction statements that cause immediate, non-sequential alteration of normal sequential instruction processing

Break Logic◦ Execution of a break ; statement at any location in a loop-

structure causes immediate exit from the loop-structure

Continue Logic◦ Execution of a continue ; statement at any location in a

loop-structure causes execution to continue at the beginning of the loop structure (at the next loop iteration) while skipping the remaining statements.

Page 28: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Break Logic◦ Execution of a break ; statement at any location in a

loop-structure causes immediate exit from the loop-structure

for ( k = 0 ; k < 10 ; k++ ) { if ( k == 5 ) break ; printf ( “%d, ”, k ) ; }

Produces output : 0, 1, 2, 3, 4

Page 29: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Continue Logic◦ Execution of a continue ; statement at any location in a

loop-structure causes execution to continue at the beginning of the loop structure (at the next loop iteration) while skipping the remaining statements.

for ( k = 0 ; k < 5 ; k++ ) { if ( k == 3 ) continue ; printf ( “%d, ”, k ) ; }

Produces output : 0, 1, 2, 4

Page 30: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control
Page 31: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Data◦ Types, Declarations, Literal values◦ Input/Output specification codes

Operators Structured program development

◦ Selection / Decision control logic If If – else Switch

Control structures◦ Repetition (Loop) control logic

Counters Sentinels◦ While Do-While◦ For

Page 32: 60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control

Reading◦ Chapters 1 – 4 (All sections)

◦ Midterm Examination 1 will focus on concepts and techniques from Chapters 1-3, completely Chapter 4.1 – 4.4 , 4.7, 4.8

Some material presented in Lecture 2 slides may not be tested at this time, but will be tested later Students are responsible for all material presented in the

Lecture slides, as well as all assigned reading and Laboratory exercises and Examinations.