UCT Department of Computer Science Computer Science 1015F Iteration

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

Computer Science 1620 Loops.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
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.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 5 Loops.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Chapter 4: Control Structures II
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Iteration Hussein Suleman UCT Dept of Computer Science CS115 ~ 2004.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Selection UCT Department of Computer Science Computer Science 1015F Hussein Suleman March 2009.
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
Lecture 7: Repeating a Known Number of Times
Chapter 5: Control Structures II
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Repetition-Sentinel,Flag Loop/Do_While
Chapter 2.2 Control Structures (Iteration)
Chapter 5: Control Structures II
Arrays, For loop While loop Do while loop
Looping and Repetition
- Additional C Statements
Outline Altering flow of control Boolean expressions
Chapter 4 - Program Control
Control Statements Loops.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
CprE 185: Intro to Problem Solving (using C)
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Control Statements Loops.
PROGRAM FLOWCHART Iteration Statements.
More Loops Topics Counter-Controlled (Definite) Repetition
Chap 7. Advanced Control Statements in Java
CprE 185: Intro to Problem Solving (using C)
More Loops Topics Counter-Controlled (Definite) Repetition
Looping and Repetition
Control Statements:.
Presentation transcript:

Hussein Suleman <hussein@cs.uct.ac.za> March 2009 UCT Department of Computer Science Computer Science 1015F Iteration Hussein Suleman <hussein@cs.uct.ac.za> March 2009

Problem Output the 7x table.

What is Iteration? Executing the same basic task or set of statements multiple times. e.g., print the 7x table (from 1 to 12)

Counter-controlled Loops Counter-controlled loops execute for a fixed number of times. A special counter variable is used to control the loop and may be referred to within the loop. Java provides the “for” statement as a counter- controlled loop.

The “for” statement for ( initialisation statement; condition; increment statement ) { statements … }

Example Usage int n; for ( n=1; n<=12; n++ ) { System.out.println (n + “ x 7 = “ + (n*7)); } Output: 1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 ...

Flowchart vs Java int n; for ( n=1; n<=12; n++ ) { System.out.println (n + “ x 7 = “ + (n*7)); } n <= 12? Assign n=1 no yes n++ Output (7*n)

Additional “for” syntax We can define a variable in the initialisation section, which is local to the body of the loop. for ( int i=1; i<=10; i=i+1 ) Multiple comma-separated expressions can appear in the “increment” section, even decrements. for (int i=10; i>0; i--) for (int i=1,j=7; i<=12; i++,j+=7)

Problem revisited Output the n x table for any integer value of n.

Solution? for ( int i=1; i<=12; i++ ) System.out.println (i+" x "+n+" = "+(n*i));

General Semantics of “for” Condition? Initialisation false LOOP true Incr. Statements

Problem Find the product of the integers from 1..n, corresponding to n!.

Problem Calculate ab using a for loop, assuming that a is a float and b is an integer.

Nesting of statements for and if are both statements, therefore they can each appear within the statement body. for ( int i=1; i<=10; i++ ) { if (a<b) max=b; } if (a<b) { for (int i=1; i<=10; i++ ) ... } for ( int j=1; j<=10; j++ )

Nested loops Where a task is carried out multiple times and a subtask within that is carried out multiple times. Example: Draw a triangle of arbitrary height on the screen, such as: * ** *** ****

Problem * ** *** **** * *** ***** ******* **** *** ** * Write programs to generate (on the screen) the following triangles of user-specified height: * ** *** **** * *** ***** ******* **** *** ** *

Condition-controlled Loops If we do not know the number of iterations a priori (in advance), we can use a condition-controlled (or event- controlled) loop - where the loop executes while a condition is true Two statements: while (<condition>) { <statements> } do { <statements> } while (<condition>)

“while” Example LOOP int sum = 0; int num = input.nextInt (); while (num != 0) { sum = sum + num; num = input.nextInt (); } Condition? false LOOP true Statements

Problem Approximate the logarithm (with a base of 10) of an integer using repeated division.

Problem Approximate the logarithm (with a base of 10) of an integer using repeated division. Design a user interface where the user can continue to ask for logarithms until a value of 0 is supplied.

Menus A menu is a list of choices presented to the user, with the means to select one. Example: Souper Sandwich Menu 1. Chicken, cheese and chilli sauce 2. Chicken and cheese 3. Cheese 4. Exit Program Enter the sandwich number:

Menu Example int choice = input.nextInt (); // get selection while (choice != 4) // continue until exitted { System.out.println (); // leave a line switch (choice) // output ingredients case 1 : System.out.println ("Add chilli"); case 2 : System.out.println ("Add chicken"); case 3 : System.out.println ("Add cheese"); } choice = input.nextInt (); // get selection

“do..while” statement LOOP When the “loop body” is going to be executed at least once, we can check the condition after the loop (instead of before). Condition? Statements false LOOP true

“do..while” Example int choice; do { choice = input.nextInt (); // get selection System.out.println (); // leave a line switch (choice) // output ingredients { case 1 : System.out.println ("Add chilli"); case 2 : System.out.println ("Add chicken"); case 3 : System.out.println ("Add cheese"); } } while (choice != 4) // continue until exitted

Problem Find the reverse of an integer. For example, the reverse of the integer 12345 is 54321 and the reverse of 98 is 89. Use only integer manipulations - do not convert the number to a String.

Infinite Loops Loops where the condition is always true Example: while (true) { System.out.println (“Wheeee!”); } do { … } while (true); for ( int i=1; i<10; ) { … }

break exits immediately from a loop Example: int i = 0; while (true) { System.out.println (i); if (i == 10) break; }

continue immediately starts next iteration Example: for ( int i=0; i<=10; i++ ) { if (i % 3 == 0) continue; System.out.println (i); }

Selecting Loops General Rules: When you know the number of iterations, use a “for” When the iterations depend on a condition, use a “do..while” if the loop must execute at least once otherwise, use a “while”

Converting Loops How do we write the equivalent of “while” using “for” “do..while” using “for” “for” using “while” “do..while” using “while” “for” using “do..while” “while” using “do..while”