Professional Documents
Culture Documents
Fundamentals
Lecture 6
Recap on Loops
• What are the repetitive/ iterative control structures?
• Why do we need them?
• What are the types of iterative structures?
• What is for loop?
• What is while loop?
• What is do-while loop?
• What are the nested loop?
Repetition Control Structures
(Loops)
• Apart from decision making control structure,
there are some repetition control structures in
programming. Repetition control structures allows
certain portion of a program to repeat either
indefinitely or for fixed period of time.
• They are also known as iterative structures, loops.
• The three basic types of loops are:
• While loop
• Do-While loop
• For loop
When to use the loop
• For calculating a table
• For extensive calculation
• For computing average of 20 subjects marks
• In general, when the given problem requires you to repeat or
iterate over some portion of the code, you use the loop.
while Loop
The for loop does something a fixed number of times.
What happens if you don’t know how many times you want to do
something before you start the loop?
In this case a different kind of loop may be used: the while loop.
while Loop
while loop executes a set of statements as
long as the condition specified at the
beginning is true.
The condition is evaluated at the beginning
of the loop
If it is true, the loop will execute
If it is false, the loop will not execute even
once
while Loop
Syntax:
while(condition)
{statement1;
…
statement n;
}
do-while
Similar to while loop, except the condition is at the end of the
loop.
The loop always executes at least once, regardless of whether the
condition is true or not.
Syntax:
do
{statement 1;
...
statement n; } while(<condition>);
Note: A semicolon at the end and curly braces are always required.
do-while
Output:
Example 1
int a=1; 2
3
do {
cout<<a<<endl;a++; }while(a<=3);
However if,
int a=4; Output:
do { 4
cout<<a<<endl;a++; }while(a<=3);
Nested Loop
• Using one looping construct inside another.
• Used when for one repetition of a process, many
repetitions of another process are needed.
• Some applications are
• to print patterns,
• find sum of a repeating series, etc.
Infinite Loops
• Loops that never stop are infinite loops
• The loop body should contain a line that will
eventually cause the boolean expression to
become false
• Example: Print the odd numbers less than 12
x = 1;
while (x != 12)
{
cout << x << endl;
x = x + 2;
}
• Better to use this comparison: while ( x < 12)
22
The for-Statement
•A for-Statement (for-loop) is the most common
loop in C, C++, Java, C#
- Designed for common tasks such as adding
numbers in a given range
- Is sometimes more convenient to use than a while
loop
- Does not do anything a while loop cannot do
8
for/while Loop Comparison
• int sum = 0;
int n = 1;
while(n <= 10) // add the numbers 1 - 10
{
sum = sum + n;
n++;
}
• int sum = 0;
int n = 0;
for (n = 1; n <= 10; n++) //add the numbers 1 - 10
sum = sum + n;
9
Which Loop To Use?
• Choose the type of loop late in the design process
- First design the loop using pseudocode
- Translate the pseudocode into C, C++
- The translation generally makes the choice of an
appropriate loop clear
- While-loops are used for all other loops when there
might be occassions when the loop should not run
- Do-while loops are used for all other loops when
the loop must always run at least once
14
The break-Statement
• There are times to exit a loop before it ends
- If the loop checks for invalid input that would ruin
a calculation, it is often best to end the loop
•The break-statement can be used to exit a
loop before normal termination
- Be careful with nested loops! Using break only
exits the loop in which the break-statement occurs
18
Examples:
Compute the average of the following series using all three types of loops
10,20,1,5,4,3,8,7,2