Chapter 2.2 Control Structures (Iteration)

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

Computer Science 1620 Loops.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within 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.
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)
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Control Structures in C++ while, do/while, for switch, break, continue.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
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
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
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 © 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.
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.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Copyright © 2012 Pearson Education, Inc. Chapter 5: 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.
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.
Computer Programming -1-
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
UCT Department of Computer Science Computer Science 1015F Iteration
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
while Repetition Structure
Chapter 5: Control Structures II
Lecture 7: Repeating a Known Number of Times
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 4 - Program Control
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Control Structures II (Repetition)
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Chapter 5: Control Structures II
Looping.
- Additional C Statements
Additional Control Structures
Chapter 4 - Program Control
Repetition Control Structure
Program Control Topics While loop For loop Switch statement
2.6 The if/else Selection Structure
Chapter 5: Control Structures II (Repetition)
Dale Roberts, Lecturer IUPUI
Repetition Statements (Loops) - 2
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
Chapter 4 Repetition Structures
Presentation transcript:

Chapter 2.2 Control Structures (Iteration) Dr. Shady Yehia Elmashad

Outline 1. C++ Iterative Constructs 2. The for Repetition Structure 3. Examples Using the for Structure The while Repetition Structure Examples Using the while Structure The do/while Repetition Structure The break and continue Statements Nested control structures

1. C++ Iterative Constructs There are three constructs: while statement for statement do-while statement

2. The for Repetition Structure The general format when using for loops is for ( initialization; LoopContinuationTest; increment ) statement Example: for( int counter = 1; counter <= 10; counter++ ) cout << counter << endl; Prints the integers from one to ten No semicolon after last statement

2. The for Repetition Structure Syntax for (ForInit ; ForExpression; PostExpression) Action Example for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; }

2. The for Repetition Structure For loops can usually be rewritten as while loops: initialization; while ( loopContinuationTest){ statement increment; } Initialization and increment as comma-separated lists for (int i = 0, j = 0; j + i <= 10; j++, i++) cout << j + i << endl;

3. Examples Using the for Structure Sum the numbers from 0 to 10 #include <iostram.h> void main ( ) { int sum = 0 ; for ( int i = 0; i < = 10; i++ ) sum = sum + i ; } cout << “ Summation = “ << sum ; Summation =

3. Examples Using the for Structure Sum the even numbers from 0 to 100 #include <iostram.h> void main ( ) { int sum = 0 ; for ( int i = 0; i < = 100; i+=2 ) sum = sum + i ; } cout << “ Summation = “ << sum ; Summation =

3. Examples Using the for Structure Sum the odd numbers from 0 to 100 #include <iostram.h> void main ( ) { int sum = 0 ; for ( int i = 1; i < = 100; i+=2 ) sum = sum + i ; } cout << “ Summation = “ << sum ; Summation =

3. Examples Using the for Structure Printing characters depending on user entry #include <iostram.h> void main ( ) { int n ; char ch; cout << “ Please enter the character: “ ; cin >> ch ; cout << “ Please enter the number of repetition: “ ; cin >> n ; for ( int i = 0; i < n ; i++ ) cout << ch; }

4. The while Repetition Structure

4. The while Repetition Structure While Semantics

4. The while Repetition Structure Programmer specifies an action to be 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. Example int product = 2; while ( product <= 1000 ) product = 2 * product;

4. The while Repetition Structure Flowchart of while loop product <= 1000 product = 2 * product true false

5. Examples Using the while Structure Printing characters depending on user entry #include <iostram.h> void main ( ) { int n, i = 0 ; char ch; cout << “ Please enter the character: “ ; cin >> ch ; cout << “ Please enter the number of repetition: “ ; cin >> n ; while ( i < n ) { cout << ch ; i ++ ; }

5. Examples Using the while Structure The summation of the numbers squared from 0 to 10 #include <iostram.h> void main ( ) { int sq_sum = 0, x = 0, y ; while ( x < = 10 ) { y = x * x ; sq_sum = sq_sum + y ; x ++ ; } cout << “The summation of the numbers squared from 0 to 10 “ << sq_sum ;

5. Examples Using the while Structure Factorial of a number #include <iostram.h> void main ( ) { int n, fact = 1 ; cout << “ Please enter a number “ << endl ; cin >> n ; while ( n > 0 ) { fact = fact * n ; n -- ; } cout << “ The factorial of your number is “ << fact ;

6. The do/while Repetition Structure The do/while repetition structure is similar to the while structure, Condition for repetition tested after the body of the loop is executed Syntax: do { statement(s) } while ( condition ); Example (letting counter = 1): cout << counter << " "; } while (++counter <= 10); This prints the integers from 1 to 10 All actions are performed at least once. true false action(s) condition

7. The break and continue Statements Causes immediate exit from a while, for, do/while or switch structure Program execution continues with the first statement after the structure Common uses of the break statement: - Escape early from a loop - Skip the remainder of a switch structure

7. The break and continue Statements Skips the remaining statements in the body of a while, for or do/while structure and proceeds with the next iteration of the loop In while and do/while, 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

7. The break and continue Statements #include <iostream.h> Void main() {     int sum = 0, num;      // Allow the user to enter up to 10 numbers     for (int count=0; count < 10; ++count) {         cout << "Enter a number to add, or 0 to exit: ";         cin >> num;           // exit loop if user enters 0         if (num == 0)             break;         // otherwise add number to our sum         sum += num;     }      cout << "The sum of all the numbers you entered is " << sum << "\n"; } This program allows the user to type up to 10 numbers, and displays the sum of all the numbers entered at the end. If the user enters 0, the break causes the loop to terminate early (before 10 numbers have been entered).

7. The break and continue Statements #include <iostream.h>  void main ( ) {     while (true) // infinite loop     {         cout << "Enter 0 to exit or anything else to continue: ";         int num;         cin >> num;           // exit loop if user enters 0         if (num == 0)             break;     }     cout << "We're out!\n"; } Note that break can be used to get out of an infinite loop.

7. The break and continue Statements #include <iostream.h>  void main ( ) {   for (int count=0; count  < =20; ++count) {     // if the number is divisible by 4, skip this iteration     if ((count % 4) == 0)         continue;       // If the number is not divisible by 4, keep going     cout << count << endl; } This program prints all of the numbers from 0 to 20 that aren’t divisible by 4.

8. Nested Control Structures Accept 10 numbers from the user & print the max. one #include <iostram.h> void main ( ) { int num, largest = 0 ; for ( int i = 0; i < 10; i ++ ) { cout << “ Enter a number: “ ; cin >> num ; if ( num > largest) { largest = num ; } cout << “ The largest number is “ << largest << endl ;

8. Nested Control Structures Multiplication Table of 5 #include <iostram.h> void main ( ) { cout << “ \ t 1 \ t 2 \ t 3 \ t 4 \ t 5 “ ; << endl ; for ( int i = 1 ; i < = 5 ; i ++ ) { cout << i ; cout << “ \ t “ ; for ( int j = 1 ; j < = 5 ; j ++ ) { cout << i * j << “ \ t “ << “ | “ ; } cout << endl;

8. Nested Control Structures Multiplication Table of n #include <iostram.h> void main ( ) { cout << “ Please enter a number: “ ; cin >> n ; for ( int i = 1 ; i < = n ; i ++ ) { cout << i ; cout << “ \ t “ ; } cout << endl ; for ( int j = 1 ; j < = n ; j ++ ) { for ( int k = 1 ; k < = n ; k ++ ) { cout << j * k << “ \ t “ << “ | “ ; cout << endl;

8. Nested Control Structures * * * * * for (int i=1; i<=5; i++){ for (int j=1; j<=5; j++){ cout<<"*"; } cout<<endl;

8. Nested Control Structures 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 for (int i=1; i<=5; i++){ for (int j=1; j<=5; j++){ cout<<i; } cout<<endl;

8. Nested Control Structures 1 2 3 4 5 for (int i=1; i<=5; i++){ for (int j=1; j<=5; j++){ cout<<j; } cout<<endl;

8. Nested Control Structures * * * * * * * * * * * * * * * for (int i=1; i<=5; i++){ for (int j=1; j<=i; j++){ cout<<“*”; } cout<<endl;

8. Nested Control Structures 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 for (int i=1; i<=5; i++){ for (int j=1; j<=i; j++){ cout<<j; } cout<<endl;

8. Nested Control Structures 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 for (int i=1; i<=5; i++){ for (int j=1; j<=i; j++){ cout<<i; } cout<<endl;