Chapter 9 Repetition Structure (Loop)

Post on 24-Feb-2016

76 views 0 download

Tags:

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

Transcript of Chapter 9 Repetition Structure (Loop)

CHAPTER 9REPETITION 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: • 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

While 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).

Example

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

Outputs:

Example

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

Outputs :

Example

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

Outputs:

Example

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

Outputs:

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

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.

Example

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

Outputs:

Example

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

Outputs:

Example

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

Outputs:

Example

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

Outputs:

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.

For Loop

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

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.

Example

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

Outputs:

Example

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

Outputs:

Example

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

Outputs:

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

• 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;}