Chapter 4 Loops Write code that prints out the numbers 1- 100. Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
Sahar Mosleh California State University San MarcosPage 1 While Loop and For Loop.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other 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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
CONTROLLING PROGRAM FLOW
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
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.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
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.
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.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
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.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
Chapter 6. Loops A control structure that causes a statement or group of statements to be executed repeatedly There are 3 types of loops –while –for –do.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Chapter 3 Selection Statements
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Control Structures II (Repetition)
Chapter 5: Loops and Files.
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Iteration with While You can say that again.
For & do/while Loops.
Loops A loop is a repetition control structure.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Chapter 6: Repetition Statements
Control Structures Part 1
Alternate Version of STARTING OUT WITH C++ 4th Edition
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

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 doing this: while loops, do-while loops, and for loops. while Loop - Syntax while (condition) { stmt;//Loop body } Symantics: 1. Evaluate the condition. 2. If condition is true execute body of loop go to step 1 3. If condition is false the loop terminates and the next statement after the while loop is executed

int a = 1; while (a <= 100) { cout << a << endl; a = a + 1; } //while cout << “Finished”; What will happen if the statement a = a + 1 is deleted? There are several different ways to increment a value. a = a + 1; a += 1; a++; ++a; For looping  a++;

Incrementing values num = num + 1; num++; //postincrement ++num; //preincrement See Page 48 for the difference between postincrement and preincrement. Decrementing values num = num – 1; num--; --num; Specialized Assignment equivalent statement num += 4;num = num + 4; num -= 4;num = num – 4; num *= 4;num = num * 4; num /= 4;num = num / 4; num %= 4;num = num % 4;

int p = 1; while (p < 50) { cout << p << endl; p = p * 2; } //while cout << “p = “ << p; Write a while loop to print out ALL the odd numbers from 15 to 1 Write a while loop that adds up the numbers from 1 to 100, inclusive, then prints out only the answer. int sum = 0; int count = 1; while (count <= 100) { sum = sum + count; count++; }// while cout << “Sum = “ << sum; cout << “Count = “ << count;

cond can be any legal Boolean expression variables in the cond must have a value prior to entering the loop BE SURE TO MODIFY THE LOOP CONTROL VALUE INSIDE THE LOOP BODY!!!! a = 1; while (a < 10) cout << a; cout << “done”; ============================== a = 1; while (a < 10) { cout << a; a--; } //while cout << “done”; Be sure that the loop control variable does eventually reach the terminating condition!!!

n = 1; m = 10; while (n < m) { cout << “m =“ << m << “ n =“ << n; m--; n++; } //while ================================ int votes = 0; bool go = true; while(go) { cout << "Enter number of votes: "; int n; cin >> n; go = (n > 0); votes = votes + n; } cout << "Total votes = " + votes;

Checking for valid input! cout << “Input a number between 1 and 10”; cin >> num; while ((num 10)) { cout << “Invalid. Try again”; cin >> num ; }//while cout << “Thank You!”;

do-while loop The do-while loop is very similar to the while loop, except that the test is made at the bottom of the loop. do { statement; } while (cond); nextStatement; Execute the statement/body. Check cond – if true, execute body and recheck otherwise, go on to nextStatement int i = 1; do { cout << “i = “ << i << endl; i++; } //do while (i < 10);

Checking for invalid input with a do-while do { cout << “Enter a number between 0 and 10”; cin >> num; if (num 10) cout << “Invalid Input << endl; } while (num 10); The loop will execute at least one time, but will continue to execute as long as the input does not meet the condition.

For Loops int i; for(i = 1; i <= 100; i = i + 1) cout << i; Syntax: for (init_action; condition; update_action) stmt; next_stmt; where stmt can be a compound statement. Symantics: 1. Execute initializing action. 2. Evaluate the condition 3. If the condition is true then execute body of loop; Else exit loop 4. Execute update action 5. Repeat from step 2.

int sum = 0; for (y=1; y <=10; y++) sum += y; cout << sum; =================================== int count = 0; for (int index = 3; index < 8; index++) { cout << index << “ “ << index * index; count++; } //for index cout << count; =================================== What will happen if we add the following statement at the end of the above code fragment? cout << index; =================================== cout << “before loop”; int x = 5; for (j=x; j<=4; j++) cout << j; cout << “After loop ”;

for (x = 1; x <= 5; x++) cout << “hello”; cout << “bye”; ==================================== int fact = 1; cout << “Enter a number”; cin >> n; for (f = 1; f <= n; f++) fact = fact * f; cout << fact; ==================================== /* BE CAREFUL */ for (x = 1; x <= 10; x++); cout << “x = “ << x << endl; ==================================== for (m = 10; m > 0; m--) cout << m;

Nested Loops for (a = 1; a < 4; a++) for (b = 1; b < 3; b++) cout << “a= “ << a << “ b= “ << b << endl; for (a = 1; a < 4; a++) for (b = 1; b < a; b++) cout << “a= “ << a << “ b= “ << b << endl; for (x = 1; x < 9; x += 2) { cout << “x = “ + x); for (y = x; y < 10; y += x) cout << “ y = “ << y << endl; } // for x

Write a loop that prints out the odd numbers between 1 and 30, inclusive. (Do it using all 3 constructs; while loop, do-while loop and for loop) total = 1; for (j=1; j <= 5; j++) { total *= 2; cout << j << “ “; cout << total; } //for Rewrite it using while. Rewrite it using do-while.

What are the differences among for loops, while loops and do-while loops? What’s the minimum number of times the body of each type of loop can be executed? SKIP the notes at the end of section 4.4 SKIP sections 4.8 and 4.9 Review Questions: