Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -

Slides:



Advertisements
Similar presentations
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.
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.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
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)
Control Structures in C++ while, do/while, for switch, break, continue.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Chapter 5: Control Structures II (Repetition)
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.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
For Repetition Structures (L13) * General Form of the for Statement * Components of a Typical for Header * Pre/Postincrement of the Counter * Stream Manipulator.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
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.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Review 1 Computers and Programming I Dr. Ming Zhang Tel: (757) Fax: (757) Office: Gosnold 217b Subject Overview.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For 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.
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
Chapter 4 – C Program Control
Control Structures II (Repetition)
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
- Additional C Statements
Chapter 2.2 Control Structures (Iteration)
2.6 The if/else Selection Structure
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
CS149D Elements of Computer Science
Repetition Statements (Loops) - 2
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop - Selection within a loop - Evaluating functions of one variable - Interactive loop control * Exercise/Home Work Dr. Ming Zhang

General Form of do/while Statement do statement(s); while (condition expression); The process continues until the condition expression evaluates to zero ( false) Dr. Ming Zhang

Flowchart of do/while Structure Enter do/while statement Execute the statement(s) after do exit the Loop false do Evaluate the statement expression true Dr. Ming Zhang

Example of do/while #include using std::cout; int main( ) { int counter = 1; do { cout << counter << “ “; } while (++counter <=10); return(0); } output: Dr. Ming Zhang

break Statement * The break statement, when executed in a while, for, do/while, or switch structure, causes immediate exit for that structure. * Program execution continues with the first statement after the structure. * Common uses of the break statement are to escape early from a loop, or to skip the remainder of a switch structure Dr. Ming Zhang

Example of break Statement #include using std::cout; int main( ) { int x; for ( x=1; x<=10; x++) { if ( x ==5) break; //broke out of loop at x of 5 cout << x << “ “; } return(0); } Output: Dr. Ming Zhang

Continue Statement * The continue statement, when executed in a while, for, or do/while structure, skips the remaining statements in the body of that structure, and proceeds with the next iteration of the loop. * In while and do/while structures, the loop- continuation test is evaluated immediately after the continue statement is executed. * In the for structure, the increment expression is executed, then the loop-continuation test is evaluated. Dr. Ming Zhang

Example of continue Statement #include using std::cout; int main( ) { int x=1; while ( x <=10) { if ( x ==5) //skip remaining code in continue; //loop only if x is 5 cout << x << “ “; x=x+1; } return(0);} Output: ??????????????????? Dr. Ming Zhang

Example of continue Statement #include using std::cout; int main( ) { int x=1; while ( x <=10) { if ( x ==5) //skip remaining code in continue; //loop only if x is 5 cout << x << “ “; x=x+1; } return(0);} Output: …… infinite loop Dr. Ming Zhang

Example of continue Statement #include using std::cout; int main( ) { int x=0; while ( ++x <=10) { if ( x ==5) //skip remaining code in continue; //loop only if x is 5 cout << x << “ “; } return(0);} Output: Dr. Ming Zhang

Example of continue Statement #include using std::cout; int main( ) { int x; for ( x=1; x<=10; x++) { if ( x ==5) //skip remaining code in continue; //loop only if x is 5 cout << x << “ “; } return(0); } Output: ???????????????? Dr. Ming Zhang

Example of continue Statement #include using std::cout; int main( ) { int x; for ( x=1; x<=10; x++) { if ( x ==5) //skip remaining code in continue; //loop only if x is 5 cout << x << “ “; } return(0); } Output: Dr. Ming Zhang

Interactive Input within a Loop #include #define MAX 5 using std::cout; using std::cin; int main( ) {int count, num; float total, average; for(total=0.0,count=1;count<=MAX;++count) { cout << “Enter a number:”; cin >> num; total = total + num;} average = total/MAX; cout << “ The average is” << average<<endl; return(0);} Dr. Ming Zhang

Selection within a Loop #include #define MAX 5 #define THR 8 using std::cout; using std::cin; int main( ) {int count, num; float total, average; for(total=0.0,count=1;count<=MAX;++count) { cout > num; if(num> THR) total = total + num; else total = total - num;} average = total/MAX; cout << “ The average is” << average<<endl; return(0);} Dr. Ming Zhang

Evaluating Functions of One Variable # include using std::cout; using std::endl; int main( ) { int x, y; for (x=2; x<=6; ++x){ y =10*x*x +3*x -2; //function of one variable cout << x << y<<endl; } return(0); } Dr. Ming Zhang

Interactive Loop Control #include using std::cout; using std::endl; int main( ) { int num, final; cout <<“Enter the final number for the table:” cin << final;//variable final used to control a loop for(num = 1; num <= final; ++num) cout << num << (num*num); return(0) } Dr. Ming Zhang

Common Programming Error (15_1) # include using std::cout; using std::endl; int main( ) { int x, y; for (x=2; x<=1; ++x){ y =10*x*x +3*x -2; cout << x << y<<endl;} return(0);} ERROR Dr. Ming Zhang

Common Programming Error (15_2) # include using std::cout; using std::endl; int main( ) { int x, y; for (x=2, x<=1, ++x){ y =10*x*x +3*x -2; cout << x << y<<endl;} return(0);} ERROR Dr. Ming Zhang

Common Programming Error (15_3) # include using std::cout; using std::endl; int main( ) { int x, y; for (x=2;x<=1; ++x); { y =10*x*x +3*x -2; cout << x << y<<endl;} return(0);} ERROR Dr. Ming Zhang

Common Programming Error (15_4) int num1 = 10, num2 = 10; cin >> opselect ; switch (opselect) { case 1: cout << “The sum:”<< (num1+num2); case 2: cout<<“The product”<<(num1*num2); break; case 3: cout<<“The division”<<(num1/num2); break; } ERROR Dr. Ming Zhang

Common Programming Error (15_5) int num1 = 10, num2 = 10; cin >> opselect ; switch (opselect) { case1: cout << “The sum:”<< (num1+num2); break; case2: cout<<“The product”<<(num1*num2); break; case3: cout<<“The division”<<(num1/num2); break; } ERROR Dr. Ming Zhang

Common Programming Error (15_6) int num1 = 10, num2 = 10; cin >> opselect ; switch (opselect) { case 1: cout << “The sum:”<< (num1+num2); break; case 1: cout<<“The product”<<(num1*num2); break; case 1: cout<<“The division”<<(num1/num2); break; } ERROR Dr. Ming Zhang