330 likes | 435 Views
Structured Program Development. Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall. Outline. Introduction The selection statement if if….else switch The repetition statement while do…while
E N D
Structured Program Development Angela Chih-Wei Tang (唐 之 瑋) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall
Outline • Introduction • The selection statement • if • if….else • switch • The repetition statement • while • do…while • Arithmetic operators Angela Chih-Wei Tang, 2010
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 Angela Chih-Wei Tang, 2010
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 Angela Chih-Wei Tang, 2010
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 Angela Chih-Wei Tang, 2010
Control Structures – Sequential Execution Angela Chih-Wei Tang, 2010
Outline • Introduction • The selection statement • if • if….else • switch • The repetition statement • while • do...while • Arithmetic operators Angela Chih-Wei Tang, 2010
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 60Print “Passed” • If condition • True • print statement executed • program goes on to next statement • False • print statement is ignored • program goes onto the next statement Angela Chih-Wei Tang, 2010
true false print “Passed” grade >= 60 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 Angela Chih-Wei Tang, 2010
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 60Print “Passed” elsePrint “Failed” Angela Chih-Wei Tang, 2010
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” ); Angela Chih-Wei Tang, 2010
false true print “Passed” print “Failed” grade >= 60 The if…else Selection Statement (3/3) Angela Chih-Wei Tang, 2010
Nestedif…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”elseIf student’s grade is greater than or equal to 80 Print “B” elseIf 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” Angela Chih-Wei Tang, 2010
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? Angela Chih-Wei Tang, 2010
Outline • Introduction • The selection statement • if • if….else • switch • The repetition statement • while • do...while • Arithmetic operators Angela Chih-Wei Tang, 2010
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 Angela Chih-Wei Tang, 2010
true product <= 1000 product = 2 * product false The while Repetition Statement (2/2) • Example: int product = 2; while ( product <= 1000 ) product = 2 * product; Angela Chih-Wei Tang, 2010
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 zeroSet grade counter to one While grade counter is less than or equal to tenInput the next gradeAdd the grade into the totalAdd one to the grade counter Set the class average to the total divided by tenPrint the class average Angela Chih-Wei Tang, 2010
19 Angela Chih-Wei Tang, 2010
Enter grade: 98Enter grade: 76Enter grade: 71Enter grade: 87Enter grade: 83Enter grade: 90Enter grade: 57Enter grade: 79Enter grade: 82Enter grade: 94Class average is 81 Test 20 Angela Chih-Wei Tang, 2010
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 Angela Chih-Wei Tang, 2010
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” 22 Angela Chih-Wei Tang, 2010
23 Angela Chih-Wei Tang, 2010
24 Angela Chih-Wei Tang, 2010
Class Average Program – Test Results Test 1 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 Test 2 Enter grade, -1 to end: -1No grades were entered 25 Angela Chih-Wei Tang, 2010
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 Angela Chih-Wei Tang, 2010
true false action(s) condition The do…while Repetition Statement (2/2) Angela Chih-Wei Tang, 2010
Test 1 2 3 4 5 6 7 8 9 10 Angela Chih-Wei Tang, 2010
Outline • Introduction • The selection statement • if • if….else • switch • The repetition statement • while • do...while • Arithmetic operators Angela Chih-Wei Tang, 2010
Arithmetic Assignment Operators Angela Chih-Wei Tang, 2010
Increment and Decrement Operators (1/2) Angela Chih-Wei Tang, 2010
Increment and Decrement Operators (2/2) Angela Chih-Wei Tang, 2010
33 Angela Chih-Wei Tang, 2010