structured programming constructs march, 2011copyright 2011 - yvette francis
TRANSCRIPT
Copyright 2011 - Yvette Francis
Algorithm WritingStructured Programming
Constructs
March, 2011
Copyright 2011 - Yvette Francis
Structured Programming Constructs
Sequence
Selection
Repetition
March, 2011
Copyright 2011 - Yvette Francis
Sequence Places steps in correct logical order
What’s wrong here?1. Place letter in envelope2. Drop letter in mailbox3. Seal envelope4. Fold letter to fit envelope
1. Fold letter to fit envelope2. Place letter in envelope3. Seal envelope4. Drop letter in mailbox
March, 2011
Copyright 2011 - Yvette Francis
Selection Expresses choice of action based on
a condition• Example:
Persons under 18 years are not allowed to vote Only drivers 25 years and over receive a
discount of 10%
March, 2011
Copyright 2011 - Yvette Francis
Repetition Allows one or more steps to be
repeated based on a condition or for a specified number of times• Example:
Each of the 5 numbers received must be doubled
Calculate the VAT inclusive price for each of 10 items
March, 2011
Copyright 2011 - Yvette Francis
Selection in PseudocodeIF-THEN Statement
IF condition THENaction
ENDIF• Example:
If age < 18 then Print “Voting not allowed”Endif
• Example:
If DriverAge >= 25 then Premium = Premium * .90Endif
March, 2011
Copyright 2011 - Yvette Francis
Selection in PseudocodeIF-THEN-ELSE Statement
IF condition THENaction1
ELSEaction2
ENDIF• Example:
If age < 18 then Print “Voting not allowed”Else Print “You may vote”Endif
• Example:
If DriverAge >= 25 then Premium = Premium * .90Else Premium = Premium * 1.1Endif
March, 2011
Copyright 2011 - Yvette Francis
Repetition in PseudocodeFOR Loop
FOR loop-index = start-value TO end-value DOstatements….
ENDFOR
• Example:
FOR rep = 1 TO 5 DO Read num num = num * 2
ENDFOR
• Example:
VATRate = .15FOR count = 1 TO 10 DO
Read price FinalPrice = price + price * VATRate
ENDFOR
March, 2011
Copyright 2011 - Yvette Francis
Repetition in PseudocodeWHILE Loop
WHILE condition DOstatements….
ENDWHILE
• Example:
num = 1WHILE num <= 12 DO
answer = num * 2 num = num + 1 Print answer
ENDWHILE
• Example:
VATRate = .15Read priceWHILE price <> 0 DO
FinalPrice = price + price * VATRate Read price
ENDWHILE
March, 2011
Copyright 2011 - Yvette Francis
Repetition in PseudocodeREPEAT Loop
REPEAT statements….
UNTIL condition
• Example:
num = 1REPEAT
answer = num * 2 num = num + 1 Print answer
UNTIL num = 13
• Example:
VATRate = .15Read priceREPEAT
FinalPrice = price + price * VATRate Read price
UNTIL price = 0
March, 2011
Copyright 2011 - Yvette Francis
Repetition in PseudocodeComparing WHILE with REPEAT
REPEAT
statements….UNTIL condition
test-after loopperformed at least once
WHILE condition DO
statements….ENDWHILE
test-before loopmay not be performed even once
March, 2011
Copyright 2011 - Yvette Francis
Repetition in PseudocodeComparing WHILE with REPEAT
REPEAT
• Example:
num = 1REPEAT answer = num * 2 num = num + 1 Print answerUNTIL num = 13
• Example:
VATRate = .15Read priceREPEAT FinalPrice = price + price * VATRateRead priceUNTIL price = 0
WHILE
• Example:
num = 1WHILE num <= 12 DO
answer = num * 2 num = num + 1 Print answer
ENDWHILE
• Example:
VATRate = .15Read priceWHILE price <> 0 DO
FinalPrice = price + price * VATRate Read price
ENDWHILE
March, 2011