0% found this document useful (0 votes)
10 views8 pages

Chapter 7. Loops.doc

Uploaded by

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

Chapter 7. Loops.doc

Uploaded by

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

Chapter 7.

LOOPS
⮚ while statement
⮚ do-while statement
⮚ for statement
⮚ break and continue statement
⮚ goto statement
⮚ nested loops

Loops
The loop is powerful statement in programming. The loops are used for repetitive
execution of statement in block of loop as long as the condition is true, all statement between
blocks of loops are executed repeatedly. When looping condition becomes false, the control
moves to the next statement immediately after the end of loop block. Where the condition is
valid logical statement. The logical statement determines the condition under which the loop
should continue repeating.

In ‘C’ Language there are three types of loops.


⮚ while statement
⮚ do-while statement
⮚ for statement

1. while loop

While (condition)
{
Body of while;
}

In this loop, the condition is given at the top. ‘while’ statement tests the values of
expression before processing the body of ‘while’. Then comes the body of ‘while’. The
body of ‘while’ may have one statement or more than one statements. If there is single
statement no need for { }. If there are more than one statements then brackets are
compulsory.

Flowchart for while loop


Computer checks the first condition, the body of while gets executed if and only if
the condition is true. Computer continues repeating the body till condition is true when
condition becomes false it stops and control goes to the next statement.
This loop should be used when condition is important.
Example:- Display 1,2,3,….. 100.
main()
{
int n=1;
while(n<=100)
{
printf(“%d\n”,n);
n++; /++n; /n=n+1; /n+=1;
}

2. do – while loop
This loop is used when the statements are to be executed initially and the result
of statement is compared. In case if the condition is satisfied or true, the looping is
continued otherwise looping is terminated.
In this loop computer enters into the body of do-while execute it and then checks
the conditions. If conditions is true it goes back and continues till condition remains true
when condition becomes false it goes to next statement.

do
{
body of loop;
}
while (condition);
E.g. do
{ printf(“%d \n”, n);
n++; }while (n<=100);

Flowchart of do while statement

In the above example the statement is executed 100 times. Initially the value of n is
displayed and then incremented by ne until the value of n is equal to 100. When the
value of n becomes 100 then the execution comes out of loop and goes to the next
statement.

⮚ Difference between ‘while’ and ‘do-while’

While do-while
1. Condition is on top 1. condition is at bottom
2. No necessity of brackets if there 2. Brackets are compulsory even if
is single statement in body there is a single statement.
3. Computer executes the body if condition 3. Computer executes the body at
is true. least once even if condition is
false.
4. This should be used when condition is more 4. This should be used when the
important. process is important.
5. This loop is also referred as entry control loop. 5. This loop is also referred as exit
controlled loop.
6. While (n<=100) 6. Do
{ printf(“%d\n”, n); { printf(“%d\n”,n);
n++; n++;
} }while (n<=100);

E.g. Display 1, 2,…..100 using do-while statement.


main()
{
int n=1;
do
{
printf(“%d\n”,n);
n++;
} while(n<=100);
}
3. for loop
This statement is used when calculations is to be carried out between initial and
final value with step. The ‘for’ loop condition consist of three actions namely initialization,
testing and incrementing/decrementing. Each action should be separated by semicolon
(;). Also this for ‘loop’ statement is used when number of execution time is known.

Syntax: for (initial value; condition; increment/decrement)


{
body of for;
}

E.g. for(i=1;i<=100;i++)
for(n=1;i<=50;n++)
for(i=2;i<=200;i++)

Flowchart “for” loop


In this loop the initial value, condition, increment/decrement is specified in the same line.
Here computer starts with the initial value, checks the condition. If the condition is true computer
execute the body and automatically goes up. It takes the increment or decrement automatically
and it continues.

Here everything is specified in the same line and hence we can tell the number of
repetitions it can take. When we know the number of repetitions in advance then we should go
for the ‘for’ loop. This loop can be written as follows:

1) i=1; for (; i<=100;i++)

2) i=1; for (i<=100)

{ i++; }

3) for (i=1;1<=100)

{ i++; }

4) for(i=1; j=10;i<=10;i++;j--)

We can initialize, increment/ decrement more than one variable in for statement.

5) ( ; ; )

Every parameter is optional in for loop. But it results in infinite loop.


Examples:

1> Display 1,2,3, …….., 100

main()

int i;

for (i=1;i<=100;i++)

printf(“%d\n”, i);

2> Display 1,2,4, 7, …. , k

main()

int i,k,d;

scanf(“%d”, &k);

d=1;

for (i=1;i<=k;i=i+d)

printf(“%d\n”, i);

d=d+1;

Goto statment

This statement transfers the control of the computer from one statement to other
statement in the program which may not be in sequence.

This statement can be written in two ways…

i) Unconditional of direct of blind.

ii) Conditional.
i) Unconditional

When a goto statement is written without any condition then it is called


unconditional goto statement. When this statement gets executed the control gets
transferred directly without checking any condition. It is possible that computer may
either in to an infinite loop from which it cannot come out. In such situation the system
hangs.

The format : goto label; where label is any valid identifier which indicates
the statement to which control should be
transferred.

e.g. goto top; goto bottom; goto first;

ii) Conditional

When goto statement is written as body of if or if-else .i.e., with some condition
then it is called conditional goto. Here computer checks the condition, if the condition is
true it executes goto. By putting proper condition we can control the number of
repetitions.

The format : if (condition)

goto label;

e.g. if (n<=100)

goto top;

if (a<b)

goto bottom;

It is never advised to use goto statement in programming, still if you want to use then always go
for conditional goto.

Nested loops

⮚ When one loop is inserted completely within another loop then it is called nested loop.

⮚ Nested loop means in one loop statement (inner loop) is executed inside the other loop
statement (outer loop).

⮚ The statement provides more number of cycles of executions because for every value of
outer loop statement is varied, the value of inner loop statement is also varied.
The nested loop can have form like these figures.

When the nested loop is executed then for every increment in the outer loop the inner loop
takes all its values. Here the execution gets multiplied. Hence nested loops are powerful, useful.

E.x. main()
{
int i,j;
for (i=1;i<=3;i++)
{
for(j=1;<=3;j++)
{
printf(“%d %d \n”, i, j)
}
}
}

The output :

11
22
13
21
22
23
31
32
33

You might also like