Lec 05 Repetition

Lec 05 Repetition - FIT1002 Computer Programming 2010...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
FIT1002 Computer Programming 2010 Module 5 Repetition www.monash.edu.au FIT1002 Computer Programming Semester 1, 2010 Module 5 – Repetition COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Copyright Act 1968 (the Act). The material in this communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice. The authors acknowledge that material in these modules has been adapted from slides produced by Lewis, DePasquale and Chase for Java Foundations: Introduction to Programming and Data Structures , Addison-Wesley, 2007 2 Assumed knowledge Before the beginning of this lecture, students should be familiar with: – Variables and constants – Assignment statements – Writing expressions – Evaluating boolean expressions Before the beginning of this lecture, students should have read over: – Chapter 4 pages 132-143 &147-156 of Lewis et al. 3 Objectives By the end of this lecture, students should: – Understand repetition (looping) control structures – Understand the difference between a for loop, a while loop and a do…while loop – Be able to construct and use loops of each of these types – Understand how to form and use nested control structures
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
FIT1002 Computer Programming 2010 Module 5 Repetition 4 Repetition • Repeat an instruction. .. – … for as long as some condition holds – Test the condition once each iteration • Also known as iteration or loop 5 How do I get out? Example: Maze Algorithm 6 while (not at exit) { if (no wall on left) { turn left, step ahead } else { if (no wall ahead) step ahead else turn right } } “run through the maze always sticking to the wall on your left” Example: Maze Algorithm
Background image of page 2
FIT1002 Computer Programming 2010 Module 5 Repetition 7 Repetition in Java • Repetition statements are controlled by boolean expressions (like conditional statements) • Java has three kinds of repetition statements: – the while loop – the do… while loop – the for loop • The programmer should choose the right kind of loop for the situation 8 The while loop while ( condition ) block Syntax If the condition is true, the block is executed Then the condition is evaluated again, and if it is still true, the block is executed again The block is executed repeatedly until the condition becomes false The condition is evaluated at the start of each iteration. When the condition evaluates to false, the block is not executed. Control passes to the next statement after the block. 9 Logic of a while loop block true false condition evaluated
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
FIT1002 Computer Programming 2010 Module 5 Repetition 10 The while loop • An example of a while loop int count = 1; while (count <= 5) { System.out.println (count); count++; } If the condition of a while loop is false initially, the statement is never executed Therefore, the body of a while
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

Lec 05 Repetition - FIT1002 Computer Programming 2010...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online