FOR LOOPS.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
CS150 Introduction to Computer Science 1
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Overview Go over parts of quiz? Another iteration structure for loop.
Control of flow We learned that default flow of instructions is sequential. Then, we learned how to control the flow using "if" and "switch." Now, we will.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Program Development and Design Using C++, Third Edition
Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k
Computer Programming -1-
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Basic concepts of C++ Presented by Prof. Satyajit De
Topic 6 Recursion.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
while Repetition Structure
Repetition Control Structure in C++ Program
Unit 3 Lesson 9 Repetition Statements (Loops)
CS161 Introduction to Computer Science
Lecture 7: Repeating a Known Number of Times
GC211Data Structure Lecture2 Sara Alhajjam.
Java 4/4/2017 Recursion.
Variables A piece of memory set aside to store data
Engineering Problem Solving with C++, Etter/Ingber
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Programming Fundamentals
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 07 More Repetition Richard Gesick.
Computer Science 101 While Statement.
Lecture 4B More Repetition Richard Gesick
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 4 Repetition Structures
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Outline Altering flow of control Boolean expressions
Counting Loops.
1) C program development 2) Selection structure
3 Control Statements:.
Repetition Control Structure
Unit 3 Test: Friday.
Chapter 6: Repetition Statements
Computing Fundamentals
Let’s all Repeat Together
Fundamental Programming
Repetition Statements (Loops) - 2
Review of Previous Lesson
Class rational part2.
Presentation transcript:

FOR LOOPS

for (initialize counter ; test counter ; update counter ) For Loops Structure for (initialize counter ; test counter ; update counter ) statement; for (initialize counter ; test counter ; update counter ) { statement; } The for loop structure starts with the keyword for then between brackets you have your header where you initialize your counter, have your termination condition, and the update of the counter

Factorial with while loop /***** factorial **************************************** * @param: num - a positve integer: number > 0 * * @returns: the factorial of num ***********************************************************/ long factorial(int num){ long result= 1; int i=1; while (i <= num) { result *= i; i++; } return result; }

Factorial /** factorial ************************************************** * * @param: num @pre: num must be a non-negative integer * * @returns: the factorial of num ****************************************************************/ long factorial(int num){ long result= 1; for (int i = 1; i <= num; i++) result *=i; return result; } long factorial(int num){ long result= 1; int i=1; while (i <= num) { result *= i; i++; } return result; }

for (i = 1; i <= num; i++) fact *= i;

* * * 3 3 2 1 3 False True True True Execution Example int finalValue; cin >> finalValue; for (int counter = 0; counter < finalValue; counter++) cout << "*"; False True True True Display Screen 3 finalValue counter 3 2 1 3 * * *

Common Errors How many times this loop will execute Logical Error Print all numbers between 50 and 60 inclusive for(int i = 50 ; i < 60 ; i++) { cout<< i << endl; } Off-By-One Logical Error for(int x=0; x > -1; x++) { //do something } Infinite-Loop

Common Errors What is the output of the following code: Syntax Error int n; cin>>n; for(int x=0; x<n; x++) cout<< “*”; cout<<“the loop repeated”<<x+1; Variables Scope Logical Error for(int x=0; x<3; x++) ; cout<< “*”; Null statement int new_term=0; int sum=0; for(int x=0; x<3; x++) new_term=x*x; sum+= new_term; cout<<“the sum is:”<<sum; Logical Error Body Scope

Example: Fibonacci Numbers The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. Complete the Pseudo code below then write it as a C++ function /** fibonacci ************************************************* * @params: n - an integer @pre: n >= 2 * @returns: the n'th fibonacci number ****************************************************************/ int fibonacci(int n) define F0 define F1 let Flast be F1 that is 1 let F2ndlast be F0 that is 0 for i = 2 to n Fi = Flast + F2ndlast F2ndlast = Flast Flast = Fi

/. fibonacci. @params: n - an integer @pre: n >= 2 /** fibonacci ************************************************* * * @params: n - an integer @pre: n >= 2 * * @returns: the n'th fibonacci number ****************************************************************/ int fibonacci(int n) { } int fLast = 1; int f2Last = 0; int fi; for (int i = 2; i <= n; i++) { fi = fLast + f2Last; f2Last = fLast; fLast = fi; } return fi;

Example 2: Definite integral The definite integral of f(x) from x = a to x = b can be approximated by n trapezoids, each of width deltaX = (b-a)/n. The i'th trapezoid has a left edge height f(Xleft) and a right edge height of f(Xright). Write a function for computing the integral with arguments a, b and n, assuming the function f(x) has been declared and written (you don't need to know what f(x) is- it can be anything, e.g. sin(x) ). You may assume b > a. The area of a trapezoid is computed by ((Xleft + Xright) / 2 )* T. Give an implementation for the function using the algorithm described above. double integrate(double a, double b, int n);

Example of f(x) can be a sin(x) You can assume that this is already implemented for you and you can call it directly //This is an example of what f(x) might be double f(double x) { return sin(x); }

Solution double integrate(double a, double b, int n) { double area = 0; double T = (b - a) / n; double xleft = a; double xright = xleft + T; for (int i = 0; i<n; i++) { area += ( (f(xleft) + f(xright)) / 2) * T; xleft = xright; xright = xleft + T; } return area;