L4-Control Statement in C - Part 2.ppt
L4-Control Statement in C - Part 2.ppt
Content for the slides have been taken from the various sources and
from the reference books.
Output:
i=1 j=l j=2 j=3 j=4 i=2 j=l j=2 J=3 j=4 i=3 j = 1 j = 2 j = 3 j = 4
Nesting of Loops
Infinite loop
• The loops that go on executing infinitely and never terminate are called infinite loops.
Sometimes we write these loops by mistake while sometimes we deliberately make use
of these loops in our programs.
Infinite loop
• A common mistake made by beginners is to use the assignment operator(=)
where equality operator(==) should have been used.
• If this mistake is made in the loop condition then it may cause the loop to
execute infinitely. For example consider this loop:
while(n=2)
{
……
}
• Here we wanted the loop to execute till the value of n is equal to 2. So we
should have written n= =2 but mistakenly we have written n = 2.
• Now n = 2 is an assignment expression and the value of this expression is 2,
which is a nonzero(true) value and hence the loop condition is always true.
Infinite loop
Break Statement
• break statement is used inside ,loops and switch statements. Sometimes it becomes
necessary to Coming out of the loop even before the loop condition becomes false.
• In such a situation, break statement is used to terminate the loop.
• This statement causes an immediate exit from that loop in which this statement appears.
Break Statement
Break Statement
continue statement
• The continue statement is used when we want to go to the next iteration of the
loop after skipping some statements of the loop.
• This continue statement can be written simply as continue;
• It is generally used with a condition. When continue statement is encountered
all the remaining statements in the current iteration are not executed and the
loop continues with the next iteration.
• The difference between break and continue is that when break is. encountered
the loop terminates and the control is transferred to the next statement
following the loop, but when a continue statement is encountered the loop is
not terminated and the control is transferred to the beginning of the loop.
• In while and do-while loops, after continue statement the control. is transferred
to the test condition and then the loop continues, whereas in for loop after
continue statement the control is transferred to update expression and then the
condition is tested.
continue statement
continue statement
goto statement
goto statement
Thank you for your attention!
< Questions?