Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

Introduction to C Programming
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
While loops.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Repetition Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Loop Statements After reading and studying this Section,
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Control Structures II (Repetition)
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
CPS120 Introduction to Computer Science Iteration (Looping)
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
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.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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 For Loops l From Chapter 9 l A shorthand way of coding count loops.
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
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.
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.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Chapter 9 Repetition.
Chapter 4 Repetition Statements (loops)
Lecture 6 Repetition Richard Gesick.
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
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.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 5 Repetition.
Outline Altering flow of control Boolean expressions
LRobot Game.
Chapter 9 Control Structures.
Lecture Notes – Week 3 Lecture-2
Pages:51-59 Section: Control1 : decisions
Repetition Control Structure
Chapter 8: More on the Repetition Structure
Chapter 6: Repetition Statements
Alternate Version of STARTING OUT WITH C++ 4th Edition
Seating “chart” Front - Screen rows Back DOOR.
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Statements in C Programming
Pages:51-59 Section: Control1 : decisions
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Looping and Repetition
Presentation transcript:

Repetition Control Structure

Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition in control flow. We will examine “while” statement in Java and learn how to implement repetition using this statement. More specifically, we will learn the “syntax” or structure and “semantics” or meaning, and the usage of this statement.

Topics for discussion Repetition structure (loop) design while loop : syntax, semantics, example Loop control Summary

while loop : syntax while ( condition ) {statement } “condition” is a logical expression that evaluates to true or false. It could be a relational or boolean expression. “statement” could a single statement or more than one statement bounded by { }.

while loop : semantics 1) The condition is evaluated. 2) If it is true, then the body of the loop is executed. Then the control is transferred back to the condition for re-evaluation. 3) If the logical expression is false, the while loop is exited and control is transferred to the statement after the while statement.

The while statement while (condition) { statement block } Condition? Statements yes no

Example 1 Problem: Do something 12 times. int count = 0; // to keep count of how many are done while (count <12) // 0,1,2,3,4,5,6,7,8,9,10,11 { // do something // update the counter count = count + 1; // increment the counter by 1 }

Example in Greenfoot int count = 0; // initialize the count GreenfootImage bg = getWorld().getBackground(); bg.setColor(Color.WHITE); // set the color while (count < 6) // 0, 1, 2, 3,,4,5 { bg.drawString(“ “ + count, 100*count, 320); }

Loop Design While Loop design should consider: – Initialization of conditions (count = 0) – Termination of the loop (when count >= 6) – Testing (at the top or bottom of the loop) (at the top, count >= 6) – Updating conditions ( count = count + 1) – Of course, the body of the loop. ( output the count on the scenario) Body of the loop: Statements representing the process to be repeated. These are statements within the scope of a loop.

10 concepts 1.While loop for repeating operations 2.While initialization 3.While condition 4.While body 5.While condition updated within the loop 6.While termination 7.Writing out to Greenfoot background 8.Setting the color for background (for future output) 9.Location of output (x and y) 10.Algebraic expression (count*100)