Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.11 Assignment Operators 2.12 Increment and Decrement Operators.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
PD GMIT Modified from  2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
LOOPING do { } while ( ) for (I = 0; I < 5; I++) { } while ( ) { }
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2003 Prentice Hall, Inc. All rights reserved. 1 Input / Output Input/output –cin Standard input stream Normally keyboard –cout Standard output stream.
 2003 Prentice Hall, Inc. All rights reserved. 1 Algorithms Computing problems –Solved by executing a series of actions in a specific order Algorithm.
Control Structures in C++ while, do/while, for switch, break, continue.
1 Review C++_ the unit of programming is the class from which objects are eventually instantiated. C++ classes contain functions that implement class class.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline Assignment Operators Increment and Decrement Operators Essentials of Counter-Controlled.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
1 Lecture 3 Control Structures else/if and while.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 05 (Part II) Control Statements: Part II.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 – C Program Control
Chapter 2 - Control Structures
Control Structures Sequential execution Transfer of control
while Repetition Structure
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Chapter 2 - Control Structures
Structured Program
Chapter 2.2 Control Structures (Iteration)
Chapter 6 Control Statements: Part 2
Chapter 2 - Control Structures
Chapter 2 - Control Structures
2.6 The if/else Selection Structure
EPSII 59:006 Spring 2004.
Chapter 2 - Control Structures
Presentation transcript:

Loop

 While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop

Definition General format Example Flow chart Counter control repetition Event control repetition While Loop While Loop

While loop  Action repeated while some condition remains true  Psuedocode while there are more items on my shopping list Purchase next item and cross it off my list  while loop repeated until condition becomes false

General format when using while loops initialization; While( LoopContinuationTest){ statement; increment; } Example int product = 2; while ( product <= 1000 ) product = 2 * product;

product <= 1000 product = 2 * product true false Flowchart of while loop

Counter-controlled repetition  Loop repeated until counter reaches certain value  Number of repetitions known  Example A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz.

1 2 // Class average program with counter-controlled repetition. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 // function main begins program execution 10 int main() 11 { 12 int total; // sum of grades input by user 13 int gradeCounter; // number of grade to be entered next 14 int grade; // grade value 15 int average; // average of grades // initialization phase 18 total = 0; // initialize total 19 gradeCounter = 1; // initialize loop counter 20

21 // processing phase 22 while ( gradeCounter <= 10 ) { // loop 10 times 23 cout << "Enter grade: "; // prompt for input 24 cin >> grade; // read grade from user 25 total = total + grade; // add grade to total 26 gradeCounter = gradeCounter + 1; // increment counter 27 } // termination phase 30 average = total / 10; // integer division // display result 33 cout << "Class average is " << average << endl; return 0; // indicate program ended successfully } // end function main The counter gets incremented each time the loop executes. Eventually, the counter causes the loop to end. Enter grade: 98 Enter grade: 76 Enter grade: 71 Enter grade: 87 Enter grade: 83 Enter grade: 90 Enter grade: 57 Enter grade: 79 Enter grade: 82 Enter grade: 94 Class average is 81

 Counter-controlled repetition requires Name of control variable/loop counter Initial value of control variable Declares counter to be an integer int counter = 1; Names counter Sets counter to an initial value of 1 Condition to test for final value Increment/decrement to modify control variable when looping

Event-controlled repetition Event-controlled repetition  Suppose problem becomes: Develop a class-averaging program that will process an arbitrary number of grades each time the program is run Unknown number of students How will program know when to end?  Event Indicates “end of data entry” Loop ends when special input it cannot be confused with regular input -1 in this case

1 2 // Using the continue statement in a for structure. 3 #include 4 #include 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 ifstream fp(“data2.txt”); // open the file “data.txt” 12 int num; 13 float grade, total=0; 14 String ssn; While ( !fp.eof()){ 17 fp>>ssn >>grade; 18 total = total+ grade; // sum of grades 19 num++; 20 } // end for structure while cout << num << “students with average grades: " << total/num 23 << endl; 24 return 0; // indicate successful termination 25 } Read all the information inside the file until the read mark reaches the end of file.

9 students with average grades: Data2.txt

General format Example Do-While Loop Do-While Loop

General format when using do- while loops initialization; do{ statement; increment; }while( LoopContinuationTest); Example int product = 2; do { product = 2 * product; } while ( product <= 1000 );

General format Example Re-written Initialization and increment for multiple variables Applications For Loop For Loop

No semicolon after last statement General format when using for loops for ( initialization; LoopContinuationTest; increment ) statement Example for( int counter = 1; counter <= 10; counter++ ) cout << counter << endl;  Prints integers from one to ten for Repetition Structure for Repetition Structure

1 2 // Counter-controlled repetition with the for structure. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 // Initialization, repetition condition and incrementing 12 // are all included in the for structure header for ( int counter = 1; counter <= 10; counter++ ) 15 cout << counter << endl; return 0; // indicate successful termination } // end function main

for loops can usually be rewritten as while loops initialization; while ( loopContinuationTest){ statement increment; } Initialization and increment for multiple variables, use comma-separated lists for (int i = 0, j = 0; j + i <= 10; j++, i++) cout << j + i << endl;

1 2 // Summation with for. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 int sum = 0; // initialize sum // sum even integers from 2 through for ( int number = 2; number <= 100; number += 2 ) 15 sum += number; // add number to sum cout << "Sum is " << sum << endl; // output sum 18 return 0; // successful termination } // end function main Sum is 2550

Program to calculate compound interest  A person invests $ in a savings account yielding 5 percent interest. Assuming that all interest is left on deposit in the account, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula for determining these amounts: a = p(1+r) n  p is the original amount invested (i.e., the principal), r is the annual interest rate, n is the number of years and a is the amount on deposit at the end of the nth year

1 2 // Calculating compound interest. 3 #include 4 5 using std::cout; 6 using std::endl; 7 using std::ios; 8 using std::fixed; 9 10 #include using std::setw; 13 using std::setprecision; #include // enables program to use function pow // function main begins program execution 18 int main() 19 { 20 double amount; // amount on deposit 21 double principal = ; // starting principal 22 double rate =.05; // interest rate 23 header needed for the pow (program will not compile without it).

24 // output table column heads 25 cout << "Year" << setw( 21 ) << "Amount on deposit" << endl; // set floating-point number format 28 cout.self(ios::fixed,ios::floatfield); 29 cout.self(ios::showpoint); 30 cout << setprecision( 2 ); // calculate amount on deposit for each of ten years 33 for ( int year = 1; year <= 10; year++ ) { // calculate new amount for specified year 36 amount = principal * pow( rate, year ); // output one table row 39 cout << setw( 4 ) << year 40 << setw( 21 ) << amount << endl; } // end for return 0; // indicate successful termination } // end function main pow(x,y) = x raised to the yth power. Sets the field width to at least 21 characters. If output less than 21, it is right-justified.

Year Amount on deposit Numbers are right-justified due to setw statements (at positions 4 and 21).

continue statement  Used in while, for, do/while  Skips remainder of loop body  Proceeds with next iteration of loop while and do/while structure  Loop-continuation test evaluated immediately after the continue statement for structure  Increment expression executed  Next, loop-continuation test evaluated Continue Statement Continue Statement

1 2 // Using the continue statement in a for structure. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 // loop 10 times 12 for ( int x = 1; x <= 10; x++ ) { // if x is 5, continue with next iteration of loop 15 if ( x == 5 ) 16 continue; // skip remaining code in loop body cout << x << " "; // display value of x } // end for structure cout << "\nUsed continue to skip printing the value 5" 23 << endl; return 0; // indicate successful termination Skips to next iteration of the loop.

26 27 } // end function main Used continue to skip printing the value 5

While, do-while, and for loop Counter-control and event-control Many loops have three phases  Initialization Initialize the program variables  Processing Input data, adjusts program variables  Termination Evaluate the condition and stop loop if necessary Conclusion Conclusion