1 Statements: Control Structure Issues (Chapters 15-17 and 19 of Code Complete) Don Bagert CSSE 375, Rose-Hulman October 17, 2006.

Slides:



Advertisements
Similar presentations
Statement-Level Control Structures
Advertisements

ISBN Chapter 8 Statement-Level Control Structures.
Repeating Structures Do While Loops. Do While Loop While loops have a test condition before the loop –This means that java will test the condition before.
1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Chapter 5: Control Structures II (Repetition)
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
UNIT II Decision Making And Branching Decision Making And Looping
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
sequence of execution of high-level statements
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Controlling Execution Dong Shao, Nanjing Unviersity.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CSI 3120, Control, page 1 Control statements Simple statements Basic structured statements Sequence Selection Iteration The jump statement.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Control Structures sequence of execution of high-level statements.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
Test 2 Review Outline.
Exam 2 Review.
Unit-1 Introduction to Java
Control Structures II (Repetition)
Control Statements Lecture 6 from Chapter 5.
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
Expressions and Control Flow in JavaScript
Statement-Level Control Structures
The University of Texas – Pan American
Control statements Simple statements Basic structured statements
Iteration: Beyond the Basic PERFORM
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Three Special Structures – Case, Do While, and Do Until
Statement-Level Control Structures
Program Flow.
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

1 Statements: Control Structure Issues (Chapters and 19 of Code Complete) Don Bagert CSSE 375, Rose-Hulman October 17, 2006

2 Outline  Selection Statements  Loops  Some Other Statements

3 Selection Statements

4 Definition  A selection statement is a statement where a selection of which statements to execute out of two or more possibilities is determined through the use of a condition of some kind. Examples: if and switch statements in Java and C++

5 Some Selection Statement Issues – 1/3  In general, case (switch) statements should be used whenever possible if three of more cases are involved. However, in most languages, case statements are limited to the comparison of an “integral” variable to a series of values or value ranges, so a nested if-then-else must often be used The Boston rail yard: A view of multiple train switches.

6 Some Selection Statement Issues – 2/3  In a nested if-then-else, “lining up” all of the elses aids readability. Example (in Java): if (NumberInput == 0) NumberOfZeroesCounted++; else if (NumberInput > 0) NumberOfPositivesCounted++; else NumberOfNegativesCounted++;

7 Some Selection Statement Issues – 3/3  Using an “else do nothing” can be useful, especially with nested if statements. Example (using an outline in PDL syntax): if... then if... then... else... With which then will the else be associated?

8 Loops

9 Some Loop Issues – 1/3  A pre-test loop (such as the while statement in Java) is most generic of the loop statements, since it can be used to represent any loop.  However, if the loop definitely needs to be executed at least once (e.g. in a bubblesort) a post-test loop (such as the do-while in Java) should be used instead.

10 Some Loop Issues – 2/3  When for loops should be used varies from language to language  Visual Basic Example (a simple format): for ListIndex = 1 to ListSize  Java and C++ Example (more complex): for (i = 0; i < ListSize && !found; i++)

11 Some Loop Issues – 3/3  Entry from the beginning of and exit from either the beginning or the end of the loop is useful in many ways  Therefore (in my opinion), the use of features such as the “break” in Java should be avoided if at all possible

12 Some Other Statements

13 Goto and Return Statements  The goto is a very powerful statement which you will probably find in frequent use in certain types of legacy code  The return statement has some features of the goto. In my opinion, it should generally not be used for procedures (void functions in Java and C++) and only as the last statement of a function. Example: Return (FunctionValue);

14 Recursion  Recursion should be used only when the complexity is significantly less than that of using a loop  Infinite recursion must be avoided in the same way as infinite loops are

15 Quiz Exercise 5: Coding Standards for Control Statements