Week#10

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

Programming Fundamentals

1
Repetition (looping)
Lecture Outline
1 What is a loop?
2 Count/Event Controlled Loops

3 While statement

4 Do-While statement

5 For statement
1. What is a loop?

⮚Loops are a part of C++ programming


used to repeat a specific block of
code.
2. Count/Event Controlled Loops

We need to repeat a specific code for two reasons:

1- defined number of iterations.In this case we call it


count controlled repetition.

2- certain event happens.In this case we call it event


controlled repetition
3. While Statement

The while loop has two important parts:


(1) an expression that is tested for a true or false value,
(2) a statement or block that is repeated as long as the
expression is true.
3. While Statement

SYNTAX

while ( condition )
{
loop body statements
}
3. While Statement
Example
Every while loop will always
contain three main elements:
int count ;
count = 4;
Priming: initialize your
while (count > 0)
variables.
{ cout<<count ;
count -- ;
Testing: test against some
}
known condition.
cout << “Done” ;
Updating: update the
variable that is tested.
The While loops
The While loops Initial
value
Stopping
• The while loop consists of 3 condition

major parts:
o Initial value
o Stopping condition
o update

update
3. While Statement
Example 1 Example 2 (Infinite Loop:
#include <iostream> A loop that never ends)
using iostream std;
main () #include <iostream>
{ using iostream std;
const int MAX =10; main ()
int index =1; {
while (index <= MAX) const int MAX =10;
{cout<<“index: “<< index<<endl; int index =1;
index = index + 1; while (index <= MAX)
} { cout<<“index: “<< index<<endl;
} }
}
The While loops
• Cases that causes infinite loops
3. While Statement
Example
Use a while loop to print the numbers from 1 to 20 each on a
separate line.
int count ;

count = 1 ;
while ( count < =20 )
{ cout<<“count = “<<count<<endl;
count++;
}
3. While Statement
Example
Use a while loop to read 10 numbers and get their average
int total=0,count,av ,n;
count = 0 ;
while ( count < 10 )
{
cin>>n;
total = total + n ;
count++ ;
}
av=total/10;
cout<<“\n The average = “ << av ;
3. While Statement
Example on event controlled looping

int x=0;

while (x!=10)
{ The code inside this loop will be
cout<<“try again!” ; repeated until the user enters
cin>>x; 10, hence the condition will be
} false.
4. Do-While Statement

SYNTAX

do
{
loop body statements
} while ( condition );
4. Do-While Statement
⮚ The do…while repetition statement is similar to the
while statement.
⮚ In the while statement, the loop-continuation
condition is tested at the beginning of the loop
before the body of the loop is performed.
⮚ The do…while statement tests the loop-continuation
condition after the loop body is performed.
⮚ Therefore, the loop body will be executed at least
once.
⮚ When a do…while terminates, execution continues
with the statement after the while clause .
4. Do-While Statement
Example

int x=1;

do
{ The text will be printed at least
cout<<“ I’m in the loop \n”; one time, then it will be repeated
cin>>x; until the user enter 0
} while (x!=0 );
5. For Statement

SYNTAX

for (start; condition; update)


{
loop body statements
}
5. for Statement
• Converting from while statement to for statement
5. for Statement
Example
print the numbers from 1 to 20 each on a separate line.

int count ; int count ;

count = 1 ; for(count = 1;count < =20;count++ )


while ( count < =20 ) {
{ cout<<“ \n“<<count ;
cout<<“ \n“<<count ; }
count++;
}
5. for Statement
Example read 10 numbers and get their average

int total=0,count,av ,n; int total=0,count,av ,n;


count = 0 ;
for(count=0;count < 10;count++ ) while ( count < 10 )
{ {
cin>>n ; cin>>n ;
total = total + n ; total = total + n ;
} count++ ;
av=total/10; }
cout<<“The average = “<< av<<endl; av=total/10;
cout<<“The average = “<< av<<endl;
5. for Statement
Example: prints the factorial of n
5. for Statement
5. for Statement
Variation Examples
⮚ any expression may be more complex:
for (i=100*y; i>=1; i--)
for (i=100; i>=y/2; i--)
for (i=100; i>=1; i- =4*y)
⮚ Increment may be negative:
for (i=100; i>=1; i--)
This counts from 100 to 1.
⮚ Increment/decrement may be greater than 1:
for (i=100; i>=5; i- =5)
This counts from 100 to 5 in steps of 5
THANK YOU

27

You might also like