Docsity

Computer programming, Apuntes de Programación C

Vista previa parcial del texto

¡Descarga Computer programming y más Apuntes en PDF de Programación C solo en Docsity!

C Programming1 L

DR MOHAMMED AL-HUBAISHI

DR : Mohammed Al

  • Hubaishi

Chapter 3

Structured Program

Development in C

3.1 Introduction  Before writing a program to solve a particular problem, we must have a thorough understanding of the problem and a carefully planned solution approach.  The next two chapters discuss techniques that facilitate the development of structured computer programs.

3.2 Algorithms (Cont.)  Consider the “rise-and-shine algorithm” followed by one junior executive for getting out of bed and going to work: (1) Get out of bed, (2) take off pajamas, (3) take a shower, (4) get dressed, (5) eat breakfast, (6) carpool to work.  This routine gets the executive to work well prepared to make critical decisions.

3.2 Algorithms (Cont.)  Suppose that the same steps are performed in a slightly different order: (1) Get out of bed, (2) take off pajamas, (3) get dressed, (4) take a shower, (5) eat breakfast, (6) carpool to work.  In this case, our junior executive shows up for work soaking wet.  Specifying the order in which statements are to be executed in a computer program is called program control.

3.3 Pseudocode (Cont.)  A carefully prepared pseudocode program may be converted easily to a corresponding C program.  Pseudocode consists only of action and decision statements— those that are executed when the program has been converted from pseudocode to C and is run in C.  Definitions are not executable statements.  They’re simply messages to the compiler.

3.3 Pseudocode (Cont.)  For example, the definition  int i;

tells the compiler the type of variable i and

instructs the compiler to reserve space in

memory for the variable.

 But this definition does not cause any action—such as input, output, a calculation or a comparison—to occur when the program is executed.  Some programmers choose to list each variable and briefly mention the purpose of each at the beginning of a pseudocode program.

3.4 Control Structures (Cont.)  The finger of blame was pointed at the goto statement that allows you to specify a transfer of control to one of many possible destinations in a program.  The notion of so-called structured programming became almost synonymous with “goto elimination.”  Research had demonstrated that programs could be written without any goto statements.  The challenge of the era was for programmers to shift their styles to “goto-less programming.”

3.4 Control Structures (Cont.)  The results were impressive, as software development groups reported reduced development times, more frequent on-time delivery of systems and more frequent within-budget completion of software projects.  Programs produced with structured techniques were clearer, easier to debug and modify and more likely to be bug free in the first place.  Research had demonstrated that all programs could be written in terms of only three control structures, namely the sequence structure, the selection structure and the iteration structure.

3.4 Control Structures (Cont.)  Like pseudocode, flowcharts are useful for developing and representing algorithms, although pseudocode is preferred by most programmers.  Consider the flowchart for the sequence structure in Fig. 3.1.  We use the rectangle symbol, also called the action symbol, to indicate any type of action including a calculation or an input/output operation.  The flowlines in the figure indicate the order in which the actions are performed—first, grade is added to total, then 1 is added to counter.  C allows us to have as many actions as we want in a sequence structure.

3.4 Control Structures (Cont.) Selection Statements in C  C provides three types of selection structures in the form of statements.  The if selection statement (Section 3.5) either selects (performs) an action if a condition is true or skips the action if the condition is false.  The if…else selection statement (Section 3.6) performs an action if a condition is true and performs a different action if the condition is false.  The switch selection statement (discussed in Chapter 4) performs one of many different actions depending on the value of an expression.

3.4 Control Structures (Cont.)  The if statement is called a single-selection statement because it selects or ignores a single action.  The if…else statement is called a double-selection statement because it selects between two different actions.  The switch statement is called a multiple-selection statement because it selects among many different actions. Iteration Statements in C  C provides three types of iteration structures in the form of statements, namely while (Section 3.7), do…while, and for (both discussed in Chapter 4).  That’s all there is.