CS150 Introduction to Computer Science 1

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
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.
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.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Compound Operators 03/07/11. More Operators, First Section 4.4.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
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.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
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.
First steps Jordi Cortadella Department of Computer Science.
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.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
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.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
Count and add list of numbers From user input and from file.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Overview Go over parts of quiz? Another iteration structure for loop.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 CS161 Introduction to Computer Science Topic #6.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CS 240 Computer Programming 1
Introduction to Computer Programming
C++ Programming: CS150 For.
for Repetition Structures
Chapter 2 Assignment and Interactive Input
Chapter 5: Loops and Files.
Repetition-Counter control Loop
Programming Fundamentals
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Unary Operators ++ and --
Counting Loops.
CS150 Introduction to Computer Science 1
Suppose I want to add all the even integers from 1 to 100 (inclusive)
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
do/while Selection Structure
Repetition Statements (Loops) - 2
Presentation transcript:

CS150 Introduction to Computer Science 1 Loops Sections 5.1, 5.6 10/10/07 CS150 Introduction to Computer Science 1

Increment and Decrement Operators (5.1) C++ provides a shortcut to increment or decrement a variable by 1 int x = 99, y = 90; x++; // this is equivalent to x += 1 x--; // this is equivalent to x -= 1 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 In a Loop Often, this is used to increment a loop counter int x = 1; while( x < 5 ){ cout << “ x : “ << x << endl; x++; } 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Examples This can be used in an expression: y = x++ + 9; This is equivalent to: y = x + 9; x += 1; This can also be used in a conditional ( x-- > 9 ) is equivalent to: ( x > 9); x -= 1; 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Practice Write one statement of code to do each of the following: int x =0, y=1; Add x + 9 to y and increment x by 1 Add x * 4 to y and increment x by 1 Add y – 13 to x and decrement y by 1 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Prefix vs Postfix ++x is prefix The x += 1 happens before the expression is evaluated x++ is postfix the x += 1 happens after the expression is int y=0, x=0; x = y++ + 1; // x = y + 1; y += 1; y = ++x + 1; // x += 1; y = x + 1; 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Examples int x = 0, y = 0; x = y++ * 2; y = ++x / 2; x = x++ + 1; x = ++x + 1; y = (y+ x++) * 2; x = y++ + ++x; 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Practice Write a single C++ statement to do each of the following: int y = 0, x = 0, z = 0; Decrement x by 1 then add 2x to y Add 2y to x then increment y by 1 Subtract 9x – 1 from y then decrement x by 1 Increment y by 1 then add 8-2y to x Increment x and y each by 1 then add x+y to z 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 for loops (5.6) 3 main steps for loops: Initialize, Test, Update for loops provide a concise way to do this // initialize test update for (count = 0; count < 5; count++) { cout << count << endl; } 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 For vs While This for loop for (count = 0; count < 5; count++) { cout << count << endl; } is equivalent to count = 0; while(count < 5) count ++; // update happens at the end 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example Write a for loop that outputs odd numbers less than 10 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Practice What does this output? for (i = 5; i < 10; i+= 2) { cout << i; } Rewrite the for loop as a while loop 10/10/07 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 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Practice Write a program that computes the factorial of a number. The factorial of a number is given by the formula The user will input N N! = N*(N-1)*…*2*1 where 0!=1, 1!=1, 2!=2, 3!=6, … 10/10/07 CS150 Introduction to Computer Science 1

Localized Declarations for (int i = 0; i < n; i++) { cout << i << endl; } cout << i << endl; // This will cause an error i is declared ONLY in the loop Convert this to a while loop 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Potential Pitfalls What is the output of the following loop for (count = 0; count < 5; count++) { cout << count << endl; count++; } 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Practice What is the output of the following loop for (count = 0; count < 10; count += 2) { cout << count << endl; } 10/10/07 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Write a program that allows the user to enter 20 integers, you should then print out the following: The sum of all integers inputted The average of all integers inputted The largest integer of all integers inputted 10/10/07 CS150 Introduction to Computer Science 1