Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Repeating Actions While and For Loops
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Loops and Files.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
CPS120 Introduction to Computer Science Iteration (Looping)
Programming Logic and Design Fifth Edition, Comprehensive
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Copyright © 2012 Pearson Education, Inc. Chapter 5: 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.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
While ( number
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Introduction To Repetition The for loop
Lecture 6 Repetition Richard Gesick.
Lesson 05: Iterations Class Chat: Attendance: Participation
Programming Logic and Design Fourth Edition, Comprehensive
Loop Structures.
Programming Fundamentals
JavaScript: Control Statements.
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.
Looping and Repetition
MSIS 655 Advanced Business Applications Programming
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
Chapter 6: Repetition Statements
JavaScript: Control Statements II
CHAPTER 21 LOOPS 1.
More Loops Topics Counter-Controlled (Definite) Repetition
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Building Java Programs
More Loops Topics Counter-Controlled (Definite) Repetition
Learning Plan 4 Looping.
More Loops Topics Counter-Controlled (Definite) Repetition
Looping and Repetition
Presentation transcript:

Loops Tonga Institute of Higher Education

Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop  While Loop  Do-While Loop

Loops Parts of a loop  Action  End Condition Example: Scanning items at checkout counter in grocery store.  Action: Get a price for the item and add it to the bill.  End Condition: All items have a price and have been added to the bill.

For Loop Executes a definite number of times. Condition is evaluated before the body of the loop is executed. Incrementation of counter takes place after the Body of Loop is executed. For (initialization; continuation condition; increment) Body of Loop For (start at this value; keep looping as long as this expression is true; increment the value) Body of Loop Semicolons Can initialize variable here Body of Loop is 1 line Body of Loop can be multiple lines

For Loop Variations for (;;)  Infinite Loop for (int counter = 1; counter < 11;)  Increment counter inside of the loop for (; counter < 11; counter = counter + 1)  Initialize the counter outside of the loop

Incrementing and Decrementing Increment  number1 = number1 + 1 Or  number1++ Shortcut to make it easier to read and avoid errors. Decrement  number1 = number1 - 1 Or  number1-- Shortcut to make it easier to read and avoid errors.

Comprehension Check For Loops

While Loop Executes an indefinite number of times.  The body of the loop is executed as long as the condition is true. Developer controls when condition changes.  Done in the body of the loop.  If not done, the loop will never end. Condition is evaluated before the body of the loop is executed.  The body of the loop is not guaranteed to be executed. while (continuation condition) { Body of Loop } Initialization of counter occurs outside of loop Incrementation occurs inside of loop Keep looping until this condition is false

Example of While Loop Prints 0, 5, 10, 15

Example of While Loop Prints 0, 5, 10, 15 Modified to make sure 15 is printed

Example of While Loop The condition is evaluated first, So nothing is printed.

Comprehension Check While Loops

Do-While Loop Executes an indefinite number of times.  The body of the loop is executed as long as the condition is true. Developer controls when counter is incremented.  Done in the body of the loop.  If not done, the loop will never end. Condition is evaluated after the body of the loop is executed.  The body of the loop is executed at least once. do { Body of Loop } while (continuation condition) Initialization of counter occurs outside of loop Incrementation occurs inside of loop

Example of Do-While Loop Prints 0, 5, 10, 15

Example of Do-While Loop Prints 0, 5, 10, 15Modified to make sure 15 is printed

Example of Do-While Loop The body of the loop is executed first. Then, the condition is evaluated to see if we want to repeat the loop. Therefore, “Hello World” is printed.

Comprehension Check Do-While Loops

Quitting a Loop The break command is used to exit a loop

Demonstration Quitting a Loop