1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
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.
Chapter 5: Control Structures II (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)
1 Lecture 10:Control Structures II (Repetition) Introduction to Computer Science Spring 2006.
Chapter 5: Control Structures II (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
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CPS120 Introduction to Computer Science Iteration (Looping)
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
1 Chapter 9 Additional Control Structures Dale/Weems.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
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.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
CPS120 Introduction to Computer Science Iteration (Looping)
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
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.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
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.
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II
C++ Programming: CS150 For.
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Control Structures II (Repetition)
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
Chapter 2.2 Control Structures (Iteration)
Iteration with While You can say that again.
Chapter 2.2 Control Structures (Iteration)
Chapter 5: Control Structures II (Repetition)
do/while Selection Structure
Programming Fundamental
Presentation transcript:

1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006

2

3 The while Loop The general form of the while statement is: while(expression) { statement1; statement2; … }

4 The for Loop The general form of the for statement is: for(initial statement; loop condition; update statement) statement; for(initial statement; loop condition; update statement) {statement1; statement2; … } The for loop executes as follows: initial statement executes (initial statement initializes a variable) loop condition is evaluated If loop condition evaluates to true Execute for loop statement Execute update statement Repeat previous step until the loop condition evaluates to false

5 Example #include using namespace std; int main() { int i; for (i=0; i<10; i++) cout<<i<<" "; cout<<endl; return(0); }

6

7 The for Loop (continued) initial statement in the for loop is the first to be executed and is executed only once If the loop condition is initially false, the loop body does not execute The update expression changes the value of the loop control variable which eventually sets the value of the loop condition to false The for loop executes indefinitely if the loop condition is always true A semicolon at the end of the for statement is a semantic error In this case, the action of the for loop is empty If the loop condition is omitted It is assumed to be true

8 The for Loop (continued) In a for statement, all three statements (initial statement, loop condition, and update statement) can be omitted The following is a legal for loop: for(;;) cout<<"Hello"<<endl;

9 The do … while Loop The general form of a do...while statement is: The procedure is: statement executes first, and then the expression is evaluated If the expression evaluates to true, the statement executes again As long as the expression in a do...while statement is true, the statement executes do statement While(expression); do { statement1; statement2; … } While(expression);

10

11 Example for do…while loop #include using namespace std; int main() { int i=0; do { cout<<i<<" "; i=i+5; } while (i<=20); cout<<endl; return(0); }

12 Break & Continue Statements break and continue alter the flow of control break statement: syntax: break; The break statement is used for two purposes: In a repetition structure (e.g. while, for, and do … while loop), it immediately exits from a loop In a switch … case structure, skip the remainder of the switch structure (an immediate exit) After the break statement executes, the program continues with the first statement after the structure The use of a break statement in a loop can eliminate the use of certain (flag) variables

13 #include using namespace std; int main() { int num, sum = 0; cin >> num; while(cin) { if (num<0) { cout<<"Negative number found!"<<endl; break; } sum=sum+num; cin>>num; } cout<<"The sum is "<<sum<<endl; return(0); } Example for break statement

14 Break & Continue Statements (continued) continue statement Syntax: continue; used in a loop (while, for, and do-while structures) Skips remaining statements and proceeds with the next iteration of the loop In a while and do-while structure Expression (loop-continue test) is evaluated immediately after the continue statement In a for structure, the update statement is executed after the continue statement Then the loop condition executes

15 #include using namespace std; int main() { int num, sum = 0; cin >> num; while(cin) { if (num<0) { cout<<"Negative number found!"<<endl; continue; } sum=sum+num; cin>>num; } cout<<"The sum is "<<sum<<endl; return(0); } Example for continue statement

16 Nested Control Structures Suppose we want to create the following pattern * ** *** **** ***** In the first line, we want to print one star, in the second line two stars and so on Since five lines are to be printed, we start with the following for statement for(i = 1; i <= 5 ; i++) The value of i in the first iteration is 1, in the second iteration it is 2, and so on

17 Nested Control Structures (continued) The syntax is: for(i = 1; i <= 5 ; i++) { for(j = 1; j <= i; j++) cout<<"*"; cout<<endl; }

18 Nested Control Structures (continued) What pattern does the code produce if we replace the first for statement with the following? for (i = 5; i >= 1; i--) Our program becomes: for(i = 1; i <= 5 ; i++) { for(j = 1; j <= i; j++) cout<<"*"; cout<<endl; }

19 Answer: ***** **** *** ** *

20 End of lecture 11 Thank you!