1 10/9/06CS150 Introduction to Computer Science 1 for Loops.

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

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.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
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/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
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
CS 1400 Chapter 5, section 2. Loops! while (rel-expression)while (rel-expression) {statements statement } if the relational-expression is true, execute.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
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
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
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.
Previously Repetition Structures While, Do-While, For.
First steps Jordi Cortadella Department of Computer Science.
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/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
Count and add list of numbers From user input and from file.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Overview Go over parts of quiz? Another iteration structure for loop.
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.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CS 240 Computer Programming 1
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161.
Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Sentinel Loops. while Syntax while(expression) statement.
Introduction to Computer Programming
C++ Programming: CS150 For.
for Repetition Structures
For loops, nested loops and scopes
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Conditional Construct
Unary Operators ++ and --
Counting Loops.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
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
CS150 Introduction to Computer Science 1
do/while Selection Structure
CS150 Introduction to Computer Science 1
Repetition Statements (Loops) - 2
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
Presentation transcript:

1 10/9/06CS150 Introduction to Computer Science 1 for Loops

2 10/9/06CS150 Introduction to Computer Science 1 for loops (5.8)  3 main things for loops: o Initialization of counter, testing of counter, updating counter  for loops provide a concise way to do this for (count = 0; count < 5; count++) { cout << count << endl; }

3 10/9/06CS150 Introduction to Computer Science 1  This for loop for (count = 0; count < 5; count++) { cout << count << endl; }  is equivalent to count = 0; while(count < 5) { cout << count << endl; count ++; }

4 10/9/06CS150 Introduction to Computer Science 1 General Format for (initialization expression; loop repetition condition; update expression) { statements; }

5 10/9/06CS150 Introduction to Computer Science Example  Write a for loop that outputs odd numbers less than 10

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

7 10/9/06CS150 Introduction to Computer Science 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

8 10/9/06CS150 Introduction to Computer Science Practice  Rewrite the following for loop as a while loop for (i = 5; i < 10; i+= 2) { cout << i; }  What does this output?

9 10/9/06CS150 Introduction to Computer Science 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/9/06CS150 Introduction to Computer Science Potential Pitfalls  What is the output of the following loop for (count = 0; count < 5; count++) { cout << count << endl; count++; }

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

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