There are two additional statements that can be used to control the operation of while loops and for loops: break statements continue statements.

Slides:



Advertisements
Similar presentations
Repeating Actions While and For Loops
Advertisements

Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
Computer Science 1620 Loops.
Do Loop The syntax of DO loop: DO variable = initial_value, final_value[, increment] [statements] END DO Example: PROGRAM LINES ! Illustration of DO-loops.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CHAPTER 6 REPETITIVE EXECUTION 6.1 Introduction A repetition structure or loop makes possible the repeated execution of one or more statements called the.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Nested Loops. Nesting Control Structures One if statement inside another one An if statement inside a loop A loop inside an if statement Control structures.
DCT 1123 Problem Solving & Algorithms
For Loops 2 ENGR 1181 MATLAB 9. For Loops and Looped Programming in Real Life As first introduced last lecture, looping within programs has long been.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
MATLAB FUNDAMENTALS: CONTROL STRUCTURES – LOOPS HP 100 – MATLAB Wednesday, 10/1/2014
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!
Chapter 5 Loops.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
Controlling Execution Dong Shao, Nanjing Unviersity.
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,
Variety of JavaScript Examples Please use speaker notes for additional information!
Bordoloi and Bock Control Structures: Iterative Control.
Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.
6.2 For…Next Loops General Form of a For…Next Loop
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Matlab Programming for Engineers
CPS120 Introduction to Computer Science Iteration (Looping)
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.
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.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CSE123 - Lecture 4 Structured Programming- Loops.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
Computer Programming -1-
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Fundamentals of PL/SQL part 2 (Basics)
Chapter 3: Decisions and Loops
Branching Constructs Review
The nested repetition control structures & Continue Statements
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 4 Loops Case Studies
Conditinoal Constructs Review
Outline Altering flow of control Boolean expressions
Conditinoal Constructs Review
Count Controlled Loops (Nested)
Iteration: Beyond the Basic PERFORM
Lab5 PROGRAMMING 1 Loop chapter4.
3.1 Iteration Loops For … To … Next 18/01/2019.
Alternate Version of STARTING OUT WITH C++ 4th Edition
ECE 103 Engineering Programming Chapter 19 Nested Loops
CS 1111 Introduction to Programming Spring 2019
Program Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Building Java Programs
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Repetition (While Loop) LAB 9
Database Programming Using Oracle 11g
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

There are two additional statements that can be used to control the operation of while loops and for loops: break statements continue statements

The break statement terminates the execution of a loop and passes control to the next statement after the end of the loop. An example of the break statement in a for loop is shown here. for ii = 1:5 if ii == 3; break; end fprintf('ii = %d\n',ii); end disp(['End of loop!']); When this program is executed, the output is » test_break ii = 1 ii = 2 End of loop!

If a continue statement is executed in the body of a loop, the execution of the current pass through the loop will stop, and control will return to the top of the loop. The controlling variable in the for loop will take on its next value, and the loop will be executed again. An example of the continue statement in a for loop is shown here. for ii = 1:5 if ii == 3; continue; end fprintf('ii = %d\n',ii); end disp(['End of loop!']); When this program is executed, the output is » test_continue ii = 1 ii = 2 ii = 4 ii = 5 End of loop!

It is possible for one loop to be completely inside another loop. If one loop is completely inside another one, the two loops are called nested loops. The following example shows two nested for loops used to calculate and write out the product of two integers. for ii = 1:3 for jj = 1:3 product = ii * jj; fprintf('%d * %d = %d\n',ii,jj,product); end

If a break or continue statement appears inside a set of nested loops, then that statement refers to the innermost of the loops containing it. For example, consider the following program: for ii = 1:3 for jj = 1:3 if jj == 3; break; end product = ii * jj; fprintf('%d * %d = %d\n',ii,jj,product); end fprintf('End of inner loop\n'); end fprintf('End of outer loop\n');