Sunteți pe pagina 1din 37

CSC-113 COMPUTER PROGRAMMING

LECTURE 06
LOOP STRUCTURE (FOR, WHILE, DO-WHILE)

1
TYPES OF CONTROL STRUCTURES
1. Sequential structure
Processes one instruction after another

2. Selection structures (if, if-else, switch)


Decision structure
Make choices by using relational or logical
operators
Case structure
Enable to pick up one of a set of tasks

3. Loop structure (for, while, do-while)


Enable to repeat tasks
WHY DO WE NEED REPETITION?
Sometimes we want to do things more than once.
E.g., Calculate the grades of 160 students
E.g., Calculate the sum of 1~100

How?
1.Cut and paste the codes 160 times?
2.cout << 1 + 2 + +100 << endl; ??
3.Then, what about summing up 1~10000?
CONCEPT OF LOOP

Loop: The real power of computers!


STOPPING THE LOOP
LOOP
There may be a situation, when you need to execute a block of code
several number of times.
In general statements are executed sequentially.
The first statement in a function is executed first, followed by the
second, and so on.
Programming languages provide various control structures that allow
for more complicated execution paths.
A loop statement allows us to execute a statement or group of
statements multiple times
LOOP
1. Loop executes a block of code repeatedly until a certain condition is
met.
2. C++ provides four loops
For, while, do while
LOOP?
A loop is a control statement that allows repeating execution of a block
of statements.
May execute a code block fixed number of times.
May execute a code block while given condition holds.
May execute a code block for each member of a collection.
Loops that never end are called an infinite loops
LOOP TYPE
FOR LOOP
A for loop is a repetition control structure that
allows you to efficiently write a loop that needs to
execute a specific number of times.
THE for LOOP

A for loop is used when your loop is to be


executed a known number of times. You can
do the same thing with a while loop, but the
for loop is easier to read and more natural for
counting loops.
FOR LOOP
for loop is another powerful loop construct in C++.
It is powerful and easy to use. for loop includes all three
characteristics as initialization, termination and increment/decrement
in a single line.
for (initialization; termination; increment / decrement) ;
FLOW OF CONTROL IN A FOR LOOP
Here is the flow of control in a for loop:
The init step is executed first, and only once. This step allows you to declare and
initialize any loop control variables. You are not required to put a statement here,
as long as a semicolon appears.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it
is false, the body of the loop does not execute and flow of control jumps to the
next statement just after the for loop.
After the body of the for loop executes, the flow of control jumps back up to
the increment statement. This statement allows you to update any loop control
variables. This statement can be left blank, as long as a semicolon appears after
the condition.
The condition is now evaluated again. If it is true, the loop executes and the
process repeats itself (body of loop, then increment step, and then again
condition). After the condition becomes false, the for loop terminates.
FLOW OF
CONTROL
IN A FOR
LOOP
FOR: HOW IT WORKS?
FOR LOOP (Example)
FOR LOOP (Example)
STEP BY STEP TRACE
int main()
{
int j;
for(j = -4; j <= 0 ; j = j + 1)
{
cout << j << endl;
}
return 0;
}
Program 5-11
// This program displays the numbers 1 through 10 and
// their squares.
#include <iostream.h>

void main(void)
{
int num;

cout << Number Number Squared\n";


cout << "-------------------------\n";

for (num = 1; num <= 10; num++)


cout << num << "\t\t" << (num * num) << endl;
}

19
Program Output

Number Number Squared


-------------------------
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100

20
WHILE LOOP
Loop is very important in every programming language that saves you
from repetitive nature of work.
The C++ supports While, do while, and for loop.
The while loop is one of the important looping construct, that is being
widely used in C ++ programming language.
To understand the concept of while loop, consider the following
diagram.
WHILE LOOP
In the example, three things are important.
(i) Initialization
(ii) Increment/Decrement
(iii) Termination
These are the important characteristics of any
loop. Initialization represents the starting
position from there your loop will be started.
In the above example int i=0 refers to
initialization of while loop. i++ refers to
increment/decrement and finally while(i<10)
refers to the termination of while loop.
Each time the while loop executed, it checks
for condition whether the value of i is less
than 10 or not. If the value of i is less than 10,
the loop will be executed and increment i by
one and then print the value of i. When the
value of i reaches 10, the while(i<10)
condition becomes false and terminates the
loop.
WHILE: HOW IT WORKS?
WHILE LOOP
WHILE LOOP HOW IT WORKS?
EXAMPLE (01)
DO WHILE
DO WHILE
do while loop treats same as while loop but only differences between
them is that, do while executes at least one time.
The code executes first then check for specified loop condition. But in
while loop, the specified loop condition evaluated first then executes
the code.
A simple demonstration will help you to figure out do while loop more
clearly.
DO WHILE

Note: You must put semi-colon(;) at the end of while condition in dowhile loop.
DO WHILE: HOW IT WORKS?
DO WHILE: HOW IT WORKS?
DO WHILE LOOP
DO WHILE: HOW IT WORKS?
NESTED LOOP
Sometimes, you are required to perform a task in nested loop
condition.
A loop within a loop is called nested loop.
You can nested n number of loop as nested.
NESTED LOOP
In the preceding example, the outer for loop wont
increment until inner for loop processes its all the cycles.
Once the condition of inner for loop becomes false, the
outer for loop increment by one and then again inner for
loop does same work as previous.
NESTED LOOP
NESTED LOOP: HOW IT WORKS?

S-ar putea să vă placă și