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

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

More on Algorithms and Problem Solving
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 Control structures  Algorithm & flowchart  If statements  While statements.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Chapter 3 - Structured Program Development
Lec3: Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Structured Program Development in C
Lecture 3 Structured Program Development in C
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 3 Structured Program Development. Objectives To understand basic problem-solving techniques. To be able to develop algorithms through the process.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
1 Lecture 3 Control Structures else/if and while.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Structured Program Development in C
Algorithm: procedure in terms of
while Repetition Structure
- Standard C Statements
Chapter 2.1 Control Structures (Selection)
Chapter 8 - JavaScript: Control Statements I
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Lecture 2: Logical Problems with Choices
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Structured Program Development in C
EPSII 59:006 Spring 2004.
Structured Program Development in C
Structured Program Development in C
Dale Roberts, Lecturer IUPUI
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Presentation transcript:

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

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

C.E., NCU, Taiwan Angela Chih-Wei Tang, 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

C.E., NCU, Taiwan Angela Chih-Wei Tang, 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

C.E., NCU, Taiwan Angela Chih-Wei Tang, 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

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

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

C.E., NCU, Taiwan Angela Chih-Wei Tang, 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

C.E., NCU, Taiwan Angela Chih-Wei Tang, 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”

C.E., NCU, Taiwan Angela Chih-Wei Tang, 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”

C.E., NCU, Taiwan Angela Chih-Wei Tang, 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” );

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

C.E., NCU, Taiwan Angela Chih-Wei Tang, 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”

C.E., NCU, Taiwan Angela Chih-Wei Tang, 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?

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

C.E., NCU, Taiwan Angela Chih-Wei Tang, 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

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

C.E., NCU, Taiwan Angela Chih-Wei Tang, 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

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

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

C.E., NCU, Taiwan Angela Chih-Wei Tang, 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

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

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

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

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 Enter grade, -1 to end: -1 No grades were entered Test 1 Test 2 25C.E., NCU, Taiwan25 Angela Chih-Wei Tang, 2010

C.E., NCU, Taiwan Angela Chih-Wei Tang, 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

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

Test C.E., NCU, Taiwan28 Angela Chih-Wei Tang, 2010

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

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

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

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

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