 2006 Pearson Education, Inc. All rights reserved. 1 5 5 Control Statements: Part 2.

Slides:



Advertisements
Similar presentations
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
4 Control Statements.
 2006 Pearson Education, Inc. All rights reserved Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
 2006 Pearson Education, Inc. All rights reserved Introduction.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2005 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 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.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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.
 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.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
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.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
 2009 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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.
Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: Control variable (loop counter) Initial value of the control variable.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2005 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved Control Statements: Part2.
 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.
Chapter 4 – C Program Control
Control Statements: Part 2
Control Statements: Part 2
Control Statements: Part 2
Control Statements: Part 2
MSIS 655 Advanced Business Applications Programming
Chapter 8 JavaScript: Control Statements, Part 2
4 C Program Control.
Chapter 6 Control Statements: Part 2
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
Chapter 8 JavaScript: Control Statements, Part 2
Control Statements:.
Presentation transcript:

 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2

 2006 Pearson Education, Inc. All rights reserved Introduction 5.2 Essentials of Counter-Controlled Repetition 5.3 for Repetition Statement 5.4 Examples Using the for Statement 5.5 do … while Repetition Statement 5.6 switch Multiple-Selection Statement 5.7 break and continue Statements 5.8 Logical Operators 5.9 Confusing Equality ( == ) and Assignment ( = ) Operators 5.10 Structured Programming Summary 5.11 (Optional) Software Engineering Case Study: Identifying Objects’ States and Activities in the ATM System 5.12 Wrap-Up

 2006 Pearson Education, Inc. All rights reserved Introduction Continue structured programming discussion – Introduce C++’s remaining control structures for, do … while, switch

 2006 Pearson Education, Inc. All rights reserved Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: – Name of a control variable (loop counter) – Initial value of the control variable – Loop-continuation condition that tests for the final value of the control variable – Increment/decrement of control variable at each iteration

 2006 Pearson Education, Inc. All rights reserved. 5 Common Programming Error 5.1 Floating-point values are approximate, so controlling counting loops with floating-point variables can result in imprecise counter values and inaccurate tests for termination. Error-Prevention Tip 5.1 Control counting loops with integer values.

 2006 Pearson Education, Inc. All rights reserved for Repetition Statement for repetition statement – Specifies counter-controlled repetition details in a single line of code Fig. 5.3 | for statement header components.

 2006 Pearson Education, Inc. All rights reserved for Repetition Statement (Cont.) General form of the for statement – for ( initialization ; loopContinuationCondition ; increment ) statement ; Can usually be rewritten as: – initialization ; while ( loopContinuationCondition ) { statement ; increment ; } If the control variable is declared in the initialization expression – It will be unknown outside the for statement

 2006 Pearson Education, Inc. All rights reserved for Repetition Statement (Cont.) The initialization and increment expressions can be comma-separated lists of expressions – These commas are comma operators Comma operator has the lowest precedence of all operators – Expressions are evaluated from left to right – Value and type of entire list are value and type of the rightmost expressions

 2006 Pearson Education, Inc. All rights reserved do … while Repetition Statement do…while statement – Similar to while statement – Tests loop-continuation after performing body of loop Loop body always executes at least once Good Programming Practice 5.9: Always including braces in a do...while statement helps eliminate ambiguity between the while statement and the do...while statement containing one statement.

 2006 Pearson Education, Inc. All rights reserved switch Multiple-Selection Statement switch statement – Used for multiple selections – Tests a variable or expression Compared against constant integral expressions to decide on action to take – Any combination of character constants and integer constants that evaluates to a constant integer value

 2006 Pearson Education, Inc. All rights reserved switch Multiple-Selection Statement (Cont.) switch statement – Controlling expression Expression in parentheses after keyword switch – case labels Compared with the controlling expression Statements following the matching case label are executed – Braces are not necessary around multiple statements in a case label – A break statements causes execution to proceed with the first statement after the switch Without a break statement, execution will fall through to the next case label Common Programming Error 5.11: Specifying an expression including variables (e.g., a + b ) in a switch statement’s case label is a syntax error.

 2006 Pearson Education, Inc. All rights reserved switch Multiple-Selection Statement (Cont.) switch statement (Cont.) – default case Executes if no matching case label is found Is optional – If no match and no default case Control simply continues after the switch Good Programming Practice 5.10: Provide a default case in switch statements. Cases not explicitly tested in a switch statement without a default case are ignored. Including a default case focuses the programmer on the need to process exceptional conditions. There are situations in which no default processing is needed. Although the case clauses and the default case clause in a switch statement can occur in any order, it is common practice to place the default clause last.

 2006 Pearson Education, Inc. All rights reserved switch Multiple-Selection Statement (Cont.) Integer data types – short Abbreviation of short int Minimum range is -32,768 to 32,767 – long Abbreviation of long int Minimum range is -2,147,483,648 to 2,147,483,647 – int Equivalent to either short or long on most computers – char Can be used to represent small integers – Portability Tip 5.4: Because int s can vary in size between systems, use long integers if you expect to process integers outside the range –32,768 to 32,767 and you would like to run the program on several different computer systems.

 2006 Pearson Education, Inc. All rights reserved break and continue Statements break / continue statements – Alter flow of control break statement – Causes immediate exit from control structure – Used in while, for, do…while or switch statements continue statement – Skips remaining statements in loop body Proceeds to increment and condition test in for loops Proceeds to condition test in while / do…while loops – Then performs next iteration (if not terminating) – Used in while, for or do…while statements

 2006 Pearson Education, Inc. All rights reserved. 15 Logical Operators && (logical AND), || (logical OR), ! (logical NOT) Operator precedence and associativity Confusing Equality (==) and Assignment (=) Operators