Iteration CSCE 121 J. Michael Moore.

Slides:



Advertisements
Similar presentations
Iteration While / until/ for loop. Iteration: while/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initalise.
Advertisements

While loops.
CS0007: Introduction to Computer Programming
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Objectives In this chapter, you will learn about:
Introduction to Computers and Programming Lecture 9: For Loops New York University.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Loops and Iteration for Statements, while Statements and do-while Statements.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Chapter 05 (Part III) Control Statements: Part II.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
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.
Loops ISYS 350. Two Types of Loops while loop for loop.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Lecture 4b Repeating With Loops
Lecture 6 Repetition Richard Gesick.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Loops ISYS 350.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Control Structure Senior Lecturer
Lecture 4A Repetition Richard Gesick.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
MSIS 655 Advanced Business Applications Programming
Structured Program Development in C
Selection CSCE 121 J. Michael Moore.
Loops A loop is a repetition control structure.
Java Programming Loops
Loops CIS 40 – Introduction to Programming in Python
3.5- The while Statement The while statement has the following syntax:
Loops ISYS 350.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Loop Strategies Repetition Playbook.
Java Programming Loops
Loops ISYS 350.
Seating “chart” Front - Screen rows Back DOOR.
Repetition Statements (Loops) - 2
Java LESSON 3 Loops.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Looping and Repetition
Presentation transcript:

Iteration CSCE 121 J. Michael Moore

Logical Parts of a Loop and Terminology Loop Body Don’t forget to initialize Iteration One instance of executing the loop body. Termination Condition (boolean expression) AKA loop control, exit condition, looping variable, etc. Initialize Update

Creating a Loop What do things have to look like before entering the loop? Initialization (control variable) Initialization (loop body) How do you get out of the loop? Control variable Termination condition What do you do ensure you can exit the loop? Update What does the loop do? Loop Body

While // initialize while (termination condition) { // do stuff // update } Note: Curly braces are optional if there is a single statement.

For Note: Curly braces are optional if there is a single statement. // A. initialize // B. termination condition // C. update for(A; B; C) { // do stuff } Do NOT update control variable inside loop.

Types of control Counting Sentinel Flag Control variable increments by set amount each iteration. Sentinel Control variable is set to a value obtained during the loop each iteration. Flag Control variable is a boolean variable that represents a condition each iteration.

Do While // initialize do { // do stuff // update (can be initialization) } while (termination condition); Note: Curly braces are optional if there is a single statement.

Loops Loop type Minimum times through loop Maximum times through loop While For Do While 1 Loop type Initialization Update While Outside of loop Within loop body For Built into statement Do While

Loop conversion Most loops can be converted to any other type of loop. What can’t? (Well you could but it would be bad practice) Good exercise that can help you build good loops structures. Remember the guidance for building a loop…

Teaser New (for C++) loop in C++ 11 Range based for loop (we’ll talk more after we do vectors).