Loops (iterations, repetitions)

Slides:



Advertisements
Similar presentations
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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 5: Control Structures II (Repetition)
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Loops. COMP104 Loops / Slide 2 Shortcut Assignment * C++ has a set of operators for applying an operation to a variable and then storing the result back.
Chapter 5: Control Structures II (Repetition)
Structured Programming Programming. COMP102 Prog Fundamentals I: Structured Programming /Slide 2 Structured Programming l Structured programing is the.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
The If/Else Statement, Boolean Flags, and Menus Page 180
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Chapter 5: Control Structures II (Repetition)
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
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,
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.
Overview Go over parts of quiz? Another iteration structure for loop.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Unary Not operator !  !true = false  !false = true.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Programming Loops (continued).
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
Engineering Problem Solving with C++, Etter/Ingber
Branching Constructs Review
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Chapter 8 Repetition Statements
Repetition and Loop Statements
Conditinoal Constructs Review
For & do/while Loops.
Conditional Construct
Conditinoal Constructs Review
Miscellaneous Flow Control
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Summary Two basic concepts: variables and assignments Basic types:
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 5: Control Structures II (Repetition)
Repetition Statements (Loops) - 2
Programming Fundamental
Presentation transcript:

Loops (iterations, repetitions)

Three fundamental structures: sequence, condition, iteration

Iterative Constructs: iteration or loop A loop repeats a statement or a sequence of statements a number of times. The fundamental iteration construct: ‘while’ statement

The fundamental while Statement Syntax while (bool expression) { Action } How it works: If Expression is true then execute Action Repeat this process until Expression evaluates to false Action is either a single statement or a group of statements within braces Expression true false Action

The first example: int n; n=5; while (n < 10) { cout << n << endl; n=n+1; } n=5; (5<10); n=n+1; (6<10); n=n+1; (7<10); n=n+1; (8<10); n=n+1; (9<10); n=n+1; (10<10)  false; finish; 10;

N! (while) int number, factorial, n; cout << "Enter number: "; cin >> number; factorial = 1; n = 1; while (n <= number) { factorial = factorial*n; n=n+1; } cout << "The factorial of " << number << " is " << factorial << endl;

2N (while) int number, result, n; cout << "Enter number: "; cin >> number; result = 1; n = 1; while (n <= number) { result = result*2; n=n+1; } cout << "Two raised to the " << number << " power is " << result << endl;

while loop summary while (condition) action initialisation(V) while (condition(V)) action-and-update(V) Initialisation of a set of variables V Condition on V Action: V = f(V) init first action second action n=1,fac=1 fac=2,n=2 fac=6,n=3 fac=fac*n;n=n+1; fac=fac*n;n=n+1; fac=fac*n;n=n+1;

GCD (the greatest comon divisor) example GCD(a,b) is the largest number that divides both a and b without leaving a remainder GCD(25,0)=? GCD(12,18) =? GCD(5,7)=? GCD(a,b)=GCD(a-b,b) if a>b GCD(a,b-a) if b>a GCD(a,b)=GCD(b,a mod b) GCD(a,0)=a Euclid method  iterative

GCD with subtraction int a,b,gcd; cin >> a; cin >> b; if (a==0) gcd=b; else { while (b != 0) { if (a>b) a = a-b; else b = b-a; } gcd=a; cout << “GCD is:” << gcd << endl;

GCD with the remainder cin >> a; cin >> b; while (b != 0) { t = b; b = a % b; a = t; } gcd=a; cout << “GCD is:” << gcd << endl;

Key points of a loop: Make sure there is a statement that will eventually stop the loop (condition) Make sure to initialize loop counters correctly (initialisation) Have a clear purpose for the loop (action)

How to Stop a Loop Known number of iterations before the loop stops Test for a user-controlled condition before or after each iteration

Common Loop Errors while(balance != 0.0) { balance = balance - amount; } This will lead to an infinite loop! balance may not become equal zero due to numerical inaccuracies while(balance != 0.0);

Common Loop Errors product=? while(n <= 1000){ product = product*n; n=n+1; } sum=? while(n <= 1000) { sum = sum+n; Be sure to initialize to 0 a variable used for sums Be sure to initialize to 1 a variable used for products

Nested Loops Nested loops are loops within loops. They are similar in principle to nested if-else statements. Many applications require nested loops.

Multiplication Table (while) // Program to output the // multiplication table int i; //Outer loop counter int j; //Inner loop counter i=1; while (i<=10) { j=1; while (j<=10) { cout << i*j << “ “; j=j+1; } cout << endl; i=i+1;

Diamond Pattern Print out the following diamond pattern * * * * * * * * * * * * * * * * * * * * * * * * *

Diamond Pattern Subproblem: print out the upper half print out the lower half

Upper triangular Pattern Print out triangular pattern: row 1: print 4 spaces, 1 star; row 2: print 3 spaces, 3 stars; row 3: print 2 spaces, 5 stars; row 4: print 1 space, 7 stars; row 5: print 0 spaces, 9 stars; Algorithm Refinement: row 1: print (5-row) spaces, (2*row - 1) stars; row 2: print (5-row) spaces, (2*row - 1) stars; row 3: print (5-row) spaces, (2*row - 1) stars; row 4: print (5-row) spaces, (2*row - 1) stars; row 5: print (5-row) spaces, (2*row - 1) stars; * * * * * * * * * * * * * * * * * * * * * * * * *

Upper Triangular Pattern int row, space, star; row=1; while(row<=5){ space=1; while(space<=5-row) { cout << " "; space=space+1; } star=1; while(star<=2*row-1) { cout << "*"; star=star+1; cout << endl ; row=row+1;

Diamond Pattern Subproblem: Print out lower half: print out the upper half print out the lower half Print out lower half: Row 6 (row 4): print 1 spaces, 7 stars; Row 7 (row 3): print 2 spaces, 5 stars; Row 8 (row 2): print 3 space, 3 stars; Row 9 (row 1): print 4 spaces, 1 stars; Algorithm Refinement: row 4: print (5-row) spaces, (2*row - 1) stars; row 3: print (5-row) spaces, (2*row - 1) stars; row 2: print (5-row) spaces, (2*row - 1) stars; row 1: print (5-row) spaces, (2*row - 1) stars;

Diamond Pattern int row, space, star; … // top half while(row>=1){ //bottom half space=1; while(space<=5-row) { cout << " "; space=space+1; } star=1; while(star<=2*row-1) { cout << "*"; star=star+1; cout << endl ; row=row-1;