22
CHAPTER 9 REPETITION STRUCTURE (LOOP) Prepared by: Lec. Ghader Kurdi

Chapter 9 Repetition Structure (Loop)

  • Upload
    kyoko

  • View
    84

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 9 Repetition Structure (Loop). Prepared by: Lec . Ghader Kurdi. Introduction. A Repetition structure represents certain parts of the code that will be executed repeatedly, or not at all based on the current state (condition ). In C++, there are 3 forms of implementing repetition: - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 9 Repetition  Structure (Loop)

CHAPTER 9REPETITION STRUCTURE (LOOP)Prepared by: Lec. Ghader Kurdi

Page 2: Chapter 9 Repetition  Structure (Loop)

Introduction• A Repetition structure represents certain parts of the code that

will be executed repeatedly, or not at all based on the current state (condition).• In C++, there are 3 forms of implementing repetition: • While Loop

While a loop is a group of instructions that the computer executes repeatedly until a terminating condition is satisfied.• Do-while Loop • For Loop

Page 3: Chapter 9 Repetition  Structure (Loop)

While Loop

Page 4: Chapter 9 Repetition  Structure (Loop)

While Loop (cont.)• Initialization of the control variable which will be used to test

the condition is applied only once.• If the condition is true, the statement and the increment are

executed, then the whole thing is done again.• The statement and the increment are executed repeatedly until

the condition becomes false.• If the condition starts out false, the while-body will never be

executed.• Increment is the statement that makes change on the

condition, otherwise, the loop will continue running (Infinite loop).

Page 5: Chapter 9 Repetition  Structure (Loop)

Example

void main() { int count = 1; while (count <= 2) { cout << "Welcome to C++!"; count++; } }

Outputs:

Page 6: Chapter 9 Repetition  Structure (Loop)

Example

void main() { int count = 0; while (count < 2) { cout << "Welcome to C++!"; } }

Outputs :

Page 7: Chapter 9 Repetition  Structure (Loop)

Example

void main() { int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } }

Outputs:

Page 8: Chapter 9 Repetition  Structure (Loop)

Example

void main() { int count = 2; while (count < 2) { cout << "Welcome to C++!"; } }

Outputs:

Page 9: Chapter 9 Repetition  Structure (Loop)

Do-While Loop do { …any number of statements… // action } while (condition) ;

Page 10: Chapter 9 Repetition  Structure (Loop)

Do-While Loop (cont.)

• The do-while statement performs the test after executing the body. • As long as the test is true, the body of the program

will be executed again.• In all cases, the body will be executed at least once,

even if the condition was wrong because testing happened at the end.

Page 11: Chapter 9 Repetition  Structure (Loop)

Example

void main() { int count = 1; do { cout << "Welcome to C++!"; count++; } while (count <= 2); }

Outputs:

Page 12: Chapter 9 Repetition  Structure (Loop)

Example

void main() { int count = 1; do { cout << "Welcome to C++!"; count++; } while (count > 2); }

Outputs:

Page 13: Chapter 9 Repetition  Structure (Loop)

Example

#include <iostream.h>void main (){ do { cout << "\nYou can't stop me!"; } while (1); // infinite do-while}

Outputs:

Page 14: Chapter 9 Repetition  Structure (Loop)

Example

#include <iostream.h>void main (){ do { cout << "You can't stop me!\n"; } while (0); // infinite do-while}

Outputs:

Page 15: Chapter 9 Repetition  Structure (Loop)

Do-While Loop (cont.)

The difference between while and do-while • The testing happens before executing the body in the

while structure.• So if the condition is false from the beginning, the

body will not be executed at all.

Page 16: Chapter 9 Repetition  Structure (Loop)

For Loop

for ( initialize; condition; increment ) { statements ; }

Page 17: Chapter 9 Repetition  Structure (Loop)

For Loop• For structure is composed of 4 parts: Initialize: initializes the loop control variable, and done only once. Condition: that is the condition that determines whether the loop should continue executing or not. Statements: the body of the loop. The increment: is a statement that changes the value of the initialization either by adding, subtracting, or any math operation. But usually it is either adding or subtracting. • It should be noticed that there is no semicolon after the

increment.• This "for structure" structure is better to be used when the

number of iterations is known.

Page 18: Chapter 9 Repetition  Structure (Loop)

Example

void main() { int i; for (i = 0; i < 2; i++) { cout << "Welcome to C++!"; } }

Outputs:

Page 19: Chapter 9 Repetition  Structure (Loop)

Example

#include <iostream> int main (){ for (int n=10; n>0; n--) { cout << n << ", "; }cout << «Done!\n";}

Outputs:

Page 20: Chapter 9 Repetition  Structure (Loop)

Example

#include <iostream> int main (){ int n = 10; while (n>0) { cout << n << ", "; --n; } cout << "Done!\n";}

Outputs:

Page 21: Chapter 9 Repetition  Structure (Loop)

Exercises • Request the user to type positive numbers until either a zero or a negative is typed,

and then show the user how many positives were typed in.

• Write a program that prints the cubes of the numbers from 1 to 10.Here’s the output from the program: 1 1 2 8 3 27 4 64 5 125 6 216 7 343 8 512 9 729 10 1000

Page 22: Chapter 9 Repetition  Structure (Loop)

• For loops can always be re-written as while loops, and vice-versa. Are the following two programs equivalent, and what is their output? Explain your answer, and run the programs to check.

Program (a)

#include <iostream>int main(){ int count ; for (count = 0; count < 5; count++) { cout << count << "\n"; } return 0;}

Program (b)

#include <iostream> int main(){ int count = 1; while (count <= 5) { cout << count << "\n"; count++; } return 0;}