0% found this document useful (0 votes)
48 views

Loops: © 2004 Pearson Addison-Wesley. All Rights Reserved 5-1

Uploaded by

Ashh Ishh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Loops: © 2004 Pearson Addison-Wesley. All Rights Reserved 5-1

Uploaded by

Ashh Ishh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Loops

© 2004 Pearson Addison-Wesley. All rights reserved 5-1


Repetition Statements

• Repetition statements allow us to execute a statement multiple times


• Often they are referred to as loops
• Like conditional statements, they are controlled by Boolean
expressions
• C++ has three kinds of repetition statements:
• the while loop
• the do loop
• the for loop
• The programmer should choose the right kind of loop for the
situation

© 2004 Pearson Addison-Wesley. All rights reserved 5-2


The while Statement
• A while statement has the following syntax:

while ( condition )
statement;

• If the condition is true, the statement is


executed
• Then the condition is evaluated again, and if it is
still true, the statement is executed again
• The statement is executed repeatedly until the
condition becomes false

© 2004 Pearson Addison-Wesley. All rights reserved 5-3


Logic of a while Loop

condition
evaluated

true false

statement

© 2004 Pearson Addison-Wesley. All rights reserved 5-4


The while Statement
• An example of a while statement:

int count = 1;
while (count <= 5)
{
cout<<count;
count++;
}

• If the condition of a while loop is false initially,


the statement is never executed
• Therefore, the body of a while loop will execute
zero or more times

© 2004 Pearson Addison-Wesley. All rights reserved 5-5


The while Repetition Structure
int product
int product == 2;
2;
• Flowchart of while loop while (( product
product <=<= 1000
1000 ))
while
product == 22 ** product;
product product;
int product = 2

true
product <= 1000 product = 2 * product

false

© 2004 Pearson Addison-Wesley. All rights reserved 5-6


Parts of a while loop

int x = 1;
while (x < 10) {
cout<<x;
x++;
}
• Label the following loop
int product = 2;
while ( product <= 1000 )
product = 2 * product;

© 2004 Pearson Addison-Wesley. All rights reserved 5-7


Infinite Loops

• The body of a while loop eventually must make the condition false
• If not, it is called an infinite loop, which will execute until the user
interrupts the program
• This is a common logical error
• You should always double check the logic of a program to ensure that
your loops will terminate normally

© 2004 Pearson Addison-Wesley. All rights reserved 5-8


Infinite Loops
• An example of an infinite loop:

int count = 1;
while (count <= 25)
{
System.out.println (count);
count = count - 1;
}

• This loop will continue executing until interrupted


(Control-C) or until an underflow error occurs

© 2004 Pearson Addison-Wesley. All rights reserved 5-9


Nested Loops

• Similar to nested if statements, loops can be nested as well


• That is, the body of a loop can contain another loop
• For each iteration of the outer loop, the inner loop iterates
completely

© 2004 Pearson Addison-Wesley. All rights reserved 5-10


Nested Loops
• How many times will the string "Here" be printed?

count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 <= 20)
{
cout<<“Here";
count2++;
}
count1++;
}

© 2004 Pearson Addison-Wesley. All rights reserved 5-11


The do Statement
• A do statement has the following syntax:

do
{
statement;
}
while ( condition )

• The statement is executed once initially, and then


the condition is evaluated
• The statement is executed repeatedly until the
condition becomes false

© 2004 Pearson Addison-Wesley. All rights reserved 5-12


Logic of a do Loop

statement

true

condition
evaluated

false

© 2004 Pearson Addison-Wesley. All rights reserved 5-13


The do Statement
• An example of a do loop:

int count = 0;
do
{
count++;
cout<<count;
} while (count < 5);

• The body of a do loop executes at least once

© 2004 Pearson Addison-Wesley. All rights reserved 5-14


Comparing while and do
The while Loop The do Loop

statement
condition
evaluated
true

condition
true false
evaluated

statement
false

© 2004 Pearson Addison-Wesley. All rights reserved 5-15


The for Statement
• A for statement has the following syntax:

The initialization The statement is


is executed once executed until the
before the loop begins condition becomes false

for ( initialization ; condition ; increment )


statement;

The increment portion is executed at


the end of each iteration

© 2004 Pearson Addison-Wesley. All rights reserved 5-16


Logic of a for loop

initialization

condition
evaluated

true false

statement

increment

© 2004 Pearson Addison-Wesley. All rights reserved 5-17


The for Statement

• A for loop is functionally equivalent to the following while loop


structure:
initialization;
while ( condition )
{
statement;
increment;
}

© 2004 Pearson Addison-Wesley. All rights reserved 5-18


The for Statement
• An example of a for loop:

for (int count=1; count <= 5; count++)


cout<<count;

• The initialization section can be used to declare a


variable
• Like a while loop, the condition of a for loop is
tested prior to executing the loop body
• Therefore, the body of a for loop will execute zero
or more times

© 2004 Pearson Addison-Wesley. All rights reserved 5-19


for loop Exercises

• Write the for statement that print the following


sequences of values.
• 1, 2, 3, 4, 5, 6, 7
• 3, 8, 13, 18, 23
• 20, 14, 8, 2, -4, -10
• 19, 27, 35, 43, 51

© 2004 Pearson Addison-Wesley. All rights reserved 5-20


Exercise

• How many times is the following loop body repeated? What is printed
during each repetition of the loop body and after exit?
x = 3;
for (int count = 0; count < 3; count++)
{
x = x * x;
cout<<x;
}

© 2004 Pearson Addison-Wesley. All rights reserved 5-21


Nested loops. What do these print?

• for (int i = 1; i < 4; i++)


for (int j = 1; j < i; j++)
cout<<i <<j;

• for (int i = 1; i < 4; i++)


for (int j = 1; j < i; j++)
cout<<“******”;

© 2004 Pearson Addison-Wesley. All rights reserved 5-22


Stars

//---------------------------------------------------
// Prints a triangle shape using asterisk (star)
// characters.
//---------------------------------------------------
void main (String[] args)
{
int MAX_ROWS = 10;

for (int row = 1; row <= MAX_ROWS; row++)


{
for (int star = 1; star <= row; star++)
cout<<"*";
}
}

© 2004 Pearson Addison-Wesley. All rights reserved 5-23


Using a loop to find if a number is prime

• Prime numbers are only divisible by 1 and themselves

© 2004 Pearson Addison-Wesley. All rights reserved 5-24

You might also like