You are on page 1of 19

Programming

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

Sort the following list in ascending order


10,20,1,5,4,3,8,7,2

Search a first occurrence of digit ‘4’ from the list


10,20,1,5,4,3,8,7,2,4
Code the patterns
(a)
1
12
123
1234
12345
(b)
12345
1234
123
12
1
(c)
1
21
321
4321
54321
*
**
***
****
*****

For ( i=1 ; i<=5 ; i++)


{
For ( k=5 ; k> i; k--)
{
Cout >> “ ”;
}
For ( j=1 ; j<=i ; j++)
{
Cout >> * ;
}
Cout>>”/n”;
}

You might also like