<< Previous Lesson Back To Main Index Next Lesson >>
while loop
for loop
do-while loop
It’s ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is begin satisfied. This repetitive operation is done through a loop control structure.
OR
A set of statements are executed using repetitive structure. These statements are executed many times while these statements appear once in program. A loop involves repeating some portion of the program either a specified number of times, or until a particular condition is being satisfied. This repetitive operation (looping) is achieved in C through a while or for or do-while statement.
There are three types of loops in C.
1. while Loop
2. for Loop
3. do-while Loop
1. while loop:-
(The Condition is checked first if it is true then executed again)
It is Variable-Length Loop. The simplest of all the looping structures in C is the while statement. In while loop the test-condition is evaluated and if the condition is true, then the body of the loop is executed and if again it is true, the body is executed once again. This process of repeated execution of the body continues until the test-condition finally becomes false and control is transferred out of the loop.
While Loop Structure:
while (test condition)
{
// body of the
loop
}
The statement is only executed if the expression is non-zero. After every execution of the statement, the expression is evaluated again and the process repeats if it non-zero.
The meaning is:
Ø
Evaluate the expression.
Ø If its value is true (i.e.
not zero) executed the statement and go back to expression.
A.
while (i<=10)
B.
while ((i<=10) &&
(j<=15))
C.
while (j<10 &&
(b>15 || c>20))
2. for loop:-
(it
is repeated for a specified number of times)
This loop allows us to specify three things about a loop in a single line:
I. Setting a loop counter to an initial value.
II. Testing the loop counter to determine whether its value has reached the number of desired repetitions.
III. Increasing the value of loop counter each time the program segment within the loop has been executed.
Syntax:-
for (initialization; test condition ; increment/decrement)
{
// body of the loop
}
Nested for loop:- (More than one times repetition using loop)
Nesting is a programming concept where one control structure is placed inside another. Nested loops can be extremely useful, but also confusing to deal with.
Multiple initialized in the loop:- for (i=1; j=1; j<=10; j++)
3. do-while loop:-
(Firstly executes once & then check the condition)
It is also Variable-Length Loop. In do statement, the program proceeds to evaluate the body of the loop first. At the end of the loop, the test-condition in the while statement is evaluated. If the condition is true, the program continues to evaluate the body of the loop once again. This process continues as long as the condition is true. When the condition becomes false, the loop will be terminated and control goes to the statement that appears immediately after the while statement.
In this loop statement, it guarantees at least one execution of the statement. The effect is that the statement part is executed once before the controlling expression is evaluated, so this guarantees at least one trip around the loop.
Syntax:-
do
{
// body of the loop
} while (test condition);
When a break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop. When the loops are nested, the break would only exit from the loop containing it. The break will exit only a single loop.
The controlled statement is executed until the loop broken by some means. The simplest and common way of doing this is by using a break statement. A break statement consists simply of the word break followed by a semi-colon. Its effect is to cause immediate exit from the enclosing loop statement.
Break Statement occurs in the body of a switch, do-while, while or for statement. When it is executed the control of flow jumps to the statement immediately following the body of the statement containing the break.
Syntax:-
Ø while (………...)
{
// body
of the loop
-----------------------
if
(condition)
{
break;
}
----------------------
}
Ø for (…………….)
{
// body
of the loop
-----------------------
if(condition)
{
break;
}
----------------------
}
Ø
do
{
// body of the loop
--------------------
if (condition)
{
break;
}
-----------------
} while (………);
This statement has only a limited number of uses. The rules for its use are the same as for break, with the exception that it doesn’t apply to switch statements. Executing a continue starts the next iteration of the smallest enclosing do-while, while or for statement immediately. The use of continue is largely restricted to the top of loops, where a decision has to be made whether or not to execute the rest of the body of the loop.
continue is also a keyword. This statement allows us to do this. When the keyword continue is encountered inside any C loop. Control automatically passes to the beginning of the loop.
Syntax:-
for (…………….)
{
// body of the loop
if(condition)
{
continue;
}
--------------------
---------------------
}
The goto statement is used to alter the normal sequence of program execution by transferring control to some other part of the program.
A goto statement can transfer the control to any place in a program, it is useful to provide branching within a loop. Another important use of goto statement is to exit from deeply nested loops when an error occurs.
Avoid goto statements because it makes a C programmer’s life miserable. Its use is one of the reasons that programs becomes unreliable, unreadable & hard to debug. Many compilers generate a less efficient code. In good programming techniques, goto is always avoided.
goto label:-
The label is used to label the target statement to which control will be transferred. The control will be transferred anywhere within the current program (function). The target statement must be labeled, and the label must be followed by a colon. Thus the target statement will appear as:
label: target statement
Each label statement within the program must have a unique label. No two statements can have the same label.
Syntax:-
0 comments:
Post a Comment