Lecture 3 - Iteration - Lecture 3 Control Statements -...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 3 Control Statements - Iteration Iteration Iteration is the form of program control that allows us to repeat a section of code. For this reason this form of control is often also referred to as repetition . The programming structure that is used to control this repetition is often called a loop . There are three types of loops in Java: for loop; while loop; dowhile loop. Loops A loop is a general term for describing language constructs which provide for repetition In general, there are 2 different types of loops: z loops where you know in advance how many times (iterations) you want to repeat statement(s) z loops where you dont know in advance how many iterations you will make z The for loop implements the first of these When to use a loop? Display a square of stars (five by five) on the screen as follows: * * * * * * * * * * * * * * * * * * * * * * * * * This could be achieved with five output statements executed in sequence. Better to repeat one output statement five times with a loop. for( /*initialisation*/ ; /*while condition*/ ; /*action*/ ) { // instruction(s) to be repeated go here } set a counter to some initial value (often zero or one) condition under which the loop may continue changes the counter value each time round the loop The for loop Note that the initialisation may be dropped if the counter is set outside the loop and that the action may be dropped if it is performed elsewhere However, in almost all cases they appear. The for loop: flowchart Statement before loop initialisation Statement(s) in loop modification Boolean expression Statement after loop true false initialisation executed Test of while condition action Using a for loop to display a 5x5 square of stars A for loop example - walkthrough The initialisation is carried out before the loop is properly entered In the example the statement i = 1 is executed for the one and only time The while condition is now checked, if this expression is true, the statements inside the braces are executed In the example the condition i <= 5 evaluates to true A for loop example - walkthrough The statement in the loop is executed, so a line ***** is printed The final part of the loop is that the action is now executed In the example, the action is i = i + 1 , which results in i taking the value 2 The loop is re-entered and the while condition is again checked, i <= 5 is true, so the statement in the loop is executed resulting in ***** being printed The action is now taken so i becomes 3 A for loop example - walkthrough The loop is re-entered, i is 3, so i <= 5 is true, so a line of ***** results and i = i + 1 gives i = 4 The loop is re-entered, i <= 5 is true, so a line of ***** results and i = i + 1 gives i = 5 The loop is re-entered, i <= 5 is true, so a line of ***** results and i = i + 1 gives i = 6 The loop is re-entered, i <= 5 is false, the loop is exited and the next statement in the program is...
View Full Document

Page1 / 9

Lecture 3 - Iteration - Lecture 3 Control Statements -...

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

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