Iteration Hussein Suleman UCT Dept of Computer Science CS115 ~ 2004.

Slides:



Advertisements
Similar presentations
Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
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.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
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.
Chapter 5: Control Structures II (Repetition)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
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
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
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.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
UCT Department of Computer Science Computer Science 1015F Iteration
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
REPETITION CONTROL STRUCTURE
Lecture 7: Repeating a Known Number of Times
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 2.2 Control Structures (Iteration)
Outline Altering flow of control Boolean expressions
Chapter 2.2 Control Structures (Iteration)
Chap 7. Advanced Control Statements in Java
Presentation transcript:

Iteration Hussein Suleman UCT Dept of Computer Science CS115 ~ 2004

UCT-CS Problem Output the 7x table.

UCT-CS What is Iteration? Executing the same task or set of statements multiple times  e.g., print the 7x table (from 1 to 12) n <= 12? Assign n=1 Output (7*n) no yes LOOP n++

UCT-CS Counter-controlled Loops Loops that usually 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

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

UCT-CS 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...

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

UCT-CS 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)

UCT-CS Problem revisited Output the n x table for any integer value of n. Encapsulate this functionality into a class, with a method called printNTimesTable, taking n as a parameter.

UCT-CS Solution? class Kiddies { public void printNTimesTable ( int n ) { for ( int i=1; i<=12; i++ ) System.out.println (i+" x "+n+" = "+(n*i)); }

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

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

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

UCT-CS 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 I=1; I<=10; I++ ) for ( int j=1; j<=10; j++ )

UCT-CS 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: * ** *** ****

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

UCT-CS 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 ( ) { }  do { } while ( )

UCT-CS “while” Example int sum = 0; int num = Keyboard.readInt (“Enter a no: “); while (num != 0) { sum = sum + num; num = Keyboard.readInt (“Enter a no: “); } Condition? Statements false true LOOP

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

UCT-CS 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.

UCT-CS 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:

UCT-CS Menu Example Menu souper = new Menu (); souper.print (); // output options int choice = Keyboard.readInt (); // 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"); } souper.print (); // output options choice = Keyboard.readInt (); // get selection }

UCT-CS “do..while” statement 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 true LOOP

UCT-CS “do..while” Example Menu souper = new Menu (); int choice; do { souper.print (); // output options choice = Keyboard.readInt (); // 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

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

UCT-CS 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; ) { … }

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

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

UCT-CS 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”

UCT-CS 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”

UCT-CS Intro to Numerical Methods Floating-point numbers cannot have an infinite number of decimal places, hence are not always accurate For real calculations, check for approximate equality instead of equality Example: if (num == 1.0) // not always a good idea float Epsilon = 0.005; if (Math.abs (num-1.0) < Epsilon) // better?

UCT-CS Bisection Algorithm If a<b and f(a)*f(b)<0, then f(x) has a root in the range a<=x<=b (for continuous f) Bisection method:  Find the midpoint of a and b  Halve the interval by choosing the one where the root appears  Continue until the interval is small or f(midpoint) is suitably close to 0

UCT-CS Problem Find a root of the non-quadratic equation:  x 7 + 6x 6 - 3x 5 + 4x 2 - x - 6 Hint: Use the bisection algorithm.