Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.

Slides:



Advertisements
Similar presentations
Chapter 04 (Part III) Control Statements: Part I.
Advertisements

 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
 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
Introduction to Computers and Programming Lecture 7:
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Control Structures in C++ while, do/while, for switch, break, continue.
 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
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
Lecture 3 Structured Program Development in C
 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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
The University of Texas – Pan American
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
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.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
 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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 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.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved. 1.
1 Lecture 3 Control Structures else/if and while.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator.
Introduction to Computers and Programming Lecture 7:
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
© 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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 7 JavaScript: Control Statements, Part 1
Algorithm: procedure in terms of
while Repetition Structure
Chapter 4 C Program Control Part I
TMF1414 Introduction to Programming
Ch 7: JavaScript Control Statements I.
Chapter 4- part 2 Control Statements: Loops 1
Control Structures Lecture 7.
Chapter 4 Control Statements: Loops 1
Chapter 3 - Structured Program Development
3 Control Statements:.
The ‘while’ loop ‘round and ‘round we go.
Chapter 3 - Structured Program Development
EPSII 59:006 Spring 2004.
Presentation transcript:

Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1

3.7 The while Iteration Statement An iteration statement (also called an iteration statement) allows you to specify that an action is to be repeated while some condition remains true. The pseudocode statement – While there are more items on my shopping list Purchase next item and cross it off my list describes the iteration that occurs during a shopping trip. The condition, “there are more items on my shopping list” may be true or false. If it’s true, then the action, “Purchase next item and cross it off my list” is performed. This action will be performed repeatedly while the condition remains true. © 2016 Pearson Education, Ltd. All rights reserved. 2

3.7 The while Iteration Statement (Cont.) As an example of a while statement, consider a program segment designed to find the first power of 3 larger than 100. Suppose the integer variable product has been initialized to 3. When the following while iteration statement finishes executing, product will contain the desired answer: product = 3; while ( product <= 100 ) { product = 3 * product; } // end while The flowchart of Fig. 3.4 illustrates the flow of control in the while iteration statement. © 2016 Pearson Education, Ltd. All rights reserved. 3

4

3.7 The while Iteration Statement (Cont.) When the while statement is entered, the value of product is 3. The variable product is repeatedly multiplied by 3, taking on the values 9, 27 and 81 successively. When product becomes 243, the condition in the while statement, product <= 100, becomes false. This terminates the iteration, and the final value of product is 243. Program execution continues with the next statement after the while. © 2016 Pearson Education, Ltd. All rights reserved. 5

3.8 Formulating Algorithms Case Study 1: Counter-Controlled Iteration To illustrate how algorithms are developed, we solve several variations of a class-average problem. Consider the following problem statement: – 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. The class average is equal to the sum of the grades divided by the number of students. The algorithm for solving this problem on a computer must – input each of the grades, – perform the average calculation, – print the result. © 2016 Pearson Education, Ltd. All rights reserved. 6

3.8 Formulating Algorithms Case Study 1: Counter-Controlled Iteration (Cont.) Let’s use pseudocode to list the actions to execute and specify the order in which these actions should execute. We use counter-controlled iteration to input the grades one at a time. This technique uses a variable called a counter to specify the number of times a set of statements should execute. In this example, iteration terminates when the counter exceeds 10. © 2016 Pearson Education, Ltd. All rights reserved. 7

8

9

10

3.8 Formulating Algorithms Case Study 1: Counter-Controlled Iteration (Cont.) Variables used to store totals should normally be initialized to zero before being used in a program; otherwise the sum would include the previous value stored in the total’s memory location. Counter variables are normally initialized to zero or one, depending on their use. An uninitialized variable contains a “garbage” value—the value last stored in the memory location reserved for that variable. © 2016 Pearson Education, Ltd. All rights reserved. 11

3.9 Converting Between Types Explicitly and Implicitly Averages do not always evaluate to integer values. Often, an average is a value such as 7.2 or –93.5 that contains a fractional part. These values are referred to as floating-point numbers and can be represented by the data type float. The variable average is defined to be of type float to capture the fractional result of our calculation. However, the result of the calculation total / counter is an integer because total and counter are both integer variables. © 2016 Pearson Education, Ltd. All rights reserved. 12

3.9 Converting Between Types Explicitly and Implicitly(Cont.) Dividing two integers results in integer division in which any fractional part of the calculation is truncated (i.e., lost). To produce a floating-point calculation with integer values, we must create temporary values that are floating-point numbers. C provides the cast operator to accomplish this task. © 2016 Pearson Education, Ltd. All rights reserved. 13

3.9 Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 2: Sentinel-Controlled Iteration (Cont.) Casting: average = ( float ) total / 10; The value stored in total is still an integer. Using a cast operator in this manner is called explicit conversion. © 2016 Pearson Education, Ltd. All rights reserved. 14

© 2016 Pearson Education, Ltd. All rights reserved. 15 Optional

3.9 Formatting Floating-Point Numbers Example code uses the printf conversion specifier %.2f to print the value of average. The f specifies that a floating-point value will be printed. The.2 is the precision with which the value will be displayed—with 2 digits to the right of the decimal point. printf( "%.2f\n", ); // prints 3.45 printf( "%.1f\n", ); // prints 3.4 © 2016 Pearson Education, Ltd. All rights reserved. 16

Sentinel-Controlled Iteration Let’s generalize the class-average problem. Consider the following problem: – Develop a class-average program that will process an arbitrary number of grades each time the program is run. In the first class-average example, the number of grades (10) was known in advance. In this example, the program must process an arbitrary number of grades. How can the program determine when to stop the input of grades? How will it know when to calculate and print the class average? © 2016 Pearson Education, Ltd. All rights reserved. 17

Sentinel-Controlled Iteration (Cont.) One way to solve this problem is to use a special value called a sentinel value (also called a signal value, a dummy value, or a flag value) to indicate “end of data entry.” The user types in grades until all legitimate grades have been entered. The user then types the sentinel value to indicate “the last grade has been entered.” Sentinel-controlled iteration is often called indefinite iteration because the number of iterations isn’t known before the loop begins executing. © 2016 Pearson Education, Ltd. All rights reserved. 18

Sentinel-Controlled Iteration (Cont.) Clearly, the sentinel value must be chosen so that it cannot be confused with an acceptable input value. Because grades on a quiz are normally nonnegative integers, –1 is an acceptable sentinel value for this problem. Thus, a run of the class-average program might process a stream of inputs such as 95, 96, 75, 74, 89 and –1. The program would then compute and print the class average for the grades 95, 96, 75, 74, and 89 (– 1 is the sentinel value, so it should not enter into the average calculation). © 2016 Pearson Education, Ltd. All rights reserved. 19

© 2016 Pearson Education, Ltd. All rights reserved. 20

© 2016 Pearson Education, Ltd. All rights reserved. 21

3.10 Data Types (Integer Types) © 2016 Pearson Education, Ltd. All rights reserved. 22 TypeStorage sizeValue range char1 byte-128 to 127 or 0 to 255 unsigned char1 byte0 to 255 signed char1 byte-128 to 127 int2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 unsigned int2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295 short2 bytes-32,768 to 32,767 unsigned short2 bytes0 to 65,535 long4 bytes-2,147,483,648 to 2,147,483,647 unsigned long4 bytes0 to 4,294,967,295

3.10 Data Types (Floating Point Types) © 2016 Pearson Education, Ltd. All rights reserved. 23 TypeStorage sizeValue rangePrecision float4 byte 1.2E-38 to 3.4E+38 6 decimal places double8 byte 2.3E-308 to 1.7E decimal places long double10 byte3.4E-4932 to 1.1E decimal places

3.11 Assignment Operators C provides several assignment operators for abbreviating assignment expressions. For example, the statement c = c + 3; can be abbreviated with the addition assignment operator += as c += 3; The += operator adds the value of the expression on the right of the operator to the value of the variable on the left of the operator and stores the result in the variable on the left of the operator. © 2016 Pearson Education, Ltd. All rights reserved. 24

© 2016 Pearson Education, Ltd. All rights reserved.25

3.12 Increment and Decrement Operators C also provides the unary increment operator, ++, and the unary decrement operator, --, which are summarized in Fig If a variable c is to be incremented by 1, the increment operator ++ can be used rather than the expressions c = c + 1 or c += 1. If increment or decrement operators are placed before a variable (i.e., prefixed), they’re referred to as the preincrement or predecrement operators, respectively. If increment or decrement operators are placed after a variable (i.e., postfixed), they’re referred to as the postincrement or postdecrement operators, respectively. © 2016 Pearson Education, Ltd. All rights reserved. 26

3.12 Increment and Decrement Operators (Cont.) Preincrementing (predecrementing) a variable causes the variable to be incremented (decremented) by 1, then the new value of the variable is used in the expression in which it appears. Postincrementing (postdecrementing) the variable causes the current value of the variable to be used in the expression in which it appears, then the variable value is incremented (decremented) by 1. © 2016 Pearson Education, Ltd. All rights reserved. 27

© 2016 Pearson Education, Ltd. All rights reserved.28

3.12 Increment and Decrement Operators (Cont.) Figure 3.13 demonstrates the difference between the preincrementing and the postincrementing versions of the ++ operator. Postincrementing the variable c causes it to be incremented after it’s used in the printf statement. Preincrementing the variable c causes it to be incremented before it’s used in the printf statement. © 2016 Pearson Education, Ltd. All rights reserved. 29

© 2016 Pearson Education, Ltd. All rights reserved.30

© 2016 Pearson Education, Ltd. All rights reserved.31