for Repetition Structures

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Chapter 04 (Part III) Control Statements: Part I.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
CS150 Introduction to Computer Science 1
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator.
Introduction to Computers and Programming Lecture 7:
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
Algorithm: procedure in terms of
Chapter 7: Expressions and Assignment Statements
Lecture 7: Repeating a Known Number of Times
Chapter 7: Expressions and Assignment Statements
Chapter 4 – Control Structures Part 1
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Unary Operators ++ and --
Lec 6.
Repetition Control Structure
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
switch Selection Structure
CS150 Introduction to Computer Science 1
2.6 The if/else Selection Structure
Functions Divide and Conquer
EPSII 59:006 Spring 2004.
Arithmetic Operations
do/while Selection Structure
CS150 Introduction to Computer Science 1
Life is Full of Alternatives
Life is Full of Alternatives
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Functions Divide and Conquer
EECE.2160 ECE Application Programming
CS150 Introduction to Computer Science 1
EECE.2160 ECE Application Programming
Programming Fundamental
Presentation transcript:

for Repetition Structures 09/27/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Last Time We Counter and sentinel-controlled repetitions Type casting Formatting output Top-down, stepwise refinement Today we will Examine different ways of writing assignments Learn about the increment and decrement operators Start looking at the for repetition structure 09/27/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Assignment Operators C++ provides the ability to abbreviate an assignment operator in which the same variable appears on either side of the operator sum = sum + num; Can be abbreviated to sum += num; 09/27/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Assignment Operators This abbreviation can be done to the following operators + - * / % Examples, where c = 3, e = 4 c += 7 e %= 2 c *= 3 e /= 4 e -= 1 09/27/04 CS150 Introduction to Computer Science 1

Increment and Decrement Operators ++ is the unary increment operator x++; is the same as x = x + 1; -- is the unary decrement operator x--; is the same as x = x - 1; 09/27/04 CS150 Introduction to Computer Science 1

Pre-increment vs. post-increment Pre Post k = --x; k =x--; k = ++x; k = x++; Increment/ Assign value of x to decrement x k, then increment then assign or decrement x value of x to k 09/27/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example What is the output if i = 2? cout << “Value of x is” << i; cout << “Value of i++ is” << i++; cout << “Value of ++i is” << ++i; cout << “Value of --i is” << --i; cout << “Value of i-- is” << i--; 09/27/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Operator Precedence () L->R Parentheses ++, --, static_cast<type>() L->R Unary ++, --, !, +, - R->L Negation, Unary *,/,% L->R Mult, div, mod +, - L->R Add, Subtract <<, >> L->R Insertion/extraction <, <=, >, >= L->R Relational ==, != L->R Equality && L->R And || L->R Or ?: R->L Conditional =, +=, -=, *=, /=, %= R->L Assignment 09/27/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 For loops 3 main things for loops: Initialization of lcv, testing of lcv, updating lcv For loops provide a concise way to do this for (count = 0; count < 5; count++) cout << count << endl; 09/27/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 General Format for (initialization expression; loop repetition condition; update expression) { statements; } 09/27/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Examples Write a for loop that outputs odd numbers less than 10 Write a program that computes the factorial of a number 09/27/04 CS150 Introduction to Computer Science 1

Localized Declarations for (int i = 0; i < n; i++) cout << i << endl; i is declared ONLY in the loop 09/27/04 CS150 Introduction to Computer Science 1

Rewrite using a while loop for (i = 5; i < 10; i+= 2) cout << i; What does this output? 09/27/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Write a program that will print the sum of the odd integers between 1 and 50 inclusive. Write one program using a while and the other using a for loop. 09/27/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Write a program that allows the user to enter an unknown number of integer values one at a time. When the user enters -999, you are to terminate the loop and print the following: The sum of all integers inputted The average of all integers inputted The largest integer of all integers inputted 09/27/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we covered Abbreviating assignment operators Increment and decrement operators for repetition structures Readings P. 98 Assignment operators P. 99 - 102 Increment and decrement operators P. 104 - 113 for repetition structures 09/27/04 CS150 Introduction to Computer Science 1