• No results found

Programming Logic and Design Eighth Edi(on

N/A
N/A
Protected

Academic year: 2021

Share "Programming Logic and Design Eighth Edi(on"

Copied!
33
0
0

Loading.... (view fulltext now)

Full text

(1)

Programming  Logic  and  Design  

Eighth  Edi(on

 

Chapter  3  

(2)

Objec3ves  

In  this  chapter,  you  will  learn  about:  

• 

The  disadvantages  of  unstructured  spagheA  code  

• 

The  three  basic  structures—sequence,  selec3on,  and  

loop  

• 

Using  a  priming  input  to  structure  a  program  

• 

The  need  for  structure  

• 

Recognizing  structure  

(3)

The  Disadvantages  of  Unstructured    

SpagheA  Code  

• 

Spaghe'  code  

–  Logically  snarled  program  statements   –  OKen  a  complicated  mess  

–  Programs  oKen  work  but  are  difficult  to  read  and  maintain   –  Confusing  and  prone  to  error  

• 

Unstructured  programs  

–  Do  not  follow  the  rules  of  structured  logic  

• 

Structured  programs    

(4)
(5)

Understanding  the  Three  Basic  

Structures  

• 

Structure  

–  Basic  unit  of  programming  logic    

–  Sequence  structure  

•  Perform  ac3ons  or  tasks  in  order   •  No  branching  or  skipping  any  task  

–  Selec:on  structure  (decision  structure)  

•  Ask  a  ques3on,  take  one  of  two  ac3ons   •  OKen  called  if-­‐then-­‐else  

•  Dual-­‐alterna:ve  ifs  or  single-­‐alterna:ve  ifs  

–  Loop  structure  

(6)

Understanding  the  Three  Basic  

Structures  

(con3nued)  

(7)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

(8)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

• 

Dual-­‐alterna:ve  ifs  

–  Contains  two  alterna3ves  

–  The  if-then-else  structure  

if someCondition is true then

do oneProcess

else

do theOtherProcess

(9)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

• 

Single-­‐alterna:ve  ifs  

–  An  else  clause  is  not  required  

• 

null  case  

–  Situa3on  where  nothing  is  done  

if employee belongs to dentalPlan then

(10)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

(11)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

• 

Loop  structure  

–  Repeats  a  set  of  ac3ons  while  a  condi3on  remains  true  

•  Loop  body  

–  Also  called  repe::on  or  itera:on  

–  Condi3on  is  tested  first  in  the  most  common  form  of  loop   –  The  while…do  or  while  loop  

(12)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

(13)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

• 

Loop  structure  

while testCondition continues to be true

do someProcess

endwhile

while you continue to be hungry

take another bite of food

determine if you still feel hungry

(14)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

• 

All  logic  problems  can  be  solved  using  only  sequence,  

selec3on,  and  loop  

• 

Structures  can  be  combined  in  an  infinite  number  of  

ways  

• 

Stacking  structures  

–  A_aching  structures  end-­‐to-­‐end  

• 

End-­‐structure  statement  

–  Indicates  the  end  of  a  structure  

–  The  endif  statement  ends  an  if-then-else  structure   –  The  endwhile  statement  ends  a  loop  structure  

(15)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

(16)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

• 

Any  individual  task  or  step  in  a  structure  can  be  

replaced  by  a  structure  

• 

Nes:ng  structures  

–  Placing  one  structure  within  another  

–  Indent  the  nested  structure’s  statements  

• 

Block  

(17)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

Figure  3-­‐7  Flowchart  and  pseudocode  showing  nested  structures—  

(18)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

Figure  3-­‐8  Flowchart  and  pseudocode  showing  nested  structures—  

(19)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

Figure  3-­‐9  Flowchart  and  pseudocode  for  a  selec3on  within    

(20)

Understanding  the  Three  Basic  

Structures  

(con3nued)

 

• 

Structured  programs  have  the  following  

characteris3cs:  

–  Include  only  combina3ons  of  the  three  basic  structures   –  Each  structure  has  a  single  entry  point  and  a  single  exit  

point  

–  Structures  can  be  stacked  or  connected  to  one  another  only   at  their  entry  or  exit  points  

(21)

Using  a  Priming  Input  to  Structure  

a  Program  

• 

Priming  input  (or  priming  read)  

–  Reads  the  first  input  data  record  

–  Is  outside  the  loop  that  reads  the  rest  of  the  records   –  Helps  keep  the  program  structured  

• 

Analyze  a  flowchart  for  structure  one  step  at  a  3me  

• 

Watch  for  unstructured  loops  that  do  not  follow  this  

order  

–  First  ask  a  ques3on  

–  Take  ac3on  based  on  the  answer   –  Return  to  ask  the  ques3on  again  

(22)

Using  a  Priming  Input  to  Structure  

a  Program  

(con3nued)  

(23)

Using  a  Priming  Input  to  Structure  

a  Program  

(con3nued)  

(24)
(25)
(26)

Understanding  the  Reasons  for  

Structure  

• 

Clarity—unstructured  programs  are  confusing  

• 

Professionalism—other  programmers  expect  it  

• 

Efficiency—most  languages  support  it  

• 

Maintenance  —other  programmers  find  it  easier  to  

read  

(27)

Figure  3-­‐18  Example  1  

Structured  Flowcharts?  

Recognizing  Structure  

(28)

Recognizing    

Structure  

(con3nued)  

(29)

Figure  3-­‐21  Steps  to  structure  the  dog-­‐

washing  process  

Recognizing    

Structure  

(con3nued)  

(30)

Figure  3-­‐22  Structured  dog-­‐washing  

flowchart  and  pseudocode  

Recognizing    

Structure  

(con3nued)  

(31)

Figure  3-­‐23  Modularized  version  of  the  

dog-­‐washing  program  

Recognizing    

Structure  

(con3nued)  

(32)

Summary  

• 

SpagheA  code  

–  Statements  that  do  not  follow  rules  of  structured  logic  

• 

Three  basic  structures  

–  Sequence,  selec3on,  and  loop  

–  Combined  by  stacking  and  nes3ng  

• 

Priming  input  

–  Statement  that  reads  the  first  input  value  prior  to  star3ng   a  structured  loop  

(33)

Summary  

(con3nued)  

• 

Structured  techniques  promote:    

–  Clarity  

–  Professionalism   –  Efficiency  

–  Modularity  

• 

Flowcharts  can  be  made  structured  by  untangling  

• 

Logical  steps  can  be  rewri_en  to  conform  to  the  

References

Related documents

1 shallot, halved 1 thyme sprig 1 bay leaf 16 t0 18 cooked baby artichokes 2 cups olive oil 1/2 teaspoon kosher salt Parsley Beurre Fondue 2 cups fresh flat-leaf parsley leaves from

Between the 40th and the 110th days after calving the concentration of calcium and potassium, that were chosen to measure nutrition status, the

– CSE 12: Basic Data Structures and Object-Oriented Design using C++ and/or Java... Introduction to

Arguing that reader comments reflect the othering dis - course typical of populist right-wing groups (Wodak, 2015; Wodak, KhosraviNik and Mral, 2013), we combine Conversation

In this paper, we compare this method to SVMRFE, LOOCSFS, GLGS, SFS-LSbound, SFFS- LSbound and T-test by using six benchmark microarray data sets; meanwhile, we propose an

Residual activity of ASNase with polymers and PEG-ASNase after heat treatment at

We then construct an aggregate measure of the stock of human capital of Austria and Germany by weighting workers of different schooling levels with their respective wage income..

The aedeagi of these three species possess a deeply furcate main piece with a single dorsal seta inserted on dorsal branch near base of distal lobe, and with a single elongate