For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.

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

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
If Statements & Relational Operators 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.
Computer Science 1620 Loops.
Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from.
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?
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS150 Introduction to Computer Science 1
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.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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.
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
CONTROLLING PROGRAM FLOW
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
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.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
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.
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.
CS101 Computer Programming I Chapter 4 Extra Examples.
Overview Go over parts of quiz? Another iteration structure for loop.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Fundamental Programming Fundamental Programming More on Repetition.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
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.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Computer Programming -1-
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Sentinel Loops. while Syntax while(expression) statement.
Programming Loops (continued).
C++ Iterative Constructs
while Repetition Structure
CS161 Introduction to Computer Science
for Repetition Structures
Branching Constructs Review
Programming Fundamentals
Conditinoal Constructs Review
Repetition Statements
For & do/while Loops.
Intro to Programming Week # 4 Control Structure Lecture # 8
Introduction to Programming
Alternate Version of STARTING OUT WITH C++ 4th Edition
Conditional Construct
Loops (iterations, repetitions)
Unary Operators ++ and --
Conditinoal Constructs Review
Counting Loops.
Miscellaneous Flow Control
Repetition Control Structure
If Statements.
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Repetition Statements (Loops) - 2
Presentation transcript:

for Loops Programming

COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update

COMP102 Prog Fundamentals I: for Loops/Slide 3 The for Statement l Syntax for (initialization; condition; update) action l How it works: n execute initialization statement n while condition is true –execute action –execute update l Example: int i; for(i=1; i<=20; i++) cout << "i is " << i << endl;

COMP102 Prog Fundamentals I: for Loops/Slide 4 N! int number, factorial, n; cout > number; factorial = 1; for(n=1; n<=number; n++) factorial *= n; cout << "The factorial of " << number << " is " << factorial << endl;

COMP102 Prog Fundamentals I: for Loops/Slide 5 2N2N int number, result, n; cout > number; result = 1; for(n=1; n<=number; n++) result *= 2; cout << "Two raised to the " << number << " power is " << result << endl;

int main() { int listSize, n; int value; double sum = 0; double average; cout << "How many input numbers? " << endl; cin >> listSize; cout << " Please enter the " << listSize << " input numbers: " << endl; for (n=listSize; n > 0; n--) { cin >> value; sum += value; } if (listSize >0) { average = sum / listSize; cout << "Average: " << average << endl; } return 0;}

COMP102 Prog Fundamentals I: for Loops/Slide 7 Loops (Iteration) l Make sure there is a statement that will eventually stop the loop. à INFINITE LOOP == loop that never stops l Make sure to initialize loop counters correctly. à OFF-BY-ONE == the number of times that a loop is executed is one too many or one too few. l Have a clear purpose for the loop.

COMP102 Prog Fundamentals I: for Loops/Slide 8 Increment and Decrement l C++ has special operators for incrementing (++) and decrementing (--) an integer by one. l The ++ operator functions as follows: n ++k : increments the value of k by one and the incremented value is used in the expression. n Example: int k = 1, j; j =++k;//First: k increased by 1, becomes 2, // Second: j is assigned to 2 k = 3; cout << ++k; // First: k is increased by 1, becomes 4, // Second: 4 outputs to the screen

COMP102 Prog Fundamentals I: for Loops/Slide 9 Increment and Decrement k++ uses the initial value of k in the expression and increments afterwards. Example: int k = 1, j; j = k++; // First: j is assigned to 1 //Second: k is increased by 1, becomes 2 k = 3; cout << k++; //First:3 printed on the screen // Second:k is increased by 1, becomes 4,

COMP102 Prog Fundamentals I: for Loops/Slide 10 Increment and Decrement The -- operator functions as follows: n --k decrements the value of k by one and the decremented value is used in the expression. Example: int k = 1, j; j =--k; // First: k decreased by 1, become 0, // Second: j is assigned to 0 k = 3; cout << --k; // First: k is decreased by 1, becomes 2, // Second: 2 is output to the screen

COMP102 Prog Fundamentals I: for Loops/Slide 11 Increment and Decrement k-- uses the initial value of k in the expression and decrements afterwards. Example: int k = 1, j; j = k--; // first: j is assigned to 1 // second: k decreased by 1, becomes 0, k = 3; cout << k--; //First: 3 output to the screen //Second: k is decreased by 1, becomes 2

COMP102 Prog Fundamentals I: for Loops/Slide 12 Increment and Decrement l Examples: int k = 4; cout << k << endl; cout << ++k << endl; // k=k+1 : k is 5 cout << k++ << endl; // k=k+1 : k is 6 int i = k++; // i is 6, k is 7 cout << i << " " << k << endl; int j = ++k; // j is 8, k is 8 cout << j << " " << k << endl;

COMP102 Prog Fundamentals I: for Loops/Slide 13 Increment and Decrement l Examples: int k = 4; cout << k << endl; cout << --k << endl; // k=k-1 : k is 3 cout << k-- << endl; // k=k-1 : k is 2 int i = k--; // i is 2, k is 1 cout << i << " " << k << endl; int j = --k; // j is 0, k is 0 cout << j << " " << k << endl;