© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Control Structures for Loops.
Computer Science 1620 Loops.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS150 Introduction to Computer Science 1
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
Loop Statements (Iteration). Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions Parameters passed by reference.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
Chapter 6. Loops A control structure that causes a statement or group of statements to be executed repeatedly There are 3 types of loops –while –for –do.
Computer Programming -1-
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Chapter 4 Repetition Structures
Introduction to Computer Programming
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Computer Science 101 While Statement.
Control Structures - Repetition
While Loops.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Iteration with While You can say that again.
Programming Fundamentals Lecture #6 Program Control
For & do/while Loops.
Conditional Construct
Repetition Control Structure
CMPT 102 Introduction to Scientific Computer Programming
© 2016 Pearson Education, Ltd. All rights reserved.
CS150 Introduction to Computer Science 1
CS149D Elements of Computer Science
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements

Avoid infinite loops  Can infinite loops ever be useful?  Yes, but usually there is a safer way to do the same thing  Should you purposely write and infinite loop?  Generally not  For some specific purposes, if you are very careful and know that the loop will be terminated regardless of the path taken through the program, an infinite loop may be useful. © Janice Regan, CMPT 128,

Exiting a loop before it completes  Sometimes you wish to exit from a loop even if the loop condition is still true  For example if your loop is reading and processing data and an error occurs that can be recognized but cannot be corrected, you may wish to print an error message and exit the loop immediately  How do you exit a loop while the loop condition is true? Use a break statement © Janice Regan, CMPT 128,

3 The break Statement:  break;  A C++ statement that causes you to exit the loop you are in  Program execution will continue at the statement following the loop

© Janice Regan, CMPT 128, break statement T F Condition Loop condition ….. Statement n; break; T F Statement 1; ….. condition2

© Janice Regan, CMPT 128, Example: break // Read and sum an unknown number of positive integers // Stop reading +ve integers when you read a -ve integer sum = 0; count = 0; while ( count < ) // Safer than infinite loop { cin >> nextint; if( nextint < 0 ) break; sum += nextint; count++; } cout << sum;

Executing only some statements in a loop  Sometimes you wish to execute a block of statements inside your loop only under particular conditions  For example you may wish to skip the last half of the body of the loop if divisor = 0 (so you will not divide by 0)  How do you execute only some of the statements inside the loop? Use a continue statement © Janice Regan, CMPT 128,

7 The continue Statement:  continue;  A C++ statement that causes you to go from anywhere in the loop to the (update statement and loop condition test)  Program execution will continue at the first statement inside the loop after the loop variable has been incremented and tested (if the test indicates another pass through the loop)

© Janice Regan, CMPT 128, continue statement T F Condition ….. Statement n; continue; T F Statement 1; ….. condition2

© Janice Regan, CMPT 128, Example: continue // Sum the even integers from 1 to 35 x = 0; sum = 0; while ( x <= 35 ) { x++; if ( x % 2 == 1 ) continue; sum += x; } cout << sum;

© Janice Regan, CMPT 128, Example: continue // Determine the kilometers per litre int litres = 0; int kilometers = 0; x = 0; while ( x < 1000 ) { cout << “enter #of liters and # kilometers”; cin >> litres >> kilometers ; x++; if ( litres == 0 ) continue; cout << “kilometers/liter “ << kilometers / litres; }