Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.

Similar presentations


Presentation on theme: "Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010."— Presentation transcript:

1 Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall

2 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 2 Outline Introduction The selection statement  if  if….else  switch The repetition statement  while  do…while Arithmetic operators

3 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 3 Pseudocode  Artificial, informal language that helps us develop algorithms  Similar to everyday English  Not actually executed on computers  Helps us “think out” a program before writing it Easy to convert into a corresponding C/C++ program Consists only of executable statements

4 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 4 Control Structures Sequential execution  Statements executed one after the other in the order written Transfer of control  When the next statement executed is not the next one in sequence  Overuse of goto statements led to many problems Bohm and Jacopini: 3 control structures  sequential execution  selection (C language) if, if … else, and switch  repetition (C language) while, do while, and for

5 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 5 Flowchart Graphical representation of an algorithm Special-purpose symbols  rectangle symbol (action symbol) action  oval symbol the beginning or end  diamond symbol  flowline : arrows for connection

6 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 6 Control Structures – Sequential Execution

7 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 7 Outline Introduction The selection statement  if  if….else  switch The repetition statement  while  do...while Arithmetic operators

8 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 8 The if Selection Statement (1/2) Selection structure  Used to choose among alternative courses of action  Pseudocode: If student’s grade is greater than or equal to 60 Print “Passed” If condition  True print statement executed program goes on to next statement  False print statement is ignored program goes onto the next statement

9 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 9 The if Selection Statement (2/2) Diamond symbol (decision symbol)  Indicates decision is to be made  Contains an expression that can be true or false  Test the condition, follow appropriate path true false grade >= 60 print “Passed”

10 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 10 The if…else Selection Statement (1/3) if  Only performs an action if the condition is true if…else  Specifies an action to be performed both when the condition is true and when it is false Psuedocode If student’s grade is greater than or equal to 60 Print “Passed” else Print “Failed”

11 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 11 The if…else Selection Statement (2/3) C code: if ( grade >= 60 ) printf( "Passed\n"); else printf( "Failed\n"); Ternary conditional operator (?:)  Takes three arguments (condition, value if true, value if false)  Our pseudocode could be written: printf( "%s\n", grade >= 60 ? "Passed" : "Failed" );  Or it could have been written: grade >= 60 ? printf( “Passed\n” ) : printf( “Failed\n” );

12 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 12 The if…else Selection Statement (3/3) truefalse print “Failed”print “Passed” grade >= 60

13 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 13 Nested if…else Selection Statement  Once condition is met, rest of statements is skipped  Pseudocode If student’s grade is greater than or equal to 90 Print “A” else If student’s grade is greater than or equal to 80 Print “B” else If student’s grade is greater than or equal to 70 Print “C” else If student’s grade is greater than or equal to 60 Print “D” else Print “F”

14 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 14 The if…else Selection Statement – Compound Statement Set of statements within a pair of braces Example: if ( grade >= 60 ) printf( "Passed.\n" ); else { printf( "Failed.\n" ); printf( "You must take this course again.\n" ); } What will be printed if “{ }” is missed?

15 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 15 Outline Introduction The selection statement  if  if….else  switch The repetition statement  while  do...while Arithmetic operators

16 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 16 The while Repetition Statement (1/2) Repetition structure  an action is repeated while some condition remains true  Psuedocode: While there are more items on my shopping list Purchase next item and cross it off my list  while loop repeated until condition becomes false

17 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 17 The while Repetition Statement (2/2) Example: int product = 2; while ( product <= 1000 ) product = 2 * product; product <= 1000 product = 2 * product true false

18 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 18 Counter-Controlled Repetition Loop repeated until counter reaches a certain value Definite repetition: number of repetitions is known Example: A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz Pseudocode: Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average

19 19C.E., NCU, Taiwan19 Angela Chih-Wei Tang, 2010

20 Enter grade: 98 Enter grade: 76 Enter grade: 71 Enter grade: 87 Enter grade: 83 Enter grade: 90 Enter grade: 57 Enter grade: 79 Enter grade: 82 Enter grade: 94 Class average is 81 Test 20C.E., NCU, Taiwan20 Angela Chih-Wei Tang, 2010

21 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 21 Formulating Algorithms with Top-Down, Stepwise Refinement (1/2) Problem becomes: Develop a class-averaging program that will process an arbitrary number of grades each time the program is run.  Unknown number of students  How will the program know to end? Three phases  Initialization: initializes the program variables  Processing: inputs data values and adjusts program variables accordingly  Termination: calculates and prints the final results

22 Formulating Algorithms with Top-Down, Stepwise Refinement (2/2) Initialize total to zero Initialize counter to zero Input the first grade While the user has not as yet entered the sentinel Add this grade into the running total Add one to the grade counter Input the next grade (possibly the sentinel) If the counter is not equal to zero Set the average to the total divided by the counter Print the average else Print “No grades were entered” 22C.E., NCU, Taiwan22 Angela Chih-Wei Tang, 2010

23 23C.E., NCU, Taiwan23 Angela Chih-Wei Tang, 2010

24 24C.E., NCU, Taiwan24 Angela Chih-Wei Tang, 2010

25 Class Average Program – Test Results Enter grade, -1 to end: 75 Enter grade, -1 to end: 94 Enter grade, -1 to end: 97 Enter grade, -1 to end: 88 Enter grade, -1 to end: 70 Enter grade, -1 to end: 64 Enter grade, -1 to end: 83 Enter grade, -1 to end: 89 Enter grade, -1 to end: -1 Class average is 82.50 Enter grade, -1 to end: -1 No grades were entered Test 1 Test 2 25C.E., NCU, Taiwan25 Angela Chih-Wei Tang, 2010

26 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 26 The do…while Repetition Statement (1/2) Similar to the while structure  condition for repetition tested after the body of the loop is performed  all actions are performed at least once Format: do { statement; } while ( condition ); Example (letting counter = 1): do { printf( "%d ", counter ); } while (++counter <= 10);  prints the integers from 1 to 10

27 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 27 The do…while Repetition Statement (2/2) true false action(s) condition

28 1 2 3 4 5 6 7 8 9 10 Test C.E., NCU, Taiwan28 Angela Chih-Wei Tang, 2010

29 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 29 Outline Introduction The selection statement  if  if….else  switch The repetition statement  while  do...while Arithmetic operators

30 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 30 Arithmetic Assignment Operators

31 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 31 Increment and Decrement Operators (1/2)

32 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2010 32 Increment and Decrement Operators (2/2)

33 33 Angela Chih-Wei Tang, 2010 C.E., NCU, Taiwan


Download ppt "Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010."

Similar presentations


Ads by Google