C How to Program, 6/e Summary ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 5: Control Structures II (Repetition)
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Algorithm and Programming Branching Algorithm & C Program Control Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University.
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
Chapter 05 (Part III) Control Statements: Part II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
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.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Chapter 4 – C Program Control
Chapter 4 C Program Control Part I
Chapter 4 C Program Control Part II
Lecture 7: Repeating a Known Number of Times
Chapter 4 - Program Control
TMF1414 Introduction to Programming
JavaScript: Control Statements.
Programming Fundamentals Lecture #6 Program Control
MSIS 655 Advanced Business Applications Programming
- Additional C Statements
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 4 - Program Control
Chapter 6 Control Statements: Part 2
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.

 In this chapter, repetition is considered in greater detail, and additional repetition control statements, namely the for and the do … while, are presented.  The switch multiple-selection statement is introduced.  We discuss the break statement for exiting immediately from certain control statements, and the continue statement for skipping the remainder of the body of a repetition statement and proceeding with the next iteration of the loop.  The chapter discusses logical operators used for combining conditions, and summarizes the principles of structured programming as presented in Chapter 3 and 4. © by Pearson Education, Inc. All Rights Reserved.

 A loop is a group of instructions the computer executes repeatedly while some loop-continuation condition remains true.  We have discussed two means of repetition: ◦ Counter-controlled repetition ◦ Sentinel-controlled repetition  Counter-controlled repetition is sometimes called definite repetition because we know in advance exactly how many times the loop will be executed.  Sentinel-controlled repetition is sometimes called indefinite repetition because it’s not known in advance how many times the loop will be executed.  In counter-controlled repetition, a control variable is used to count the number of repetitions.  The control variable is incremented (usually by 1) each time the group of instructions is performed. © by Pearson Education, Inc. All Rights Reserved.

 Counter-controlled repetition requires: ◦ The name of a control variable (or loop counter). ◦ The initial value of the control variable. ◦ The increment (or decrement) by which the control variable is modified each time through the loop. ◦ The condition that tests for the final value of the control variable (i.e., whether looping should continue). © by Pearson Education, Inc. All Rights Reserved.

 The initialization, loop-continuation condition and increment can contain arithmetic expressions. For example, if x = 2 and y = 10, the statement for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent to the statement for ( j = 2; j <= 80; j += 5 )  The “increment” may be negative (in which case it’s really a decrement and the loop actually counts downward).  If the loop-continuation condition is initially false, the loop body does not execute. Instead, execution proceeds with the statement following the for statement. © by Pearson Education, Inc. All Rights Reserved.

 Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each of the constant integral values it may assume, and different actions are taken.  This is called multiple selection.  C provides the switch multiple-selection statement to handle such decision making.  The switch statement consists of a series of case labels, an optional default case and statements to execute for each case.  Figure 4.7 uses switch to count the number of each different letter grade students earned on an exam. © by Pearson Education, Inc. All Rights Reserved.

 When the Enter key is pressed, the characters are read by function getchar one character at a time.  If the character entered is not equal to EOF, the switch statement (line 22) is entered.  Keyword switch is followed by the variable name grade in parentheses.  This is called the controlling expression.  The value of this expression is compared with each of the case labels.  Assume the user has entered the letter C as a grade.  C is automatically compared to each case in the switch. © by Pearson Education, Inc. All Rights Reserved.

 The do … while repetition statement is similar to the while statement.  In the while statement, the loop-continuation condition is tested at the beginning of the loop before the body of the loop is performed.  The do … while statement tests the loop-continuation condition after the loop body is performed.  Therefore, the loop body will be executed at least once.  When a do … while terminates, execution continues with the statement after the while clause. © by Pearson Education, Inc. All Rights Reserved.

 It’s not necessary to use braces in the do … while statement if there is only one statement in the body.  However, the braces are usually included to avoid confusion between the while and do … while statements.  For example, while ( condition )  is normally regarded as the header to a while statement. © by Pearson Education, Inc. All Rights Reserved.

 A do … while with no braces around the single- statement body appears as do statement while ( condition );  which can be confusing.  The last line— while( condition ); —may be misinterpreted by as a while statement containing an empty statement.  Thus, to avoid confusion, the do … while with one statement is often written as follows: © by Pearson Education, Inc. All Rights Reserved.

 The break and continue statements are used to alter the flow of control.  The break statement, when executed in a while, for, do … while or switch statement, causes an immediate exit from that statement.  Program execution continues with the next statement.  Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch statement (as in Fig. 4.7). © by Pearson Education, Inc. All Rights Reserved.

 C provides logical operators that may be used to form more complex conditions by combining simple conditions.  The logical operators are && (logical AND), || (logical OR) and ! (logical NOT also called logical negation).  We’ll consider examples of each of these operators.  Suppose we wish to ensure that two conditions are both true before we choose a certain path of execution. © by Pearson Education, Inc. All Rights Reserved.

 There is one type of error that C programmers, no matter how experienced, tend to make so frequently that we felt it was worth a separate section.  That error is accidentally swapping the operators == (equality) and = (assignment).  What makes these swaps so damaging is the fact that they do not ordinarily cause compilation errors.  Rather, statements with these errors ordinarily compile correctly, allowing programs to run to completion while likely generating incorrect results through runtime logic errors. © by Pearson Education, Inc. All Rights Reserved.

 Figure 4.17 summarizes the control statements discussed in Chapters 3 and 4.  Small circles are used in the figure to indicate the single entry point and the single exit point of each statement.  Connecting individual flowchart symbols arbitrarily can lead to unstructured programs.  Therefore, the programming profession has chosen to combine flowchart symbols to form a limited set of control statements, and to build only structured programs by properly combining control statements in two simple ways. © by Pearson Education, Inc. All Rights Reserved.